Inject custom translations when needed

This commit is contained in:
David Bomba 2020-08-11 11:03:49 +10:00
parent d05c6ec975
commit ab5dc13662
2 changed files with 27 additions and 37 deletions

View File

@ -30,51 +30,18 @@ class NinjaTranslator extends Translator
Arr::set($this->loaded[$namespace][$group][$locale], $item, $value);
}
/**
* Set multiple translations.
*
* @param array $items Format: [group => [key => value]]
* @param string $locale
* @return void
*/
public function add(array $items, $locale = null)
{
if(null === $locale)
{
$locale = $this->locale;
}
foreach($items as $group => $translations)
{
// Build key to parse
$key = $group.'.'.key($translations);
list($namespace, $group) = $this->parseKey($key);
// Load given group defaults if exists
$this->load($namespace, $group, $locale);
foreach($translations as $item => $value)
{
Arr::set($this->loaded[$namespace][$group][$locale], $item, $value);
}
}
}
public function replace($items, $locale = null)
{
if(null === $locale)
{
$locale = $this->locale;
}
// Load given group defaults if exists
$this->load($namespace, $group, $locale);
foreach($items as $key => $value)
{
list($namespace, $group, $item) = $this->parseKey($key);
list($namespace, $group, $item) = $this->parseKey($key);
$this->load($namespace, $group, $locale);
Arr::set($this->loaded[$namespace][$group][$locale], $item, $value);
}

View File

@ -27,5 +27,28 @@ class TranslationTest extends TestCase
}
public function testReplaceTranslation()
{
Lang::set('texts.invoice_number', 'test');
$this->assertEquals('test', trans('texts.invoice_number'));
}
public function testReplaceArray()
{
$data = [
'texts.invoice_number' => 'test',
'texts.custom_translation' => 'test2'
];
Lang::replace($data);
$this->assertEquals('test', trans('texts.invoice_number'));
$this->assertEquals('test2', trans('texts.custom_translation'));
}
}