Skip to content

Commit 7ec96f3

Browse files
fergthhthemsaid
authored andcommitted
Finished feature to allow viewing the default translation in the missing command
1 parent 1006002 commit 7ec96f3

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
/.idea
12
/vendor
23
composer.phar
34
composer.lock
4-
.DS_Store
5+
.DS_Store

src/Commands/MissingCommand.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MissingCommand extends Command
1313
*
1414
* @var string
1515
*/
16-
protected $signature = 'langman:missing';
16+
protected $signature = 'langman:missing {--default}';
1717

1818
/**
1919
* The name and signature of the console command.
@@ -95,14 +95,47 @@ private function collectValues(array $missing)
9595
$values = [];
9696

9797
foreach ($missing as $missingKey) {
98+
// var_dump($this->getDefaultValueFor($missingKey));
9899
$values[$missingKey] = $this->ask(
99-
"<fg=yellow>{$missingKey}</> translation"
100+
"<fg=yellow>{$missingKey}</> translation", $this->getDefaultValueFor($missingKey)
100101
);
101102
}
102103

103104
return $values;
104105
}
105106

107+
/**
108+
* Get default value for the current translation request.
109+
* @param string $missingKey
110+
* @return mixed
111+
*/
112+
private function getDefaultValue($missingKey)
113+
{
114+
return $this->option('default') ? $this->getTranslationInDefaultLocaleFor($missingKey) : null;
115+
}
116+
117+
/**
118+
* Get translation in default locale for given key
119+
* @param string $missingKey
120+
* @return string
121+
*/
122+
private function getTranslationInDefaultLocaleFor($missingKey)
123+
{
124+
try {
125+
126+
list($langKey, $lang) = explode(':', $missingKey);
127+
128+
list($file, $key) = explode('.', $langKey);
129+
130+
$filePath = $this->manager->files()[$file][config('app.locale')];
131+
132+
return config('app.locale') . ":{$this->manager->getFileContent($filePath)[$key]}";
133+
134+
} catch (\Exception $e) {
135+
return "Sorry. File Language not exists for default locale.";
136+
}
137+
}
138+
106139
/**
107140
* Get an array of keys that have missing values with a hint
108141
* from another language translation file if possible.

tests/MissingCommandTest.php

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ public function testCommandOutput()
2222
]);
2323

2424
$command = m::mock('\Themsaid\Langman\Commands\MissingCommand[ask]', [$manager]);
25-
$command->shouldReceive('ask')->once()->with('/user\.age:nl/')->andReturn('fill_age');
26-
$command->shouldReceive('ask')->once()->with('/product\.name:en/')->andReturn('fill_name');
27-
$command->shouldReceive('ask')->once()->with('/product\.color:nl/')->andReturn('fill_color');
28-
$command->shouldReceive('ask')->once()->with('/product\.size:nl/')->andReturn('fill_size');
29-
$command->shouldReceive('ask')->once()->with('/missing\.missing\.id:nl/')->andReturn('fill_missing_id');
30-
$command->shouldReceive('ask')->once()->with('/missing\.missing\.price:en/')->andReturn('fill_missing_price');
31-
$command->shouldReceive('ask')->once()->with('/missing\.missing\.price:nl/')->andReturn('fill_missing_price');
25+
$command->shouldReceive('getDefaultValueFor')->times(7)->with(m::any())->andReturn(null);
26+
$command->shouldReceive('ask')->once()->with('/user\.age:nl/', null)->andReturn('fill_age');
27+
$command->shouldReceive('ask')->once()->with('/product\.name:en/', null)->andReturn('fill_name');
28+
$command->shouldReceive('ask')->once()->with('/product\.color:nl/', null)->andReturn('fill_color');
29+
$command->shouldReceive('ask')->once()->with('/product\.size:nl/', null)->andReturn('fill_size');
30+
$command->shouldReceive('ask')->once()->with('/missing\.missing\.id:nl/', null)->andReturn('fill_missing_id');
31+
$command->shouldReceive('ask')->once()->with('/missing\.missing\.price:en/', null)->andReturn('fill_missing_price');
32+
$command->shouldReceive('ask')->once()->with('/missing\.missing\.price:nl/', null)->andReturn('fill_missing_price');
3233

3334
$this->app['artisan']->add($command);
3435
$this->artisan('langman:missing');
@@ -47,4 +48,44 @@ public function testCommandOutput()
4748
$this->assertEquals('fill_missing_price', $missingNLFile['missing']['price']);
4849
$this->assertEquals('fill_missing_price', $missingENFile['missing']['price']);
4950
}
51+
52+
public function testAllowSeeTranslationInDefaultLanguage()
53+
{
54+
$this->app['config']->set('app.locale', 'en');
55+
$this->createTempFiles([
56+
'en' => [
57+
'user' => "<?php\n return ['name' => 'Name', 'age' => 'Age'];",
58+
],
59+
'nl' => [
60+
'user' => "<?php\n return ['name' => 'Naam'];",
61+
],
62+
]);
63+
$manager = $this->app[Manager::class];
64+
$command = m::mock('\Themsaid\Langman\Commands\MissingCommand[ask]', [$manager]);
65+
$command->shouldReceive('getDefaultValueFor')->once()->with('/user\.age/')->andReturn('en:Age');
66+
$command->shouldReceive('ask')->once()->with('/<fg=yellow>user\.age:nl<\/> translation/', '/en:Age/');
67+
$this->app['artisan']->add($command);
68+
69+
$this->artisan('langman:missing', ['--default' => true]);
70+
}
71+
72+
public function testThrowDefaultMessageWhenLanguageFileIsNotFound()
73+
{
74+
$this->app['config']->set('app.locale', 'es');
75+
$this->createTempFiles([
76+
'en' => [
77+
'user' => "<?php\n return ['name' => 'Name', 'age' => 'Age'];",
78+
],
79+
'nl' => [
80+
'user' => "<?php\n return ['name' => 'Naam'];",
81+
],
82+
]);
83+
$manager = $this->app[Manager::class];
84+
$command = m::mock('\Themsaid\Langman\Commands\MissingCommand[ask]', [$manager]);
85+
$command->shouldReceive('getDefaultValueFor')->once()->with('/user\.age/')->andReturn("Sorry. File Language not exists for default locale.");
86+
$command->shouldReceive('ask')->once()->with('/<fg=yellow>user\.age:nl<\/> translation/', '/Sorry\. File Language not exists for default locale\./');
87+
$this->app['artisan']->add($command);
88+
89+
$this->artisan('langman:missing', ['--default' => true]);
90+
}
5091
}

0 commit comments

Comments
 (0)