Skip to content
35 changes: 25 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,20 @@ return [
* ]
*/

// 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' => [
'app.debug'
],
// 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' => [
'app.debug'
],

// 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' => false,
// 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' => false,

// Split up the exported messages.js file into separate files for each locale.
// This is to ensue faster loading times so one doesn't have to load translations for _all_ languages.
'split_export_files' => true,
];
```

Expand Down Expand Up @@ -159,9 +163,20 @@ The files can then be generated using the artisan command:
`php artisan js-localization:export`

This will generate two files in your target directory:
* `messags.js` contains your translation strings
* `messages.js` contains your translation strings
* `config.js` contains your exported config values

If you want to automatically split up the `messages.js` file into separate .js files for each locale, you can set the following to true in your `config/js-localization.php` config file:

```
'split_export_files' => true,
```

This will in turn _also_ generate the following file(s) in your target directory:
* `lang-{locale}.js` contains one language's translation strings, if the `split_export_files` config option is set to true

With static generation you can also make use of `@yield('js-localization.head.exported')` in your blade template. This will automatically include your statically generated .js files for you.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andywer I briefly mention the new @yield here, which is related to the @section part below


Remember that the files needs to be regenerated using `php artisan js-localization:export` every time any translation strings are edited, added or removed.

Features
Expand Down
7 changes: 7 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@
*/
'disable_config_cache' => false,

/*
|--------------------------------------------------------------------------
| Whether or not to split up the exported messages.js file into separate
| lang-{locale}.js files.
|--------------------------------------------------------------------------
*/
'split_export_files' => false,
];
21 changes: 21 additions & 0 deletions resources/views/head.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@
Lang.setLocale("{{ App::getLocale() }}");
</script>
@stop

@section('js-localization.head.exported')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that new @section actually a part of the file splitting changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, well, kind of. It allows you to use @yield('js-localization.head.exported'), which in turn will include the <script> tags for the exported files if they exist, and fallback to messages.js or just serving from the API endpoint instead if used incorrectly.

@if (file_exists(config('js-localization.storage_path') . '/js-localization.min.js'))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite hard to understand: Why do we check for ${storage_path}/js-localization.min.js and then set the URL to url('/vendor/js-localization/js-localization.min.js') / url('/js-localization/localization.js')?

It's not obvious how ${storage_path}/js-localization.min.js and the URLs relate to each other.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right of course. If someone would change the storage_path config, that would not work indeed. Maybe skip the @section altogether? Otherwise one would have to have a function that returns the correlation of storage_path and public_path (if that exists) to be able to figure out where the exported files actually go. And that might be a little bit out of scope for the plugin?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would probably be the easiest solution now: Removing the section again and changing the Readme to tell the user how to use the locale-specific files.

Could still add a section like this in a follow-up PR.

<script src="{{ url('/vendor/js-localization/js-localization.min.js') }}"></script>
@else
<script src="{{ url('/js-localization/localization.js') }}"></script>
@endif

@if (Config::get('js-localization.config'))
<script src="{{ url('/vendor/js-localization/config.js') }}"></script>
@endif

@if (Config::get('js-localization.split_export_files') && file_exists(config('js-localization.storage_path') . '/lang-' . App::getLocale() . '.js'))
<script src="{{ url('/vendor/js-localization/lang-' . App::getLocale() . '.js') }}"></script>
@else
<script src="{{ url('/vendor/js-localization/messages.js') }}"></script>
@endif
<script>
Lang.setLocale("{{ App::getLocale() }}");
</script>
@stop
30 changes: 29 additions & 1 deletion src/Console/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,43 @@ public function createPath($filename)
*/
public function generateMessagesFile($path)
{
$splitFiles = Config::get('js-localization.split_export_files');
$messages = MessageCachingService::getMessagesJson();

$contents = 'Lang.addMessages(' . $messages . ');';
if ($splitFiles) {
$this->generateMessageFiles(File::dirname($path), $messages);
}

$contents = 'Lang.addMessages(' . $messages . ');';

File::put($path, $contents);

$this->line("Generated $path");
}

/**
* Generate the lang-{locale}.js files
*
* @param string $path Directory to where we will store the files
* @param string $messages JSON string of messages
*/
protected function generateMessageFiles(string $path, string $messages)
{
$locales = Config::get('js-localization.locales');
$messages = json_decode($messages, true);

foreach ($locales as $locale) {
$fileName = $path . "/lang-{$locale}.js";

if (key_exists($locale, $messages)) {
$content = 'Lang.addMessages(' . json_encode([$locale => $messages[$locale]], JSON_PRETTY_PRINT) . ');';
}

File::put($fileName, $content);
$this->line("Generated $fileName");
}
}

/**
* Generage the config file.
*
Expand Down