andywer / laravel-js-localization

Simple, ease-to-use and flexible package for the Laravel web framework. Allows you to use localized messages of the Laravel webapp (see `resources/lang` directory) in your Javascript code.
MIT License
143 stars 51 forks source link

How to import translations ? #24

Closed Xeroner closed 7 years ago

Xeroner commented 8 years ago

How to import translations from resources/lang/en/all.php ?

Lang.addMessages({ "en": { "partnership": "Create report", } }); Lang.setLocale("en"); console.log(Lang.get('partnership'));

Xeroner commented 8 years ago

I wan't to import my translation, not build new one...

andywer commented 8 years ago

I am not sure if I get what your question is... You posted some JS snippet that is supposed to work.

What do you mean be "import, not build new one"?

Xeroner commented 8 years ago

How to import translations from resources/lang/en/all.php to .js file ?

andywer commented 8 years ago

See https://github.com/andywer/laravel-js-localization#configuration.

So if you use it like Lang::get('all.xy') in PHP, then configure it like:

    ...
    'messages' => [ 'all' ],
    ...
hpolonkoev commented 8 years ago

Hi @andywer ,

I have an Italian language folder in resources/lang. I added the Italian to config file : 'locales' => ['en', 'it'] And added my translations into messages : 'messages' => [ 'test' ],

When I viewing the page in English everything works fine, when I switch to Italian I am getting error : localization.js:1 Uncaught Error: No messages defined for locale: "it". Did you forget to enable it in the configuration? I checked the message.js file and it doesn't contain the IT translations in.

Any ideas why ?

andywer commented 8 years ago

Hey! No, it's hard to say. Did you do a php artisan js-localization:refresh?

hpolonkoev commented 8 years ago

@andywer, thanks for your reply.

I disabled the cache in config : 'disable_config_cache' => true. In case, I ran `php artisan js-localization:refresh too but no success.

andywer commented 8 years ago

Sorry, @hpolonkoev. Can't tell from here...

I let you know if I have another idea. Please let me/us know if you solve it :)

hpolonkoev commented 8 years ago

@andywer thanks anyway!

ghost commented 7 years ago

Hi, i use this config: `<?php

return [

/*
|--------------------------------------------------------------------------
| Define the languages you want exported messages for
|--------------------------------------------------------------------------
*/

'locales' => ['ru'],

/*
|--------------------------------------------------------------------------
| Define the messages to export
|--------------------------------------------------------------------------
|
| An array containing the keys of the messages you wish to make accessible
| for the Javascript code.
| Remember that the number of messages sent to the browser influences the
| time the website needs to load. So you are encouraged to limit these
| messages to the minimum you really need.
|
| Supports nesting:
|   [ 'mynamespace' => ['test1', 'test2'] ]
| for instance will be internally resolved to:
|   ['mynamespace.test1', 'mynamespace.test2']
|
*/

'messages' => [
    'status'
],

/*
|--------------------------------------------------------------------------
| Set the keys of config properties you want to use in javascript.
| Caution: Do not expose any configuration values that should be kept privately!
|--------------------------------------------------------------------------
*/
'config' => [],

/*
|--------------------------------------------------------------------------
| Disables the config cache if set to true, so you don't have to
| run `php artisan js-localization:refresh` each time you change configuration files.
| Attention: Should not be used in production mode due to decreased performance.
|--------------------------------------------------------------------------
*/
'disable_config_cache' => true,

];`

but in section @yield('js-localization.head') in layout i have code: `

<script type="text/javascript">
    Lang.setLocale("en");
</script>`
ghost commented 7 years ago

i fink, need ?

ghost commented 7 years ago

sorry ,my english) i hope u will understand me

andywer commented 7 years ago

Hey @kocapb.

I guess you are lacking a App::setLocale('ru') in your PHP code. Does App::getLocale() return the expected result?

ghost commented 7 years ago

thanks a lot, bug resolved