Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add new language dynamically #430

Open
camohub opened this issue Sep 6, 2024 · 4 comments
Open

How to add new language dynamically #430

camohub opened this issue Sep 6, 2024 · 4 comments
Labels

Comments

@camohub
Copy link

camohub commented Sep 6, 2024

Hi there,
can somebody tell me please how to achieve this.
I need to add new language to the app dynamically.
There is config/translatable.php which contains

    'locales' => ['en', 'cs', 'cz', 'sk', 'pl',  'de', 'hu',  'uk',  'us', 'gb', ],

This does not allow to add new language which is not defined in this array. I have a controller which want to add new languages and tries to do it this way.

        config([
            'translatable.locales'         => $newLocales,
            'translatable.locale'          => $data['default_language'],
            'translatable.fallback_locale' => $data['default_language'],
        ]);

I thought this will cange the config and library will add new language. But the library allow only the languages which are in config file array.
How can I dynamically add new languages to the application?
Thanks.

@Oleksandr-Moik
Copy link
Contributor

Hi, @camohub. It does not work, because the library does not access directly to the configurations and works with the helper class Locales. In general, after setting new locales you need to reload it with method load

config([
    'translatable.locales'         => $newLocales,
    'translatable.locale'          => $data['default_language'],
    'translatable.fallback_locale' => $data['default_language'],
]);

resolve(Locales::class)->load();

You can check how it's works in test suite:

public function it_loads_the_locales_from_the_configuration(): void
{
$this->app['config']->set('translatable.locales', [
'de',
]);
$this->app->make('translatable.locales')->load();
self::assertEquals(['de'], $this->app->make('translatable.locales')->all());
$this->app['config']->set('translatable.locales', [
'de',
'en',
]);
self::assertEquals(['de'], $this->app->make('translatable.locales')->all());
$this->app->make('translatable.locales')->load();
self::assertEquals(['de', 'en'], $this->app->make('translatable.locales')->all());
}

@camohub
Copy link
Author

camohub commented Sep 6, 2024

Thank you very much. It seems it works.
Where in the documentation I can find this?

@Oleksandr-Moik
Copy link
Contributor

@camohub
Copy link
Author

camohub commented Sep 10, 2024

Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants