From c1a82d4f50033fc57e0b516cc243605efc4d485d Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Tue, 24 Sep 2019 17:44:33 +0100 Subject: [PATCH 01/15] fix contributors missing when language first added --- src/Commands/NovaLangStats.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/NovaLangStats.php b/src/Commands/NovaLangStats.php index ae063be..b05cb1a 100644 --- a/src/Commands/NovaLangStats.php +++ b/src/Commands/NovaLangStats.php @@ -94,13 +94,13 @@ public function handle() $localeStat = $contributors->get($locale, [ 'name' => class_exists('Locale') ? \Locale::getDisplayName($locale) : $locale, - 'complete' => 0, + 'complete' => null, 'contributors' => [], ]); $complete = $sourceCount - count($missingKeys) - count($missingPhpKeys); - if ($complete > 0) { + if (!is_null($complete) && $complete > 0) { if ($blameContributors = $blame->get($locale)) { foreach ($blameContributors as $contributor => $lines) { From b24e0c799bc9b859017359df8bf4194a956de985 Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Tue, 24 Sep 2019 22:58:44 +0100 Subject: [PATCH 02/15] implement aliases --- src/Commands/NovaLangPublish.php | 73 ++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 8 deletions(-) diff --git a/src/Commands/NovaLangPublish.php b/src/Commands/NovaLangPublish.php index 0f18ae3..3063b80 100644 --- a/src/Commands/NovaLangPublish.php +++ b/src/Commands/NovaLangPublish.php @@ -17,6 +17,7 @@ class NovaLangPublish extends Command protected $signature = 'nova-lang:publish {locales? : Comma-separated list of languages} {--all : Publish all languages} + {--alias= : Publish files with a different filename for the locale} {--force : Override existing files}'; /** @@ -59,23 +60,29 @@ public function handle() return; } - $requestedLocales->each(function (string $locale) use ($availableLocales) { + $requestedLocales->each(function (string $alias, string $locale) use ($availableLocales) { - if ($locale == 'en' && $this->isForce()) { + if ($alias == 'en' && $this->isForce()) { if (!$this->confirm(sprintf('Are you sure you want to republish translations for [en] locale? This will overwrite the latest file from laravel/nova.'))) { return; } } if (! $availableLocales->contains($locale)) { - $this->error(sprintf('Unfortunately, translations for [%s] locale don\'t exist. Feel free to send a PR to add them and help other people :)', $locale)); + $this->warn(sprintf('Unfortunately, translations for [%s] locale don\'t exist. Feel free to send a PR to add them and help other people.', $locale)); return; } + $asAlias = ''; + + if ($alias !== $locale) { + $asAlias = sprintf(' as [%s]', $alias); + } + $inputDirectory = $this->directoryFrom().'/'.$locale; - $outputDirectory = $this->directoryTo().'/'.$locale; + $outputDirectory = $this->directoryTo().'/'.$alias; $inputFile = $inputDirectory.'.json'; @@ -84,7 +91,7 @@ public function handle() if (($this->filesystem->exists($outputDirectory) || $this->filesystem->exists($outputFile)) && ! $this->isForce()) { - $this->error(sprintf('Translations for [%s] locale already exist.', $locale)); + $this->warn(sprintf('Translations for [%s] locale already exist%s. Use --force to overwrite.', $locale, $asAlias)); return; } @@ -95,17 +102,36 @@ public function handle() $this->filesystem->copy($inputFile, $outputFile); - $this->info(sprintf('Translations for [%s] locale have been published successfully.', $locale)); + $this->info(sprintf('Translations for [%s] locale have been published successfully%s.', $locale, $asAlias)); }); } protected function getRequestedLocales(): Collection { if ($this->isAll()) { - return $this->getAvailableLocales(); + $locales = $this->getAvailableLocales(); + } + else { + $locales = collect(explode(',', $this->argument('locales')))->filter(); } - return collect(explode(',', $this->argument('locales')))->filter(); + $aliases = $this->getLocaleAliases($locales->count() == 1 ? $locales->first() : false); + + $locales = $locales->mapWithKeys(function (string $locale, string $alias) use (&$aliases) { + $alias = $aliases->pull($locale, $locale); + + return [$locale => $alias]; + }); + + if ($aliases->count()) { + $aliases = $aliases->map(function (string $locale, string $alias) { + return "$alias:$locale"; + })->join(','); + + $this->info(sprintf('Aliases [%s] were not used by the selected locales.', $aliases)); + } + + return $locales; } protected function getAvailableLocales(): Collection @@ -123,6 +149,37 @@ protected function getAvailableLocales(): Collection return $localesByDirectories->intersect($localesByFiles)->values(); } + protected function getLocaleAliases($single = false): Collection + { + $aliases = collect(); + + if ($input = $this->option('alias')) { + + $inputs = explode(',', $input); + + if (strpos($input, ':') === false && !$single) { + $this->error('If publishing more than one locale, the aliases must be in the format "locale:alias,...".'); + exit; + } + elseif ($single && count($inputs) > 1 && (substr_count($input, ':') < count($inputs))) { + $this->error('If publishing only one locale with a simple alias, only one alias should be passed.'); + exit; + } + + foreach ($inputs as $alias) { + @list($locale, $alias) = explode(':', $alias); + + if (is_null($alias) && $single) { + return collect([$single => $locale]); + } + + $aliases->put($locale, $alias); + } + } + + return $aliases; + } + protected function isForce(): bool { return $this->option('force'); From 30e53f03a1edad1252687d68bac8dc9068311122 Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Tue, 24 Sep 2019 23:10:41 +0100 Subject: [PATCH 03/15] checks to alias syntax --- src/Commands/NovaLangPublish.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Commands/NovaLangPublish.php b/src/Commands/NovaLangPublish.php index 3063b80..3921234 100644 --- a/src/Commands/NovaLangPublish.php +++ b/src/Commands/NovaLangPublish.php @@ -157,20 +157,34 @@ protected function getLocaleAliases($single = false): Collection $inputs = explode(',', $input); - if (strpos($input, ':') === false && !$single) { + if (strpos($input, ':') === false) { + if ($single && count($inputs) == 1) { + return collect([$single => $input]); + } + $this->error('If publishing more than one locale, the aliases must be in the format "locale:alias,...".'); exit; } - elseif ($single && count($inputs) > 1 && (substr_count($input, ':') < count($inputs))) { - $this->error('If publishing only one locale with a simple alias, only one alias should be passed.'); + elseif (substr_count($input, ':') < count($inputs)) { + if ($single) { + $this->error('If publishing only one locale with a simple alias, only one alias should be passed.'); + } + else { + $this->error('If publishing more than one locale, the aliases must be in the format "locale:alias,...".'); + } exit; } - foreach ($inputs as $alias) { - @list($locale, $alias) = explode(':', $alias); + foreach ($inputs as $input) { + @list($locale, $alias) = explode(':', $input); + + if (empty($alias) || empty($locale)) { + $this->error(sprintf('Alias [%s] is not valid.', $input)); + exit; + } - if (is_null($alias) && $single) { - return collect([$single => $locale]); + if ($aliases->has($locale)) { + $this->warn(sprintf('Alias for [%s] locale was declared more than once and will be overwritten by the last value.', $locale)); } $aliases->put($locale, $alias); From 9a698b6811f7263a26a9c03cdf1256207cf25274 Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Tue, 24 Sep 2019 23:22:06 +0100 Subject: [PATCH 04/15] add predefined aliases for zh-Hans/Hant and pt-PT/BR --- src/Commands/NovaLangPublish.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Commands/NovaLangPublish.php b/src/Commands/NovaLangPublish.php index 3921234..d0010f0 100644 --- a/src/Commands/NovaLangPublish.php +++ b/src/Commands/NovaLangPublish.php @@ -18,6 +18,8 @@ class NovaLangPublish extends Command {locales? : Comma-separated list of languages} {--all : Publish all languages} {--alias= : Publish files with a different filename for the locale} + {--zhHans : Publish Chinese translations preferring \'script\' or \'region\' } + {--ptBR : Publish Portuguese translations preferring \'BR\' or \'PT\' } {--force : Override existing files}'; /** @@ -153,7 +155,13 @@ protected function getLocaleAliases($single = false): Collection { $aliases = collect(); - if ($input = $this->option('alias')) { + $input = implode(',', array_filter([ + $this->option('alias'), + $this->option('ptBR') ? 'pt:pt-PT,pt-BR:pt' : null, + $this->option('zhHans') ? 'zh-CN:zh-Hans,zh-TW:zh-Hant' : null, + ])); + + if ($input) { $inputs = explode(',', $input); From e2f63efa18f4a01de4e069f39b2021be36d6698e Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Tue, 24 Sep 2019 23:28:30 +0100 Subject: [PATCH 05/15] update option help text --- src/Commands/NovaLangPublish.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Commands/NovaLangPublish.php b/src/Commands/NovaLangPublish.php index d0010f0..19cc447 100644 --- a/src/Commands/NovaLangPublish.php +++ b/src/Commands/NovaLangPublish.php @@ -17,9 +17,9 @@ class NovaLangPublish extends Command protected $signature = 'nova-lang:publish {locales? : Comma-separated list of languages} {--all : Publish all languages} - {--alias= : Publish files with a different filename for the locale} - {--zhHans : Publish Chinese translations preferring \'script\' or \'region\' } - {--ptBR : Publish Portuguese translations preferring \'BR\' or \'PT\' } + {--alias= : Publish files using a different filename for certain locales, in the format "locale:alias,..."} + {--zhHans : Publish Chinese translations as "zh-Hans/Hant" instead of "zh-CN/TW" } + {--ptBR : Publish Portuguese translations prioritising "pt-BR" as "pt" over "pt-PT" } {--force : Override existing files}'; /** From f5cbeb2538a857ebebeeae01176eab97b369fbf8 Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Tue, 24 Sep 2019 23:31:36 +0100 Subject: [PATCH 06/15] refine confirm message --- src/Commands/NovaLangPublish.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/NovaLangPublish.php b/src/Commands/NovaLangPublish.php index 19cc447..036701d 100644 --- a/src/Commands/NovaLangPublish.php +++ b/src/Commands/NovaLangPublish.php @@ -65,7 +65,7 @@ public function handle() $requestedLocales->each(function (string $alias, string $locale) use ($availableLocales) { if ($alias == 'en' && $this->isForce()) { - if (!$this->confirm(sprintf('Are you sure you want to republish translations for [en] locale? This will overwrite the latest file from laravel/nova.'))) { + if (!$this->confirm(sprintf('Are you sure you want to publish translations for [en] locale? This will overwrite the file from laravel/nova.'))) { return; } } From 2a803dd8fd13203d7921ecec5cdfac907eeb23c4 Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Wed, 25 Sep 2019 00:03:19 +0100 Subject: [PATCH 07/15] add serbian aliases and underscore flag --- src/Commands/NovaLangPublish.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Commands/NovaLangPublish.php b/src/Commands/NovaLangPublish.php index 036701d..95af989 100644 --- a/src/Commands/NovaLangPublish.php +++ b/src/Commands/NovaLangPublish.php @@ -19,7 +19,10 @@ class NovaLangPublish extends Command {--all : Publish all languages} {--alias= : Publish files using a different filename for certain locales, in the format "locale:alias,..."} {--zhHans : Publish Chinese translations as "zh-Hans/Hant" instead of "zh-CN/TW" } - {--ptBR : Publish Portuguese translations prioritising "pt-BR" as "pt" over "pt-PT" } + {--ptBR : Publish Portuguese translations prioritizing "pt-BR" as "pt" over "pt-PT" } + {--srCyrl : Publish Serbian Cyrillic translations as "sr-Cyrl" instead of "sr" } + {--srLatn : Publish Serbian translations prioritizing "sr-Latn" as "sr" instead of "sr-Cyrl" } + {--U|underscore : Use underscore instead of dash as locale separator } {--force : Override existing files}'; /** @@ -78,6 +81,10 @@ public function handle() $asAlias = ''; + if ($this->option('underscore')) { + $alias = str_replace('-', '_', $alias); + } + if ($alias !== $locale) { $asAlias = sprintf(' as [%s]', $alias); } @@ -159,8 +166,15 @@ protected function getLocaleAliases($single = false): Collection $this->option('alias'), $this->option('ptBR') ? 'pt:pt-PT,pt-BR:pt' : null, $this->option('zhHans') ? 'zh-CN:zh-Hans,zh-TW:zh-Hant' : null, + $this->option('srCyrl') ? 'sr:sr-Cyrl' : null, + $this->option('srLatn') ? 'sr-Latn:sr,sr:sr-Cyrl' : null, ])); + if ($this->option('srCyrl') && $this->option('srLatn')) { + $this->error('Options --srCyrl and --srLatn must not be used together.'); + exit; + } + if ($input) { $inputs = explode(',', $input); From c3b5fd5accb05825abd3e8874dd6808348da3487 Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Wed, 25 Sep 2019 00:47:37 +0100 Subject: [PATCH 08/15] remove srCyrl option --- src/Commands/NovaLangPublish.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Commands/NovaLangPublish.php b/src/Commands/NovaLangPublish.php index 95af989..b6b0dd9 100644 --- a/src/Commands/NovaLangPublish.php +++ b/src/Commands/NovaLangPublish.php @@ -18,9 +18,8 @@ class NovaLangPublish extends Command {locales? : Comma-separated list of languages} {--all : Publish all languages} {--alias= : Publish files using a different filename for certain locales, in the format "locale:alias,..."} - {--zhHans : Publish Chinese translations as "zh-Hans/Hant" instead of "zh-CN/TW" } + {--zhHan : Publish Chinese translations as "zh-Hans/Hant" instead of "zh-CN/TW" } {--ptBR : Publish Portuguese translations prioritizing "pt-BR" as "pt" over "pt-PT" } - {--srCyrl : Publish Serbian Cyrillic translations as "sr-Cyrl" instead of "sr" } {--srLatn : Publish Serbian translations prioritizing "sr-Latn" as "sr" instead of "sr-Cyrl" } {--U|underscore : Use underscore instead of dash as locale separator } {--force : Override existing files}'; @@ -165,16 +164,10 @@ protected function getLocaleAliases($single = false): Collection $input = implode(',', array_filter([ $this->option('alias'), $this->option('ptBR') ? 'pt:pt-PT,pt-BR:pt' : null, - $this->option('zhHans') ? 'zh-CN:zh-Hans,zh-TW:zh-Hant' : null, - $this->option('srCyrl') ? 'sr:sr-Cyrl' : null, + $this->option('zhHan') ? 'zh-CN:zh-Hans,zh-TW:zh-Hant' : null, $this->option('srLatn') ? 'sr-Latn:sr,sr:sr-Cyrl' : null, ])); - if ($this->option('srCyrl') && $this->option('srLatn')) { - $this->error('Options --srCyrl and --srLatn must not be used together.'); - exit; - } - if ($input) { $inputs = explode(',', $input); From f356b4d0d4e185d50281b8a7447074a744be687e Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Wed, 25 Sep 2019 00:47:44 +0100 Subject: [PATCH 09/15] readme for aliases --- README.md | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3c2fcf4..5fc9c9f 100644 --- a/README.md +++ b/README.md @@ -12,26 +12,59 @@ composer require coderello/laravel-nova-lang ## Usage ### Publish Command -Publish translations for one language: +* Publish translations for one language: ```bash php artisan nova-lang:publish de ``` -Publish translations for multiple languages: +* Publish translations for multiple languages (comma-separated): ```bash php artisan nova-lang:publish de,ru ``` -Publish translations for all languages: +* Publish translations for all available languages: ```bash php artisan nova-lang:publish --all ``` -Publish translations and override existing files: +* Publish translations and override existing files: ```bash php artisan nova-lang:publish de,ru --force ``` +#### Aliases +The language codes chosen for the files in this repository may not match the preferences for your project. You can use the `--alias` option to publish locales using a different filename. + +* Publish translations for one language with an alias, using the simple format `{alias}`: +```bash +php artisan nova-lang:publish de --alias=de-DE +``` + This will publish the file `de-DE.json`. + + +* Publish translations for multiple languages with multiple aliases, using the format `{locale}:{alias}` (comma-separated): +```bash +php artisan nova-lang:publish de,ru,fr --alias=de:de-DE,ru:ru-RU +``` + This will publish the files `de-DE.json`, `ru-RU.json` and `fr.json` (no alias). + +* Aliases can also be used with the `--all` flag: + + ```bash + php artisan nova-lang:publish --all --alias=es:es-ES + ``` + You do not need to supply an alias for every locale that is to be published, only those that you wish to override. + +* There are also some predefined option flags (case-sensitive) for common use cases: +| Option | Explanation | Affected Languages | When Enabled | Default | +| --- | --- | --- | --- | --- | +| `‑‑zhHan` | Use Chinese scripts instead of regions. | Chinese (Simplified)
Chinese (Traditional) | `zh‑Hans`
`zh‑Hant` | `zh‑CN`
`zh‑TW` | +| `‑‑ptBR` | Prefer Brazilian Portuguese as default. | Portuguese (Brazil)
Portuguese (Portugal) | `pt`
`pt‑PT` | `pt‑BR`
`pt` | +| `‑‑srLatn` | Prefer Serbian in Latin script as default. | Serbian (Cyrillic)
Serbian (Latin) | `sr‑Cyrl`
`sr` | `sr`
`sr‑Latn` | +| `‑U` or `‑‑underscore` | Use underscore separator instead of hyphen. | _(any)_ | _e.g._ `zh_CN` | _e.g._ `zh‑CN` | + + These can all be used alone or in combination with each other and the `--alias` option. + ### Development Commands (debug mode only) You must have the `app.debug` config option set to true for these commands to be available: @@ -42,12 +75,12 @@ This command is to assist contributors to find any untranslated keys for their c A stub JSON file will be created at `storage_path('app/nova-lang/missing/{locale}.json')`. You can copy those keys into the `resources/lang/{locale}.json` language file in your own fork of the repository, translate them and create a pull request. -Output missing translation keys for one or more languages: +* Output missing translation keys for one or more languages: ```bash php artisan nova-lang:missing de,ru ``` -Output missing translation keys for all languages: +* Output missing translation keys for all languages: ```bash php artisan nova-lang:missing --all ``` @@ -58,7 +91,7 @@ This command is to assist maintainers to update the completeness of each languag A `README.excerpt.md` and `contributors.json` file will be created at `storage_path('app/nova-lang')`. You can copy those files into your own fork of the repository and create a pull request. -Output list of languages, lines translated and contributors: +* Output list of languages, lines translated and contributors: ```bash php artisan nova-lang:stats ``` From 84278b71f981f18bbc0522a6f79bb83dc479de00 Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Wed, 25 Sep 2019 00:50:04 +0100 Subject: [PATCH 10/15] fix table formatting --- README.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5fc9c9f..4dd0065 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ php artisan nova-lang:publish de,ru --force ``` #### Aliases -The language codes chosen for the files in this repository may not match the preferences for your project. You can use the `--alias` option to publish locales using a different filename. +The language codes chosen for the files in this repository may not match the preferences for your project. You can use the `‑‑alias` option to publish locales using a different filename. * Publish translations for one language with an alias, using the simple format `{alias}`: ```bash @@ -56,12 +56,13 @@ php artisan nova-lang:publish de,ru,fr --alias=de:de-DE,ru:ru-RU You do not need to supply an alias for every locale that is to be published, only those that you wish to override. * There are also some predefined option flags (case-sensitive) for common use cases: -| Option | Explanation | Affected Languages | When Enabled | Default | -| --- | --- | --- | --- | --- | -| `‑‑zhHan` | Use Chinese scripts instead of regions. | Chinese (Simplified)
Chinese (Traditional) | `zh‑Hans`
`zh‑Hant` | `zh‑CN`
`zh‑TW` | -| `‑‑ptBR` | Prefer Brazilian Portuguese as default. | Portuguese (Brazil)
Portuguese (Portugal) | `pt`
`pt‑PT` | `pt‑BR`
`pt` | -| `‑‑srLatn` | Prefer Serbian in Latin script as default. | Serbian (Cyrillic)
Serbian (Latin) | `sr‑Cyrl`
`sr` | `sr`
`sr‑Latn` | -| `‑U` or `‑‑underscore` | Use underscore separator instead of hyphen. | _(any)_ | _e.g._ `zh_CN` | _e.g._ `zh‑CN` | + + | Option | Explanation | Affected Languages | When Enabled | Default | + | --- | --- | --- | --- | --- | + | `‑‑zhHan` | Use Chinese scripts instead of regions. | Chinese (Simplified)
Chinese (Traditional) | `zh‑Hans`
`zh‑Hant` | `zh‑CN`
`zh‑TW` | + | `‑‑ptBR` | Prefer Brazilian Portuguese as default. | Portuguese (Brazil)
Portuguese (Portugal) | `pt`
`pt‑PT` | `pt‑BR`
`pt` | + | `‑‑srLatn` | Prefer Serbian in Latin script as default. | Serbian (Cyrillic)
Serbian (Latin) | `sr‑Cyrl`
`sr` | `sr`
`sr‑Latn` | + | `‑U` or `‑‑underscore` | Use underscore separator instead of hyphen. | _(any)_ | _e.g._ `zh_CN` | _e.g._ `zh‑CN` | These can all be used alone or in combination with each other and the `--alias` option. From d86844a971ecb74f143ab13f300a6dedcab58212 Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Wed, 25 Sep 2019 00:53:49 +0100 Subject: [PATCH 11/15] table flow --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4dd0065..51d88e6 100644 --- a/README.md +++ b/README.md @@ -59,10 +59,10 @@ php artisan nova-lang:publish de,ru,fr --alias=de:de-DE,ru:ru-RU | Option | Explanation | Affected Languages | When Enabled | Default | | --- | --- | --- | --- | --- | - | `‑‑zhHan` | Use Chinese scripts instead of regions. | Chinese (Simplified)
Chinese (Traditional) | `zh‑Hans`
`zh‑Hant` | `zh‑CN`
`zh‑TW` | - | `‑‑ptBR` | Prefer Brazilian Portuguese as default. | Portuguese (Brazil)
Portuguese (Portugal) | `pt`
`pt‑PT` | `pt‑BR`
`pt` | - | `‑‑srLatn` | Prefer Serbian in Latin script as default. | Serbian (Cyrillic)
Serbian (Latin) | `sr‑Cyrl`
`sr` | `sr`
`sr‑Latn` | - | `‑U` or `‑‑underscore` | Use underscore separator instead of hyphen. | _(any)_ | _e.g._ `zh_CN` | _e.g._ `zh‑CN` | + | `‑‑zhHan` | Use Chinese scripts instead of regions. | Chinese (Simplified)
Chinese (Traditional) | `zh‑Hans`
`zh‑Hant` | `zh‑CN`
`zh‑TW` | + | `‑‑ptBR` | Prefer Brazilian Portuguese. | Portuguese (Brazil)
Portuguese (Portugal) | `pt`
`pt‑PT` | `pt‑BR`
`pt` | + | `‑‑srLatn` | Prefer Serbian in Latin script. | Serbian (Cyrillic)
Serbian (Latin) | `sr‑Cyrl`
`sr` | `sr`
`sr‑Latn` | + | `‑U` or
`‑‑underscore` | Use underscore separator. | _(any)_ | _e.g._ `zh_CN` | _e.g._ `zh‑CN` | These can all be used alone or in combination with each other and the `--alias` option. From 885e4fe1d92e6a978e2dda769b7865cdbf49729d Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Sat, 5 Oct 2019 17:24:57 +0100 Subject: [PATCH 12/15] rollback predefined aliases --- README.md | 14 ++++++-------- src/Commands/NovaLangPublish.php | 10 +--------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 51d88e6..a72918b 100644 --- a/README.md +++ b/README.md @@ -55,16 +55,14 @@ php artisan nova-lang:publish de,ru,fr --alias=de:de-DE,ru:ru-RU ``` You do not need to supply an alias for every locale that is to be published, only those that you wish to override. -* There are also some predefined option flags (case-sensitive) for common use cases: +* Here are some example aliases for common use cases: - | Option | Explanation | Affected Languages | When Enabled | Default | - | --- | --- | --- | --- | --- | - | `‑‑zhHan` | Use Chinese scripts instead of regions. | Chinese (Simplified)
Chinese (Traditional) | `zh‑Hans`
`zh‑Hant` | `zh‑CN`
`zh‑TW` | - | `‑‑ptBR` | Prefer Brazilian Portuguese. | Portuguese (Brazil)
Portuguese (Portugal) | `pt`
`pt‑PT` | `pt‑BR`
`pt` | - | `‑‑srLatn` | Prefer Serbian in Latin script. | Serbian (Cyrillic)
Serbian (Latin) | `sr‑Cyrl`
`sr` | `sr`
`sr‑Latn` | - | `‑U` or
`‑‑underscore` | Use underscore separator. | _(any)_ | _e.g._ `zh_CN` | _e.g._ `zh‑CN` | + * `zh-CN:zh-Hans,zh-TW:zh-Hant` (Use Chinese scripts instead of regions.) + * `pt:pt-PT,pt-BR:pt` (Brazilian Portuguese as default over European.) + * `sr-Latn:sr,sr:sr-Cyrl` (Serbian in Latin script as default over Cyrillic.) - These can all be used alone or in combination with each other and the `--alias` option. + +* There is also a `‑U` or`‑‑underscore` switch to publish locales with an underscore separator instead of a hyphen. This can be used in combination with aliases. ### Development Commands (debug mode only) diff --git a/src/Commands/NovaLangPublish.php b/src/Commands/NovaLangPublish.php index b6b0dd9..017e932 100644 --- a/src/Commands/NovaLangPublish.php +++ b/src/Commands/NovaLangPublish.php @@ -18,9 +18,6 @@ class NovaLangPublish extends Command {locales? : Comma-separated list of languages} {--all : Publish all languages} {--alias= : Publish files using a different filename for certain locales, in the format "locale:alias,..."} - {--zhHan : Publish Chinese translations as "zh-Hans/Hant" instead of "zh-CN/TW" } - {--ptBR : Publish Portuguese translations prioritizing "pt-BR" as "pt" over "pt-PT" } - {--srLatn : Publish Serbian translations prioritizing "sr-Latn" as "sr" instead of "sr-Cyrl" } {--U|underscore : Use underscore instead of dash as locale separator } {--force : Override existing files}'; @@ -161,12 +158,7 @@ protected function getLocaleAliases($single = false): Collection { $aliases = collect(); - $input = implode(',', array_filter([ - $this->option('alias'), - $this->option('ptBR') ? 'pt:pt-PT,pt-BR:pt' : null, - $this->option('zhHan') ? 'zh-CN:zh-Hans,zh-TW:zh-Hant' : null, - $this->option('srLatn') ? 'sr-Latn:sr,sr:sr-Cyrl' : null, - ])); + $input = $this->option('alias'); if ($input) { From dd84cedd15b0c45625cd927055a020e376f3f574 Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Sat, 5 Oct 2019 17:29:20 +0100 Subject: [PATCH 13/15] fix separators --- src/Commands/NovaLangPublish.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Commands/NovaLangPublish.php b/src/Commands/NovaLangPublish.php index 017e932..fb5885f 100644 --- a/src/Commands/NovaLangPublish.php +++ b/src/Commands/NovaLangPublish.php @@ -9,6 +9,12 @@ class NovaLangPublish extends Command { + /** + * Possible locale separators. + * @var string + */ + const SEPARATORS = '-‑_'; + /** * The name and signature of the console command. * @@ -117,7 +123,8 @@ protected function getRequestedLocales(): Collection $locales = $this->getAvailableLocales(); } else { - $locales = collect(explode(',', $this->argument('locales')))->filter(); + $locales = $this->fixSeparators($this->argument('locales')); + $locales = collect(explode(',', $locales))->filter(); } $aliases = $this->getLocaleAliases($locales->count() == 1 ? $locales->first() : false); @@ -194,6 +201,9 @@ protected function getLocaleAliases($single = false): Collection $this->warn(sprintf('Alias for [%s] locale was declared more than once and will be overwritten by the last value.', $locale)); } + + $locale = $this->fixSeparators($locale); + $aliases->put($locale, $alias); } } @@ -201,6 +211,11 @@ protected function getLocaleAliases($single = false): Collection return $aliases; } + protected function fixSeparators(string $locale) + { + return preg_replace('/['.static::SEPARATORS.']+/', '-', $locale); + } + protected function isForce(): bool { return $this->option('force'); From b0de79ab8e93a8b6ba52047cebc1e4bf9c7968e0 Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Sat, 5 Oct 2019 17:32:27 +0100 Subject: [PATCH 14/15] fix seps in alias --- src/Commands/NovaLangPublish.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Commands/NovaLangPublish.php b/src/Commands/NovaLangPublish.php index fb5885f..e98f3a5 100644 --- a/src/Commands/NovaLangPublish.php +++ b/src/Commands/NovaLangPublish.php @@ -84,7 +84,7 @@ public function handle() $asAlias = ''; if ($this->option('underscore')) { - $alias = str_replace('-', '_', $alias); + $alias = $this->fixSeparators($alias, '_'); } if ($alias !== $locale) { @@ -211,9 +211,9 @@ protected function getLocaleAliases($single = false): Collection return $aliases; } - protected function fixSeparators(string $locale) + protected function fixSeparators(string $locale, string $separator = '-') { - return preg_replace('/['.static::SEPARATORS.']+/', '-', $locale); + return preg_replace('/['.static::SEPARATORS.']+/', $separator, $locale); } protected function isForce(): bool From 9f2efbb8029cc750ef91be5f7885e17abdaf6336 Mon Sep 17 00:00:00 2001 From: Kit Burton-Senior Date: Sat, 5 Oct 2019 17:37:44 +0100 Subject: [PATCH 15/15] readme indents --- README.md | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index a72918b..b6ac02d 100644 --- a/README.md +++ b/README.md @@ -13,39 +13,38 @@ composer require coderello/laravel-nova-lang ## Usage ### Publish Command * Publish translations for one language: -```bash -php artisan nova-lang:publish de -``` + ```bash + php artisan nova-lang:publish de + ``` * Publish translations for multiple languages (comma-separated): -```bash -php artisan nova-lang:publish de,ru -``` + ```bash + php artisan nova-lang:publish de,ru + ``` * Publish translations for all available languages: -```bash -php artisan nova-lang:publish --all -``` + ```bash + php artisan nova-lang:publish --all + ``` * Publish translations and override existing files: -```bash -php artisan nova-lang:publish de,ru --force -``` + ```bash + php artisan nova-lang:publish de,ru --force + ``` #### Aliases The language codes chosen for the files in this repository may not match the preferences for your project. You can use the `‑‑alias` option to publish locales using a different filename. * Publish translations for one language with an alias, using the simple format `{alias}`: -```bash -php artisan nova-lang:publish de --alias=de-DE -``` + ```bash + php artisan nova-lang:publish de --alias=de-DE + ``` This will publish the file `de-DE.json`. - * Publish translations for multiple languages with multiple aliases, using the format `{locale}:{alias}` (comma-separated): -```bash -php artisan nova-lang:publish de,ru,fr --alias=de:de-DE,ru:ru-RU -``` + ```bash + php artisan nova-lang:publish de,ru,fr --alias=de:de-DE,ru:ru-RU + ``` This will publish the files `de-DE.json`, `ru-RU.json` and `fr.json` (no alias). * Aliases can also be used with the `--all` flag: @@ -57,12 +56,12 @@ php artisan nova-lang:publish de,ru,fr --alias=de:de-DE,ru:ru-RU * Here are some example aliases for common use cases: - * `zh-CN:zh-Hans,zh-TW:zh-Hant` (Use Chinese scripts instead of regions.) - * `pt:pt-PT,pt-BR:pt` (Brazilian Portuguese as default over European.) - * `sr-Latn:sr,sr:sr-Cyrl` (Serbian in Latin script as default over Cyrillic.) + * Use Chinese with scripts instead of regions: `zh-CN:zh-Hans,zh-TW:zh-Hant` + * Default to Brazilian Portuguese over European: `pt:pt-PT,pt-BR:pt` + * Default to Serbian in Latin script over Cyrillic: `sr-Latn:sr,sr:sr-Cyrl` -* There is also a `‑U` or`‑‑underscore` switch to publish locales with an underscore separator instead of a hyphen. This can be used in combination with aliases. +* There is also an `‑‑underscore` or `‑U` switch to publish locales with an underscore separator instead of a hyphen. This can be used in combination with aliases. ### Development Commands (debug mode only)