Skip to content

Commit fb05056

Browse files
authored
Merge pull request #172 from coderello/aliases
Implemented aliases
2 parents bd31df1 + 9f2efbb commit fb05056

File tree

3 files changed

+154
-30
lines changed

3 files changed

+154
-30
lines changed

README.md

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,56 @@ composer require coderello/laravel-nova-lang
1212

1313
## Usage
1414
### Publish Command
15-
Publish translations for one language:
16-
```bash
17-
php artisan nova-lang:publish de
18-
```
15+
* Publish translations for one language:
16+
```bash
17+
php artisan nova-lang:publish de
18+
```
1919

20-
Publish translations for multiple languages:
21-
```bash
22-
php artisan nova-lang:publish de,ru
23-
```
20+
* Publish translations for multiple languages (comma-separated):
21+
```bash
22+
php artisan nova-lang:publish de,ru
23+
```
2424

25-
Publish translations for all languages:
26-
```bash
27-
php artisan nova-lang:publish --all
28-
```
25+
* Publish translations for all available languages:
26+
```bash
27+
php artisan nova-lang:publish --all
28+
```
2929

30-
Publish translations and override existing files:
31-
```bash
32-
php artisan nova-lang:publish de,ru --force
33-
```
30+
* Publish translations and override existing files:
31+
```bash
32+
php artisan nova-lang:publish de,ru --force
33+
```
34+
35+
#### Aliases
36+
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.
37+
38+
* Publish translations for one language with an alias, using the simple format `{alias}`:
39+
```bash
40+
php artisan nova-lang:publish de --alias=de-DE
41+
```
42+
This will publish the file `de-DE.json`.
43+
44+
* Publish translations for multiple languages with multiple aliases, using the format `{locale}:{alias}` (comma-separated):
45+
```bash
46+
php artisan nova-lang:publish de,ru,fr --alias=de:de-DE,ru:ru-RU
47+
```
48+
This will publish the files `de-DE.json`, `ru-RU.json` and `fr.json` (no alias).
49+
50+
* Aliases can also be used with the `--all` flag:
51+
52+
```bash
53+
php artisan nova-lang:publish --all --alias=es:es-ES
54+
```
55+
You do not need to supply an alias for every locale that is to be published, only those that you wish to override.
56+
57+
* Here are some example aliases for common use cases:
58+
59+
* Use Chinese with scripts instead of regions: `zh-CN:zh-Hans,zh-TW:zh-Hant`
60+
* Default to Brazilian Portuguese over European: `pt:pt-PT,pt-BR:pt`
61+
* Default to Serbian in Latin script over Cyrillic: `sr-Latn:sr,sr:sr-Cyrl`
62+
63+
64+
* 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.
3465

3566
### Development Commands (debug mode only)
3667

@@ -42,12 +73,12 @@ This command is to assist contributors to find any untranslated keys for their c
4273

4374
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.
4475

45-
Output missing translation keys for one or more languages:
76+
* Output missing translation keys for one or more languages:
4677
```bash
4778
php artisan nova-lang:missing de,ru
4879
```
4980

50-
Output missing translation keys for all languages:
81+
* Output missing translation keys for all languages:
5182
```bash
5283
php artisan nova-lang:missing --all
5384
```
@@ -58,7 +89,7 @@ This command is to assist maintainers to update the completeness of each languag
5889

5990
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.
6091

61-
Output list of languages, lines translated and contributors:
92+
* Output list of languages, lines translated and contributors:
6293
```bash
6394
php artisan nova-lang:stats
6495
```

src/Commands/NovaLangPublish.php

Lines changed: 102 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
class NovaLangPublish extends Command
1111
{
12+
/**
13+
* Possible locale separators.
14+
* @var string
15+
*/
16+
const SEPARATORS = '-‑_';
17+
1218
/**
1319
* The name and signature of the console command.
1420
*
@@ -17,6 +23,8 @@ class NovaLangPublish extends Command
1723
protected $signature = 'nova-lang:publish
1824
{locales? : Comma-separated list of languages}
1925
{--all : Publish all languages}
26+
{--alias= : Publish files using a different filename for certain locales, in the format "locale:alias,..."}
27+
{--U|underscore : Use underscore instead of dash as locale separator }
2028
{--force : Override existing files}';
2129

2230
/**
@@ -59,23 +67,33 @@ public function handle()
5967
return;
6068
}
6169

62-
$requestedLocales->each(function (string $locale) use ($availableLocales) {
70+
$requestedLocales->each(function (string $alias, string $locale) use ($availableLocales) {
6371

64-
if ($locale == 'en' && $this->isForce()) {
65-
if (!$this->confirm(sprintf('Are you sure you want to republish translations for [en] locale? This will overwrite the latest file from laravel/nova.'))) {
72+
if ($alias == 'en' && $this->isForce()) {
73+
if (!$this->confirm(sprintf('Are you sure you want to publish translations for [en] locale? This will overwrite the file from laravel/nova.'))) {
6674
return;
6775
}
6876
}
6977

7078
if (! $availableLocales->contains($locale)) {
71-
$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));
79+
$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));
7280

7381
return;
7482
}
7583

84+
$asAlias = '';
85+
86+
if ($this->option('underscore')) {
87+
$alias = $this->fixSeparators($alias, '_');
88+
}
89+
90+
if ($alias !== $locale) {
91+
$asAlias = sprintf(' as [%s]', $alias);
92+
}
93+
7694
$inputDirectory = $this->directoryFrom().'/'.$locale;
7795

78-
$outputDirectory = $this->directoryTo().'/'.$locale;
96+
$outputDirectory = $this->directoryTo().'/'.$alias;
7997

8098
$inputFile = $inputDirectory.'.json';
8199

@@ -84,7 +102,7 @@ public function handle()
84102
if (($this->filesystem->exists($outputDirectory)
85103
|| $this->filesystem->exists($outputFile))
86104
&& ! $this->isForce()) {
87-
$this->error(sprintf('Translations for [%s] locale already exist.', $locale));
105+
$this->warn(sprintf('Translations for [%s] locale already exist%s. Use --force to overwrite.', $locale, $asAlias));
88106

89107
return;
90108
}
@@ -95,17 +113,37 @@ public function handle()
95113

96114
$this->filesystem->copy($inputFile, $outputFile);
97115

98-
$this->info(sprintf('Translations for [%s] locale have been published successfully.', $locale));
116+
$this->info(sprintf('Translations for [%s] locale have been published successfully%s.', $locale, $asAlias));
99117
});
100118
}
101119

102120
protected function getRequestedLocales(): Collection
103121
{
104122
if ($this->isAll()) {
105-
return $this->getAvailableLocales();
123+
$locales = $this->getAvailableLocales();
124+
}
125+
else {
126+
$locales = $this->fixSeparators($this->argument('locales'));
127+
$locales = collect(explode(',', $locales))->filter();
128+
}
129+
130+
$aliases = $this->getLocaleAliases($locales->count() == 1 ? $locales->first() : false);
131+
132+
$locales = $locales->mapWithKeys(function (string $locale, string $alias) use (&$aliases) {
133+
$alias = $aliases->pull($locale, $locale);
134+
135+
return [$locale => $alias];
136+
});
137+
138+
if ($aliases->count()) {
139+
$aliases = $aliases->map(function (string $locale, string $alias) {
140+
return "$alias:$locale";
141+
})->join(',');
142+
143+
$this->info(sprintf('Aliases [%s] were not used by the selected locales.', $aliases));
106144
}
107145

108-
return collect(explode(',', $this->argument('locales')))->filter();
146+
return $locales;
109147
}
110148

111149
protected function getAvailableLocales(): Collection
@@ -123,6 +161,61 @@ protected function getAvailableLocales(): Collection
123161
return $localesByDirectories->intersect($localesByFiles)->values();
124162
}
125163

164+
protected function getLocaleAliases($single = false): Collection
165+
{
166+
$aliases = collect();
167+
168+
$input = $this->option('alias');
169+
170+
if ($input) {
171+
172+
$inputs = explode(',', $input);
173+
174+
if (strpos($input, ':') === false) {
175+
if ($single && count($inputs) == 1) {
176+
return collect([$single => $input]);
177+
}
178+
179+
$this->error('If publishing more than one locale, the aliases must be in the format "locale:alias,...".');
180+
exit;
181+
}
182+
elseif (substr_count($input, ':') < count($inputs)) {
183+
if ($single) {
184+
$this->error('If publishing only one locale with a simple alias, only one alias should be passed.');
185+
}
186+
else {
187+
$this->error('If publishing more than one locale, the aliases must be in the format "locale:alias,...".');
188+
}
189+
exit;
190+
}
191+
192+
foreach ($inputs as $input) {
193+
@list($locale, $alias) = explode(':', $input);
194+
195+
if (empty($alias) || empty($locale)) {
196+
$this->error(sprintf('Alias [%s] is not valid.', $input));
197+
exit;
198+
}
199+
200+
if ($aliases->has($locale)) {
201+
$this->warn(sprintf('Alias for [%s] locale was declared more than once and will be overwritten by the last value.', $locale));
202+
}
203+
204+
205+
$locale = $this->fixSeparators($locale);
206+
207+
$aliases->put($locale, $alias);
208+
}
209+
}
210+
211+
return $aliases;
212+
}
213+
214+
protected function fixSeparators(string $locale, string $separator = '-')
215+
{
216+
return preg_replace('/['.static::SEPARATORS.']+/', $separator, $locale);
217+
}
218+
126219
protected function isForce(): bool
127220
{
128221
return $this->option('force');

src/Commands/NovaLangStats.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ public function handle()
9494

9595
$localeStat = $contributors->get($locale, [
9696
'name' => class_exists('Locale') ? \Locale::getDisplayName($locale) : $locale,
97-
'complete' => 0,
97+
'complete' => null,
9898
'contributors' => [],
9999
]);
100100

101101
$complete = $sourceCount - count($missingKeys) - count($missingPhpKeys);
102102

103-
if ($complete > 0) {
103+
if (!is_null($complete) && $complete > 0) {
104104

105105
if ($blameContributors = $blame->get($locale)) {
106106
foreach ($blameContributors as $contributor => $lines) {

0 commit comments

Comments
 (0)