Skip to content

Commit

Permalink
Merge pull request #33 from ben182/develop
Browse files Browse the repository at this point in the history
0.7.0
  • Loading branch information
ben182 authored Sep 15, 2019
2 parents b4607a0 + 13f00d1 commit 6c7f709
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":812:{a:2:{s:7:"defects";a:1:{s:76:"Ben182\AutoTranslate\Tests\AutoTranslateTest::test_translate_with_no_content";i:4;}s:5:"times";a:8:{s:60:"Ben182\AutoTranslate\Tests\AutoTranslateTest::test_translate";d:0.012;s:73:"Ben182\AutoTranslate\Tests\AutoTranslateTest::test_getMissingTranslations";d:0.011;s:62:"Ben182\AutoTranslate\Tests\AutoTranslateTest::test_array_undot";d:0.011;s:66:"Ben182\AutoTranslate\Tests\AutoTranslateTest::test_getTranslations";d:0.061;s:74:"Ben182\AutoTranslate\Tests\AutoTranslateTest::test_getMissingTranslations2";d:0.008;s:74:"Ben182\AutoTranslate\Tests\AutoTranslateTest::test_getMissingTranslations3";d:0.01;s:68:"Ben182\AutoTranslate\Tests\AutoTranslateTest::test_fillLanguageFiles";d:0.007;s:76:"Ben182\AutoTranslate\Tests\AutoTranslateTest::test_translate_with_no_content";d:0.007;}}}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<a href="https://travis-ci.org/ben182/laravel-auto-translate" rel="nofollow"><img src="https://camo.githubusercontent.com/8c01aa130a16fabf6a8e313719f4f274c7c401b4/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f62656e3138322f6c61726176656c2d6175746f2d7472616e736c6174652f6d61737465722e7376673f7374796c653d666c61742d737175617265" alt="Build Status" data-canonical-src="https://img.shields.io/travis/ben182/laravel-auto-translate/master.svg?style=flat-square" style="max-width:100%;"></a>
<a href="https://scrutinizer-ci.com/g/ben182/laravel-auto-translate" rel="nofollow"><img src="https://camo.githubusercontent.com/a2132ab348aaaeae4e0cfee432965a86b8d6b7af/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f62656e3138322f6c61726176656c2d6175746f2d7472616e736c6174652e7376673f7374796c653d666c61742d737175617265" alt="Quality Score" data-canonical-src="https://img.shields.io/scrutinizer/g/ben182/laravel-auto-translate.svg?style=flat-square" style="max-width:100%;"></a></p>

With this package you can translate your language files using a translator service. The package ships with a free Google Translate version and Deepl.
With this package you can translate your language files using a translator service. The package ships with a free Google Translate version, Google Translate API and Deepl.

Specify a source language and a target language and it will automatically translate your files. This is useful if you want to prototype something quickly or just a first idea of the translation for later editing. The package ships with two artisan commands. One for translating all the missing translations that are set in the source language but not in the target language. The other one for translating all source language files and overwriting everything in the target language.

Expand Down
9 changes: 9 additions & 0 deletions src/AutoTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ public function translate(string $targetLanguage, $data, $callbackAfterEachTrans
$dottedSource = Arr::dot($data);

foreach ($dottedSource as $key => $value) {
if ($value === '') {
$dottedSource[$key] = $value;

if ($callbackAfterEachTranslation) {
$callbackAfterEachTranslation();
}
continue;
}

$variables = $this->findVariables($value);

$dottedSource[$key] = is_string($value) ? $this->translator->translate($value) : $value;
Expand Down
18 changes: 17 additions & 1 deletion src/Commands/AllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,23 @@ public function handle()

$availableTranslations = 0;
$sourceTranslations = $this->autoTranslator->getSourceTranslations();
$availableTranslations = count(Arr::dot($sourceTranslations)) * count($targetLanguages);
$availableTranslations = count($dottedTranslations = Arr::dot($sourceTranslations)) * count($targetLanguages);

if (empty($dottedTranslations)) {
$this->line('0 keys found...aborting');

return;
}

$strLen = collect($dottedTranslations)->map(function ($value) {
return strlen($value);
})->sum() * count($targetLanguages);

$this->line($strLen.' characters will be translated');

if (! $this->confirm('Continue?', true)) {
return;
}

$bar = $this->output->createProgressBar($availableTranslations);
$bar->start();
Expand Down
16 changes: 16 additions & 0 deletions src/Commands/MissingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,28 @@ public function handle()
$this->line('Found '.$foundLanguages.' '.Str::plural('language', $foundLanguages).' to translate');

$missingCount = 0;
$strLen = 0;
foreach ($targetLanguages as $targetLanguage) {
$missing = $this->autoTranslator->getMissingTranslations($targetLanguage);
$missingCount += $missing->count();
$strLen += $missing->map(function ($value) {
return strlen($value);
})->sum();
$this->line('Found '.$missing->count().' missing keys in '.$targetLanguage);
}

if ($missingCount === 0) {
$this->line('0 missing keys found...aborting');

return;
}

$this->line($strLen.' characters will be translated');

if (! $this->confirm('Continue?', true)) {
return;
}

$bar = $this->output->createProgressBar($missingCount);
$bar->start();

Expand Down
19 changes: 19 additions & 0 deletions tests/AutoTranslateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,23 @@ public function test_translate()
],
], $translations);
}

public function test_translate_with_no_content()
{
$mock = Mockery::mock(TranslatorInterface::class);
$mock
->shouldReceive('setSource')
->shouldReceive('setTarget')

->shouldNotReceive('translate')
->mock();

$this->app->instance(TranslatorInterface::class, $mock);

$translations = AutoTranslateFacade::translate('de', [
'user' => [
'age' => '',
],
]);
}
}

0 comments on commit 6c7f709

Please sign in to comment.