From ab5dc13662d64fecec150493cb62750ec620ac6b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 11 Aug 2020 11:03:49 +1000 Subject: [PATCH] Inject custom translations when needed --- app/Helpers/Language/NinjaTranslator.php | 41 +++--------------------- tests/Unit/TranslationTest.php | 23 +++++++++++++ 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/app/Helpers/Language/NinjaTranslator.php b/app/Helpers/Language/NinjaTranslator.php index 3edec7587579..50517f903dad 100644 --- a/app/Helpers/Language/NinjaTranslator.php +++ b/app/Helpers/Language/NinjaTranslator.php @@ -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); } diff --git a/tests/Unit/TranslationTest.php b/tests/Unit/TranslationTest.php index 3b99a0c8182e..bda53eabcbb5 100644 --- a/tests/Unit/TranslationTest.php +++ b/tests/Unit/TranslationTest.php @@ -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')); + + } }