nWidart / laravel-modules

Module Management In Laravel
https://docs.laravelmodules.com
MIT License
5.54k stars 963 forks source link

How to use json translation files using modules? (Translation Strings As Keys) #954

Closed dexcell closed 2 years ago

dexcell commented 4 years ago

I want to use translation strings as key, how to do it in laravel modules? https://laravel.com/docs/6.x/localization#using-translation-strings-as-keys

I've tried adding the json file to modules lang folder. e.g. Modules\ModuleName\Resources\lang\id.json but laravel doesn't read it.

Does laravel modules support Translation Strings As Keys? (Not short key translation).

Thanks

JamesHemery commented 4 years ago

Hi @dexcell,

This package can help you => laravel-json-translation-loader

dexcell commented 4 years ago

@JamesHemery Thank you! i will have a look at it

nWidart commented 4 years ago

I'll close this as there hasn't been a reply in a while, feel free to comment if needed.

dexcell commented 4 years ago

Unfortunately there is still no solution for this.

nWidart commented 4 years ago

This is not related to this package as far as I can see. Users can choose to do this however they prefer.

dexcell commented 4 years ago

Hi thank you for the reply,

I'm sorry i have to a bit disagree, i think laravel modules should support default laravel translation out of the box.

Since default laravel offers translation strings as keys, then i want to use it inside the modules just like normal laravel app. (isn't that the purpose of laravel modules?).

For now i managed to load the json translation manually by adding loadJsonTranslationsFrom inside registerTranslation function in service provider file. I hope the generator would done this automatically (e.g php artisan module:make ModuleName)

/**
     * Register translations.
     *
     * @return void
     */
    public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/modulename');

        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, 'modulename');
            $this->loadJsonTranslationsFrom($langPath);
        } else {
            $this->loadTranslationsFrom(module_path('ModuleName', 'Resources/lang'), 'modulename');
            $this->loadJsonTranslationsFrom(module_path('ModuleName', 'Resources/lang'));
        }
    }
nWidart commented 4 years ago

This package supports laravel translations out of the box. Both json and the PHP array way.

dexcell commented 4 years ago

Unfortunately on my test it doesn't load the json translation file, only the PHP array way. (On laravel v6) i haven't tried the v7 yet.

After i added loadJsonTranslationsFrom then it does read the json file correctly. Looks like loadTranslationsFrom only load php array translation.

dexcell commented 4 years ago

Sorry, i mean json translation file without prefixing.

l1th1um commented 4 years ago

I use this

$this->loadJsonTranslationsFrom(DIR.'/../Resources/Lang');

raulpacheco2k commented 3 years ago

Hi thank you for the reply,

I'm sorry i have to a bit disagree, i think laravel modules should support default laravel translation out of the box.

Since default laravel offers translation strings as keys, then i want to use it inside the modules just like normal laravel app. (isn't that the purpose of laravel modules?).

For now i managed to load the json translation manually by adding loadJsonTranslationsFrom inside registerTranslation function in service provider file. I hope the generator would done this automatically (e.g php artisan module:make ModuleName)

/**
     * Register translations.
     *
     * @return void
     */
    public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/modulename');

        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, 'modulename');
            $this->loadJsonTranslationsFrom($langPath);
        } else {
            $this->loadTranslationsFrom(module_path('ModuleName', 'Resources/lang'), 'modulename');
            $this->loadJsonTranslationsFrom(module_path('ModuleName', 'Resources/lang'));
        }
    }

Thanks! This solution helped me! I believe this library should implement this solution, it is a flawed view to think that it should not be implemented.

bedoya commented 3 years ago

This package supports laravel translations out of the box. Both json and the PHP array way.

Wouldn't it be nice if your answer was not always limited to state the fact that your packages can do stuff, but to guide others who care enough to give a try to your work? You state that your package supports json translations out of the box, but you don't care to show how we should do it, because you assume that if we idiots don't know how to do it we shouldn't even be using your package, right? WRONG! ... have a little modesty and get down to earth with mortals.

raulpacheco2k commented 3 years ago

@bedoya Congratulations on your reply. I agree with you.

tuaititecnologia commented 2 years ago

Nowdays still does not work out of the box as mentioned before. At least is not documented. This is the registerTranslations() not using hardcoded module name, so it's easier to copy and paste.

public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/' . $this->moduleNameLower);
        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, $this->moduleNameLower);
            $this->loadJsonTranslationsFrom($langPath);
        } else {
            $this->loadTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
            $this->loadJsonTranslationsFrom(module_path($this->moduleName, 'Resources/lang'));
        }
    }
raulpacheco2k commented 2 years ago

Nowdays still does not work out of the box as mentioned before. At least is not documented. This is the registerTranslations() not using hardcoded module name, so it's easier to copy and paste.

public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/' . $this->moduleNameLower);
        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, $this->moduleNameLower);
            $this->loadJsonTranslationsFrom($langPath);
        } else {
            $this->loadTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
            $this->loadJsonTranslationsFrom(module_path($this->moduleName, 'Resources/lang'));
        }
    }

In my tests this works on Laravel 8.

Marciobds commented 2 years ago

I'm having some trouble with this as well, but for me only the translations from the first module loaded are being actually loaded, translations from all other modules are not.

I can't find a way of making it work. But I am using https://orchid.software/ package for creating admin panel, so I don't know if I'm doing something wrong..

limsocheat commented 2 years ago

Nowdays still does not work out of the box as mentioned before. At least is not documented. This is the registerTranslations() not using hardcoded module name, so it's easier to copy and paste.

public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/' . $this->moduleNameLower);
        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, $this->moduleNameLower);
            $this->loadJsonTranslationsFrom($langPath);
        } else {
            $this->loadTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
            $this->loadJsonTranslationsFrom(module_path($this->moduleName, 'Resources/lang'));
        }
    }

I followed your instruction, but translation is not loaded. It return raw string instead. {{ __('reservation:Reservation Confirmed') }}

return reservation:Reservation Confirmed

my ReservationServiceProvider:

/**
     * Register translations.
     *
     * @return void
     */
    public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/' . $this->moduleNameLower);

        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, $this->moduleNameLower);
            $this->loadJsonTranslationsFrom($langPath);
        } else {
            $this->loadTranslationsFrom(module_path($this->moduleName, 'resources/lang'), $this->moduleNameLower);
            $this->loadJsonTranslationsFrom(module_path($this->moduleName, 'resources/lang'));
        }
    }
dcblogdev commented 2 years ago

I'll re-open this so I can look into this further.

dcblogdev commented 2 years ago

it works for me, enabled both json and traditional files:

public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/' . $this->moduleNameLower);

        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, $this->moduleNameLower);
            $this->loadJsonTranslationsFrom($langPath, $this->moduleNameLower);
        } else {
            $this->loadTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
            $this->loadJsonTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
        }
    }

Changing my language in config to fr

'locale' => 'fr',

then in a module create a fr.json file:

{
  "Name ok": "changed",
}

In a view:

{{ __('Name ok') }}

shows changed for me.

When using json translations you can only prefix the key if you manually add a prefix such as:

{
  "reservation:Name ok": "changed",
}

In a view:

{{ __('reservation:Name ok') }}

This will be due to Laravel will be looking for an exact match.

limsocheat commented 2 years ago

it works for me, enabled both json and traditional files:

public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/' . $this->moduleNameLower);

        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, $this->moduleNameLower);
            $this->loadJsonTranslationsFrom($langPath, $this->moduleNameLower);
        } else {
            $this->loadTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
            $this->loadJsonTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
        }
    }

Changing my language in config to fr

'locale' => 'fr',

then in a module create a fr.json file:

{
  "Name ok": "changed",
}

In a view:

{{ __('Name ok') }}

shows changed for me.

When using json translations you can only prefix the key if you manually add a prefix such as:

{
  "reservation:Name ok": "changed",
}

In a view:

{{ __('reservation:Name ok') }}

This will be due to Laravel will be looking for an exact match.

I followed all of your code, but still not working:

1. service provider

Screen Shot 2022-03-30 at 4 55 39 PM

2. km.json

Screen Shot 2022-03-30 at 4 56 05 PM

3. blade

Screen Shot 2022-03-30 at 4 56 31 PM

4. result

Screen Shot 2022-03-30 at 4 56 52 PM

I also tried: {{ ('academic:Academic') }}, {{ ('academic::Academic') }}, {{ __('Academic') }} in blade, but still not render.

Laravel Version: "laravel/framework": "^9.0", Module Version: "nwidart/laravel-modules": "^9.0", docs: https://docs.laravelmodules.com/v9/languages

nyneplus commented 2 years ago

I am upgrading to Laravel 9 and experience the same as @limsocheat. Shall I try to find a root cause or is any found already?

vietsach0808 commented 2 years ago

I have found a solution to this problem (Laravel 9). Open file: /config/app.php and change

'locale' => '',
'fallback_locale' => '',

Create the Locale middleware and set the locale you want.

public function handle($request, Closure $next)
    {
        $locale = 'en';
        \App::setLocale($locale);
        return $next($request);
    }

Add this middleware to your routes. After it the translation will work.

Route::group(['middleware' => 'locale'], function() {
// your route
});
nyneplus commented 2 years ago

For me it was the component BenSampo/laravel-enum. It loaded a translation too soon. I found it by adding debug_print_backtrace();exit();

to Illuminate\Foundation\Helpers.php @line 882 in the function __($key = null, $replace = [], $locale = null) so i saw where it was used to soon.

Rattone commented 2 years ago

I followed all of your code, but still not working:

1. service provider Screen Shot 2022-03-30 at 4 55 39 PM

2. km.json Screen Shot 2022-03-30 at 4 56 05 PM

3. blade Screen Shot 2022-03-30 at 4 56 31 PM

4. result Screen Shot 2022-03-30 at 4 56 52 PM

I also tried: {{ ('academic:Academic') }}, {{ ('academic::Academic') }}, {{ __('Academic') }} in blade, but still not render.

Laravel Version: "laravel/framework": "^9.0", Module Version: "nwidart/laravel-modules": "^9.0", docs: https://docs.laravelmodules.com/v9/languages

I have the same problem in a Laravel 8 installation, performing the same steps indicated by @dcblogdev and @limsocheat the translations are not read, keep getting the translations from project root (not module).

...beyond this, am I wrong or the loadJsonTranslationsFrom() method just want one parameter?

https://laravel.com/api/9.x/Illuminate/Support/ServiceProvider.html#method_loadJsonTranslationsFrom

Commit: https://github.com/nWidart/laravel-modules/commit/3b1285084019d25eb08619d5bfb1f958d17c2a47

shawe commented 7 months ago

image

For me, the problem with the translations are only the path used, by default is not using the same standard path that in laravel projects. Also, for me, it may include the original array translation folder in the root module folder (not in core project as lang/modules, the lang/XX/YYYY.php inside the module).

By default in laravel-modules it's using a combined path that is not based on the original laravel structure for languages:

At this moment, for me is enough only with json file to get all translations in one file, but I think that is always preferable get it in the common way in the Laravel to get full compatibility on implement it has any dev prefers to use on each case.