From d7242fc87bdfffed4f9ba87005e8e75c1557df01 Mon Sep 17 00:00:00 2001 From: Sven Buijsrogge Date: Sat, 12 May 2018 11:35:21 +0200 Subject: [PATCH 1/5] Create a locales.json file to store all the found locales --- src/Commands/SynchroniseTranslations.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Commands/SynchroniseTranslations.php b/src/Commands/SynchroniseTranslations.php index a3621c8..27b4468 100644 --- a/src/Commands/SynchroniseTranslations.php +++ b/src/Commands/SynchroniseTranslations.php @@ -16,6 +16,7 @@ public function handle() { $poeditor = new PoEditor($this->argument('project')); + $locales = collect(); foreach ($poeditor->languages() as $language) { $languageFile = resource_path('lang/' . $language['code'] . '.json'); @@ -39,8 +40,18 @@ public function handle() // Write the new file \File::put($languageFile, $terms); + + $locales->push([ + 'name' => $language['name'], + 'code' => $language['code'], + ]); } + \File::put( + resource_path('lang/locales.json'), + $locales->toJson() + ); + $this->info("Translations synchronised"); } } From 3678b5ea424afdc00ba0d148fb6ff5bb4a96d45a Mon Sep 17 00:00:00 2001 From: Sven Buijsrogge Date: Sat, 12 May 2018 11:44:55 +0200 Subject: [PATCH 2/5] Write the locale file tests --- tests/LocalesTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/LocalesTest.php diff --git a/tests/LocalesTest.php b/tests/LocalesTest.php new file mode 100644 index 0000000..5c11305 --- /dev/null +++ b/tests/LocalesTest.php @@ -0,0 +1,29 @@ +artisan('synchronise:translations'); + + $poeditor = new PoEditor(); + + // Get the locales file + $locales = []; + foreach ($poeditor->languages() as $language) { + $this->assertFileExists( resource_path('lang/' . $language['code'] . '.json') ); + + $locales[] = [ + 'name' => $language['name'], + 'code' => $language['code'], + ]; + } + + $this->assertJsonStringEqualsJsonFile(resource_path('lang/locales.json'), json_encode($locales)); + } +} From b7d115a390d725e33711d5f616408dfbfbb1f485 Mon Sep 17 00:00:00 2001 From: Sven Buijsrogge Date: Sat, 12 May 2018 11:45:07 +0200 Subject: [PATCH 3/5] Rename file --- tests/LanguageTest.php | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 tests/LanguageTest.php diff --git a/tests/LanguageTest.php b/tests/LanguageTest.php deleted file mode 100644 index b35609b..0000000 --- a/tests/LanguageTest.php +++ /dev/null @@ -1,19 +0,0 @@ -artisan('synchronise:translations'); - - $poeditor = new PoEditor(); - foreach ($poeditor->languages() as $language) { - $this->assertFileExists( resource_path('lang/' . $language['code'] . '.json') ); - } - } -} From 8cf7d7e29bb1fb626ca9babb8d71497d05fbbcfa Mon Sep 17 00:00:00 2001 From: Sven Buijsrogge Date: Sat, 12 May 2018 11:45:27 +0200 Subject: [PATCH 4/5] Rewritten a couple of lines to make it more readable --- src/Commands/SynchroniseTranslations.php | 37 +++++++++++++++--------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/Commands/SynchroniseTranslations.php b/src/Commands/SynchroniseTranslations.php index 27b4468..9660a89 100644 --- a/src/Commands/SynchroniseTranslations.php +++ b/src/Commands/SynchroniseTranslations.php @@ -3,6 +3,7 @@ namespace SeBuDesign\PoEditor\Commands; use Illuminate\Console\Command; +use Illuminate\Support\Collection; use SeBuDesign\PoEditor\PoEditor; class SynchroniseTranslations extends Command @@ -18,8 +19,6 @@ public function handle() $locales = collect(); foreach ($poeditor->languages() as $language) { - $languageFile = resource_path('lang/' . $language['code'] . '.json'); - $terms = collect($poeditor->terms($language['code'])) // Pluck the translation content and set the term as the key ->pluck('translation.content', 'term') @@ -30,16 +29,12 @@ public function handle() } return $content; - }) - ->toJson(); - - // If an old file exists remove it - if (\File::exists($languageFile)) { - \File::delete($languageFile); - } + }); - // Write the new file - \File::put($languageFile, $terms); + $this->updateFile( + resource_path('lang/' . $language['code'] . '.json'), + $terms + ); $locales->push([ 'name' => $language['name'], @@ -47,11 +42,27 @@ public function handle() ]); } - \File::put( + $this->updateFile( resource_path('lang/locales.json'), - $locales->toJson() + $locales ); $this->info("Translations synchronised"); } + + protected function updateFile($file, $content) + { + // If an old file exists remove it + if (\File::exists($file)) { + \File::delete($file); + } + + // If it is a collection create a json string of it + if ($content instanceof Collection) { + $content = $content->toJson(); + } + + // Write the new file + \File::put($file, $content); + } } From 9ea9385ac18a6c07e2bdb5824ae28a4e61403dd3 Mon Sep 17 00:00:00 2001 From: Sven Buijsrogge Date: Sat, 12 May 2018 11:46:02 +0200 Subject: [PATCH 5/5] Add line for readability --- tests/LocalesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/LocalesTest.php b/tests/LocalesTest.php index 5c11305..1176ecc 100644 --- a/tests/LocalesTest.php +++ b/tests/LocalesTest.php @@ -23,7 +23,7 @@ public function it_has_all_locales_synchronised() 'code' => $language['code'], ]; } - + $this->assertJsonStringEqualsJsonFile(resource_path('lang/locales.json'), json_encode($locales)); } }