Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
#56 Extended to generate multiple files per locale (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
rabrowne85 authored and martinlindhe committed May 1, 2019
1 parent 056293f commit 13ec531
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import Locale from './vue-i18n-locales.generated';

Vue.use(VueInternationalization);

const lang = document.documentElement.lang.substr(0, 2);
const lang = document.documentElement.lang.substr(0, 2);
// or however you determine your current app locale

const i18n = new VueInternationalization({
Expand Down Expand Up @@ -93,7 +93,7 @@ Object.keys(Locales).forEach(function (lang) {
## Using vuex-i18n
### vuex-i18n
```
npm i --save vuex-i18n
Expand Down Expand Up @@ -152,13 +152,13 @@ php artisan vue-i18n:generate --format {es6,umd,json}
```
php artisan vue-i18n:generate --format umd
```
An UMD module can be imported into the browser, build system, node and etc.
An UMD module can be imported into the browser, build system, node and etc.
Now you can include the generated script in the browser as a normal script and reference it with window.vuei18nLocales.
```vue
<script src="{{ asset('js/vue-i18n-locales.generated.js') }}"></script>

// in your js
// in your js
Vue.use(VueI18n)
Vue.config.lang = Laravel.language
Object.keys(window.vuei18nLocales).forEach(function (lang) {
Expand All @@ -169,12 +169,23 @@ You can still require/import it in your build system as stated above.
One advantage of doing things like this is you are not obligated to do a build of your javascript each time a the translation files get changed/saved. A good example is if you have a backend that can read and write to your translation files (like Backpack). You can listen to a save event there and call vue-i18n-generator.
## Generating Multiple Files
Sometimes you may want to generate multiple files as you want to make use of lazy loading. As such, you can specify that the generator produces multiple files within the destination directory.
There are two options:
1. One file per laravel module language file using switch ```--multi```
2. One file per locale using switch ```--multi-locales```
```
php artisan vue-i18n:generate --multi{-locales}
```
## Parameters
The generator adjusts the strings in order to work with vue-i18n's named formatting,
so you can reuse your Laravel translations with parameters.
resource/lang/message.php:
```php
return [
Expand Down
7 changes: 4 additions & 3 deletions src/Commands/GenerateInclude.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GenerateInclude extends Command
*
* @var string
*/
protected $signature = 'vue-i18n:generate {--umd} {--multi} {--with-vendor} {--file-name=} {--lang-files=} {--format=es6}';
protected $signature = 'vue-i18n:generate {--umd} {--multi} {--with-vendor} {--file-name=} {--lang-files=} {--format=es6} {--multi-locales}';

/**
* The console command description.
Expand All @@ -37,6 +37,7 @@ public function handle()
$fileName = $this->option('file-name');
$langFiles = $this->option('lang-files');
$format = $this->option('format');
$multipleLocales = $this->option('multi-locales');

if ($umd) {
// if the --umd option is set, set the $format to 'umd'
Expand All @@ -47,9 +48,9 @@ public function handle()
throw new \RuntimeException('Invalid format passed: ' . $format);
}

if ($multipleFiles) {
if ($multipleFiles || $multipleLocales) {
$files = (new Generator($config))
->generateMultiple($root, $format);
->generateMultiple($root, $format, $multipleLocales);

if ($config['showOutputMessages']) {
$this->info("Written to : " . $files);
Expand Down
31 changes: 16 additions & 15 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,14 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l

$jsonLocales = json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;

if(json_last_error() !== JSON_ERROR_NONE)
{
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('Could not generate JSON, error code '.json_last_error());
}

// formats other than 'es6' and 'umd' will become plain JSON
if ($format === 'es6') {
$jsBody = $this->getES6Module($jsonLocales);
} elseif($format === 'umd') {
} elseif ($format === 'umd') {
$jsBody = $this->getUMDModule($jsonLocales);
} else {
$jsBody = $jsonLocales;
Expand All @@ -114,7 +113,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
* @return string
* @throws Exception
*/
public function generateMultiple($path, $format = 'es6')
public function generateMultiple($path, $format = 'es6', $multiLocales = false)
{
if (!is_dir($path)) {
throw new Exception('Directory not found: ' . $path);
Expand All @@ -139,7 +138,7 @@ public function generateMultiple($path, $format = 'es6')
$this->availableLocales[] = $noExt;
}
if ($fileinfo->isDir()) {
$local = $this->allocateLocaleArray($fileinfo->getRealPath());
$local = $this->allocateLocaleArray($fileinfo->getRealPath(), $multiLocales);
} else {
$local = $this->allocateLocaleJSON($fileinfo->getRealPath());
if ($local === null) continue;
Expand All @@ -157,13 +156,12 @@ public function generateMultiple($path, $format = 'es6')
$fileToCreate = $jsPath . $fileName . '.js';
$createdFiles .= $fileToCreate . PHP_EOL;
$jsonLocales = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
if(json_last_error() !== JSON_ERROR_NONE)
{
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('Could not generate JSON, error code '.json_last_error());
}
if ($format === 'es6') {
$jsBody = $this->getES6Module($jsonLocales);
} elseif($format === 'umd') {
} elseif ($format === 'umd') {
$jsBody = $this->getUMDModule($jsonLocales);
} else {
$jsBody = $jsonLocales;
Expand Down Expand Up @@ -201,7 +199,7 @@ private function allocateLocaleJSON($path)
* @param string $path
* @return array
*/
private function allocateLocaleArray($path)
private function allocateLocaleArray($path, $multiLocales = false)
{
$data = [];
$dir = new DirectoryIterator($path);
Expand Down Expand Up @@ -238,11 +236,14 @@ private function allocateLocaleArray($path)
if ($lastLocale !== false) {
$root = realpath(base_path() . $this->config['langPath'] . '/' . $lastLocale);
$filePath = $this->removeExtension(str_replace('\\', '_', ltrim(str_replace($root, '', realpath($fileName)), '\\')));
$this->filesToCreate[$filePath][$lastLocale] = $this->adjustArray($tmp);
if ($multiLocales) {
$this->filesToCreate[$lastLocale][$lastLocale][substr($filePath, 1)] = $this->adjustArray($tmp);
} else {
$this->filesToCreate[$filePath][$lastLocale] = $this->adjustArray($tmp);
}
}

$data[$noExt] = $this->adjustArray($tmp);

}
}
return $data;
Expand Down Expand Up @@ -291,10 +292,10 @@ private function adjustArray(array $arr)
*/
private function adjustVendor($locales)
{
if(isset($locales['vendor'])) {
foreach($locales['vendor'] as $vendor => $data) {
foreach($data as $key => $group) {
foreach($group as $locale => $lang) {
if (isset($locales['vendor'])) {
foreach ($locales['vendor'] as $vendor => $data) {
foreach ($data as $key => $group) {
foreach ($group as $locale => $lang) {
$locales[$key]['vendor'][$vendor][$locale] = $lang;
}
}
Expand Down

0 comments on commit 13ec531

Please sign in to comment.