#Laravel Overwriting lang classes

1 messages · Page 1 of 1 (latest)

merry rock
#

Hello everybody,
as the title is saying i want to overwrite some lang classes.
The plan is to archive costume translation based on a route parameter.
For example in the rout parameter is passed an id of a project, then i want to load the default lang folder en/de or what ever.
After that has happened i want to check if there is another class with the same name inside a customization/projectID/lang/en folder.
If there is found one i want to overwrite the default one.

For example there is in the default one :
'id' => 'ID'
'example' => 'example'

And in the one inside the customization folder :
'id' => 'critical ID'

I want to get the 'critical ID' when i use 'id' inside the blade but still want to be able to use the 'example' from the default.

If anyone has an idea or experience with something like that please share it with me.

golden granite
merry rock
#

Thanks for the replay, let me check that. *
Yeah i think its not common to do such things but in my company we got around 1000 custom trans for one specific template and to load them all on top of the defaults one would be a bit overkill i guess 🙂

#

@golden granite That looks like the thing i need ^^ i just need to check how its working i am not that good coder just started 1.5 years ago but i will get it as soon as i work with this.
Thank u Sir !

dusky pebble
#

@merry rock Why do you want to load different translations based on a route parameter? Translations are for translating a string into different languages. Why would the translation change project to project?

merry rock
#

In my company we have a project portal this there are many projects over 10000 which are all based on a default layout and some dynamic loaded in content.
Now some customers got some extra wished like an field has to get another name for example "id" => "Id" should be "id" => "critical id" and so on.
There are more then 1000 extra wishes so far and we do not want to load in 1000 extra entries on each project to meet this requirement.
We want to check if there is a class with the same name like in the lang folder inside a customization folder and if yes overwrite the matching array key entries to change "id" => "id" to "id" => "critical id" to have first a better overview which words are changed for a specific project and like i said to not load in all 1000 unique translations.

#

@dusky pebble

dusky pebble
#

Then it sounds like a better approach would be to use namespaced translations.

#

Where a project-specific directory is checked first, and then it falls back to the regular lang directories.

merry rock
#

But then i need to put all translations inside each project-specific directory or not ? There are still many matching ones on the default translations. @dusky pebble

dusky pebble
#
Lang::addNamespace('project', [
    // Put project-specific location here
    
    // Fall back to regular lang directories
    resource_path('lang'),
    app_path('lang'),
]);

Then you’d reference keys using something like __('project::some_key')

merry gust
#

Martin, I don't mean to thread jack, but can something like that be done loading json from MySQL? I ran into this problem but I made my own parser and fallback to laravel if the key is not found. But I didn't get around to trans_choice as yet 😦

merry rock
merry rock
prisma mesa
#

Such code should be going in one of your service providers.

merry rock
#

Thanks i will check that.

merry rock
#

Okay i tried i failed i tried again and failed again ^^
What i have done so far is created a provider. Created a custom folder for the project-specific translation and wrote down some test data.
Then i tried to implement the code the dear Mr. Bean has given me and all went horribly wrong xD

#

Its just a bit much for me i need more detailed instructions ... 😦 sry

prisma mesa
#

You created a new provider or added to an existing, like AppServiceProvider? Because if you made a new one, you must register it in your app.php config or from within another service provider

merry rock
#

Did that already have created a LangServiveProvider

#

have registered in the app.php

prisma mesa
#

As far as Lang goes, I do not work with it often so I cannot comment on the exact code Bean shared. I'm simply chiming in to inform you that such code needs to be put in a provider.

#

What errors are you getting?

merry rock
#

That is done, its more about the provider itself. There is the boot and register method. In which one i need to copy the code ? .

#

and how i will get my route parameter inside there by calling it inside a blade template i am a bit confused.

prisma mesa
#

This would be explained in the provider docs. For you, that would be the boot method because register is where you bind your services into the container, and boot is where you then configure any services as you need them.

merry rock
#
    public function boot($projectId)
    {
        Lang::addNamespace('project', [
            // Put project-specific location here
            resource_path(sprintf('custom_lang.%d', $projectId)),

            // Fall back to regular lang directories
            resource_path('lang'),
            app_path('lang'),
        ]);
    }
}
#

Unable to resolve dependency [Parameter #0 [ <required> $projectId ]] in class App\Providers\LangServiceProvider

#

Then i tryed to call inside the blade {{__('project::'.$id)}}

prisma mesa
#

Where did you see any examples of boot accepting an argument? Neither register or boot are handed anything

merry rock
#

Yeah that's what i thought too and there is my problem how can i then get the right resource path of my custom lang folder ?

prisma mesa
#

How are you even defining this custom path based on this project ID? Is it a config?

merry rock
#

that's exactly the point i am confused about.

merry rock
#

i am so lost right now -.-

dusky pebble
#

You need to tackle things one at a time.

#

Instead of overwhelming yourself.

merry rock
#

True but i wanne learn this ^^ and i dont rly know where to start.
It cannot be that hard to archive what i want.

dusky pebble
#

I’ve already told you where to start though?

#

You’re probably going to have to register the namespace in a middleware so that you can actually get your project ID. It’s not going to be accessible in a service provider, and it’s definitely not going to be magically injected into a boot method of a service provider.

merry rock
#

Thant's pretty plausible.

prisma mesa
#

As Martin said, you should not be relying on request/sessions in providers. They run well before your app takes its path to your controller. Look for examples of people setting their app locale or tenants within a Middleware and extrapolate that idea to solve your issue at hand.

merry rock
#

Yeah that's what i wanted to do from the start on, the only thing i was not sure about was how to reload the lang folder based on a given path. Cause i do not know when this folders are getting loaded in. Or how to manuely load in a costume made translation file after the default ones already got loaded.

merry rock
#

@dusky pebble

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Lang;

class CostumeLangLoader
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        Lang::addNamespace('project', [
            // Put project-specific location here
            resource_path(sprintf('custom_lang\%d',$request->route('id'))),
            // Fall back to regular lang directories
            resource_path('lang'),
            app_path('lang'),
        ]);
        return $next($request);
    }
}

Should be fine.