Skip to content

Commit 331cb76

Browse files
committed
multi target locales support
1 parent 4702f59 commit 331cb76

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ But there is few more useful parameters:
7979

8080
| name of parameter | description | is required? | default value |
8181
|-------------------|-----------------------------------------|--------------|------------------------------------|
82-
| locale | The locale to be exported | NO | default lang of application |
82+
| locale | The locale to be exported. Multiple separated by comma | NO | default lang of application |
8383
| group | The name of translation file to export | NO | \* - all files |
8484
| output | Filename of exported translation files | NO | storage/app/lang-import-export.csv |
8585
| -A / --append | Append name of group to the name of file | NO | empty |
8686
| -X / --excel | Set file encoding (UTF-16) for Excel | NO | UTF-8 |
8787
| -D / --delimiter | Field delimiter | NO | , |
8888
| -E / --enclosure | Field enclosure | NO | " |
89-
89+
| -T / --target-locale | Only missing keys for for this locale are exported | NO | " |
9090
### Import
9191

9292
```

src/Console/ExportToCsvCommand.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class ExportToCsvCommand extends Command
1616
* @var string
1717
*/
1818
protected $signature = 'lang:export
19-
{locale? : The locale to be exported (default - default lang of application).}
19+
{locale? : The locales to be exported. Separated by comma (default - default lang of application).}
2020
{group? : The name of translation file to export (default - all files).}
21-
{output? : Filename of exported translation, :locale is replaced (optional, default - :locale-export.csv).}
21+
{output? : Filename of exported translation, :locale, :target is replaced (optional, default - :locale:target-export.csv).}
2222
{--A|append : Append name of group to the name of file (optional, default - empty).}
2323
{--X|excel : Set file encoding for Excel (optional, default - UTF-8).}
2424
{--D|delimiter=, : Field delimiter (optional, default - ",").}
2525
{--E|enclosure=" : Field enclosure (optional, default - \'"\').}
26-
{--T|target-locale=" : Target language, only missing keys are exported (optional).} ';
26+
{--T|target-locale=" : Target languages, only missing keys are exported. Separated by comma (optional).} ';
2727

2828
/**
2929
* The console command description.
@@ -61,7 +61,7 @@ class ExportToCsvCommand extends Command
6161
public function __construct()
6262
{
6363
parent::__construct();
64-
$this->defaultPath = base_path(':locale-export') . $this->ext;
64+
$this->defaultPath = base_path(':locale:target-export') . $this->ext;
6565
}
6666

6767
/**
@@ -75,13 +75,23 @@ public function handle()
7575

7676
$this->sayItsBeginning();
7777

78-
foreach (explode(',', $this->parameters['locale']) as $locale) {
79-
$translations = $this->getTranslations($locale);
80-
$this->saveTranslations($locale, $translations);
81-
$this->info(strtoupper($locale) . ' Translations saved to: ' . $this->getOutputFileName($locale));
78+
foreach ($this->strToArray($this->parameters['locale']) as $locale) {
79+
foreach ($this->strToArray($this->parameters['target_locale'], [null]) as $target) {
80+
$translations = $this->getTranslations($locale, $target);
81+
$this->saveTranslations($locale, $target, $translations);
82+
$this->info(strtoupper($locale) . strtoupper($target ?: '') . ' Translations saved to: ' . $this->getOutputFileName($locale, $target));
83+
}
8284
}
8385
}
8486

87+
private function strToArray($string, $fallback = [])
88+
{
89+
if (!$string) {
90+
return $fallback;
91+
}
92+
return array_filter(array_map('trim', explode(',', $string)));
93+
}
94+
8595
/**
8696
* Fetch command parameters (arguments and options) and analyze them.
8797
*
@@ -131,14 +141,15 @@ private function sayItsBeginning()
131141
* Get translations from localization files.
132142
*
133143
* @param $locale
144+
* @param null $target
134145
* @return array
135146
*/
136-
private function getTranslations($locale)
147+
private function getTranslations($locale, $target = null)
137148
{
138149
$from = LangListService::loadLangList($locale, $this->parameters['group']);
139-
if ($this->parameters['target_locale']) {
140-
$target = LangListService::loadLangList($this->parameters['target_locale'], $this->parameters['group']);
141-
foreach ($target as $group => $translations) {
150+
if ($target) {
151+
$targetList = LangListService::loadLangList($target, $this->parameters['group']);
152+
foreach ($targetList as $group => $translations) {
142153
foreach ($translations as $key => $v) {
143154
unset($from[$group][$key]);
144155
}
@@ -151,12 +162,13 @@ private function getTranslations($locale)
151162
* Save fetched translations to file.
152163
*
153164
* @param $locale
165+
* @param $target
154166
* @param $translations
155167
* @return void
156168
*/
157-
private function saveTranslations($locale, $translations)
169+
private function saveTranslations($locale, $target, $translations)
158170
{
159-
$output = $this->openFile($locale);
171+
$output = $this->openFile($locale, $target);
160172

161173
$this->saveTranslationsToFile($output, $translations);
162174

@@ -166,11 +178,13 @@ private function saveTranslations($locale, $translations)
166178
/**
167179
* Open specified file (if not possible, open default one).
168180
*
181+
* @param $locale
182+
* @param $target
169183
* @return FilePointerResource
170184
*/
171-
private function openFile($locale)
185+
private function openFile($locale, $target)
172186
{
173-
$fileName = $this->getOutputFileName($locale);
187+
$fileName = $this->getOutputFileName($locale, $target);
174188
if (substr($fileName, -4) != $this->ext) {
175189
$fileName .= $this->ext;
176190
}
@@ -250,12 +264,16 @@ private function adjustToExcel()
250264

251265
/**
252266
* @param $locale
267+
* @param null $target
253268
* @return mixed
254269
*/
255-
private function getOutputFileName($locale)
270+
private function getOutputFileName($locale, $target = null)
256271
{
257272
$fileName = $this->parameters['output'];
258273
$fileName = str_replace(':locale', $locale, $fileName);
274+
if ($target) {
275+
$fileName = str_replace(':target', $target, $fileName);
276+
}
259277
return $fileName;
260278
}
261279

0 commit comments

Comments
 (0)