Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Reno1979's avatar

Dynamically override/extend language files

Our current resources folder in laravel 5.3 looks like this

    themename1\
        assets\
        views\
        lang\
    themename2\
        assets\
        views\
        lang\
    lang\
        en\
        nl\

Our views are allready loaded dynamically based on the current theme name.

How can we also use the themed language files?

An extension to the default base language files is preferred, if this is not possible the change of the lang path can be a solution. (but no clue how to do this).

Loading with a namespace worked, but is not what we want. (lang(themename::....) )

The manual shows how to override language files when you build a package (place overides in resources\lang\vendor{package}{locale} ). But I was not able to use this technique without an actual package. Does anyone have a good suggestion?

0 likes
7 replies
zachleigh's avatar

Maybe keep them in the lang folder? Something like this:

lang\
    en\
        themename1\
        themename2\
        nl\
        themename1\
        themename2\

And then access them like normal:

lang('themename1.directory.value');

If you want them in the same folders as the other theme assets, maybe symbolic links in the lang directory going to the theme lang files??

1 like
Reno1979's avatar

thank you @zachleigh

The subdirectories inside might just work partially, so it is a start.

Making it switch dynamically :

trans($themename.'.directory.value');

The problem with this is that all language files should contain the exact same keys. (so basically be full copies with adjustments) It seems a bit redunant and cumbersome to maintain.

Want I want is that this call:

trans('directory.value');

gives the default return value from the nl/ directory, except when the language file from the theme overwrites this value.

zachleigh's avatar

Made a mistake: the function is actually `trans':

trans('directory.value');

So what you could do is make your own lang function where you get the set theme, stick it on the front of your key string, and then call trans.

function lang($keys) {
    $theme = $getTheme; // you probably have some way of getting the currently set theme

    return trans($theme . '.' . $keys);
}

Then call your function like normal:

lang('directory.key');

Just an idea that might be worth exploring.

2 likes
Reno1979's avatar

@zachleigh

This is one way to do it, with some more work I could simply check if the key $theme . '.' . $keys returns itself or a string value.

if the used key returns is own key value the trand() function is not able to find this key. Then the trans function should try trans($keys)

But I'm pretty sure this has a performance penalty.

There must be a better way, even packages can extend the language files.

Reno1979's avatar

The documentation says the following:

Some packages may ship with their own language files. Instead of changing the package's core files to tweak these lines, you may override >them by placing files in the resources/lang/vendor/{package}/{locale} directory.

So, for example, if you need to override the English language lines in messages.php for a package named skyrim/hearthfire, you should place >a language file at: resources/lang/vendor/hearthfire/en/messages.php. Within this file, you should only define the language lines you wish to >override. Any language lines you don't override will still be loaded from the package's original language files.

Can this build in Laravel functionality be used for my goal??

Reno1979's avatar

ok I have solved it :

The idea is to register the themename as namespace and to overwrite the helper trans()

[overwriting Laravel helpers is another topic ... ;) ]

In the `AppServiceProvider' we add the following to the 'boot' method. :

    // Make sure we know about the namspace in the trans helper
    $customPath = 'yourCustomPath'; // We use our custom resource_path() helper 
    $namespace = $getThemeName;   // We stored it in config('custom.themename') 
        $this->loadTranslationsFrom($customPath, $namespace );

And this is our new trans() helper:

function trans($id = null, $parameters = [], $domain = 'messages', $locale = null) {

    if (is_null($id)) {
            return app('translator');
    }
    
    $namespace  = $getThemeName;   // We stored it in config('custom.themename') 
    $newId          =$namespace .'::'.$id;

    $translatedValue = app('translator')->trans($newId, $parameters, $domain, $locale);
    
    // If the translater can't locate the key ($newId), it will return the same value as the given key ($newId)
    if($translatedValue == $newId){
        $translatedValue = app('translator')->trans($id, $parameters, $domain, $locale);
    }

    return $translatedValue;
}

Because this causes a double try for values that are not overwritten by the custom language files I am a bit worried about a performance penalty.

1 like

Please or to participate in this conversation.