Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Buijsrogge committed May 12, 2018
2 parents a5887c0 + 86ad4f7 commit cb86856
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
42 changes: 32 additions & 10 deletions src/Commands/SynchroniseTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SeBuDesign\PoEditor\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use SeBuDesign\PoEditor\PoEditor;

class SynchroniseTranslations extends Command
Expand All @@ -16,9 +17,8 @@ public function handle()
{
$poeditor = new PoEditor($this->argument('project'));

$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')
Expand All @@ -29,18 +29,40 @@ public function handle()
}

return $content;
})
->toJson();
});

// If an old file exists remove it
if (\File::exists($languageFile)) {
\File::delete($languageFile);
}
$this->updateFile(
resource_path('lang/' . $language['code'] . '.json'),
$terms
);

// Write the new file
\File::put($languageFile, $terms);
$locales->push([
'name' => $language['name'],
'code' => $language['code'],
]);
}

$this->updateFile(
resource_path('lang/locales.json'),
$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);
}
}
19 changes: 0 additions & 19 deletions tests/LanguageTest.php

This file was deleted.

29 changes: 29 additions & 0 deletions tests/LocalesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace SeBuDesign\PoEditor\Test;

use SeBuDesign\PoEditor\PoEditor;

class LocalesTest extends TestCase
{
/** @test */
public function it_has_all_locales_synchronised()
{
$this->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));
}
}

0 comments on commit cb86856

Please # to comment.