From d05c6ec97582d832f5fbdf27727a280b3ae68b43 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 11 Aug 2020 10:48:05 +1000 Subject: [PATCH 1/3] Inject custom translations --- app/Helpers/Language/NinjaTranslator.php | 83 +++++++++++++++++++ app/Providers/AppServiceProvider.php | 9 +- app/Providers/AuthServiceProvider.php | 11 +-- app/Providers/EventServiceProvider.php | 4 - .../NinjaTranslationServiceProvider.php | 37 +++++++++ config/app.php | 1 + tests/Unit/TranslationTest.php | 31 +++++++ 7 files changed, 158 insertions(+), 18 deletions(-) create mode 100644 app/Helpers/Language/NinjaTranslator.php create mode 100644 app/Providers/NinjaTranslationServiceProvider.php create mode 100644 tests/Unit/TranslationTest.php diff --git a/app/Helpers/Language/NinjaTranslator.php b/app/Helpers/Language/NinjaTranslator.php new file mode 100644 index 000000000000..3edec7587579 --- /dev/null +++ b/app/Helpers/Language/NinjaTranslator.php @@ -0,0 +1,83 @@ +parseKey($key); + + if(null === $locale) + { + $locale = $this->locale; + } + + // Load given group defaults if exists + $this->load($namespace, $group, $locale); + + 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); + + Arr::set($this->loaded[$namespace][$group][$locale], $item, $value); + } + + } +} \ No newline at end of file diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 3bb397b03a54..154cb4b4559b 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -11,6 +11,7 @@ namespace App\Providers; +use App\Helpers\Language\DecoratedTranslator; use App\Models\Account; use App\Models\Client; use App\Models\Company; @@ -36,12 +37,12 @@ use App\Observers\QuoteObserver; use App\Observers\TaskObserver; use App\Observers\UserObserver; use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Support\Facades\Blade; -use Illuminate\Support\Facades\Schema; -use Illuminate\Support\ServiceProvider; -use Illuminate\Support\Facades\Queue; use Illuminate\Queue\Events\JobFailed; use Illuminate\Queue\Events\JobProcessing; +use Illuminate\Support\Facades\Blade; +use Illuminate\Support\Facades\Queue; +use Illuminate\Support\Facades\Schema; +use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 6f6909681ce5..fca6b60c28fa 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -97,20 +97,11 @@ class AuthServiceProvider extends ServiceProvider public function boot() { $this->registerPolicies(); - /* - Auth::provider('users', function ($app, array $config) { - return new MultiDatabaseUserProvider($this->app['hash'], $config['model']); - }); - Auth::provider('contacts', function ($app, array $config) { - return new MultiDatabaseUserProvider($this->app['hash'], $config['model']); - - }); - */ Gate::define('view-list', function ($user, $entity) { $entity = strtolower(class_basename($entity)); - return $user->hasPermission('view_' . $entity) || $user->isAdmin(); }); } + } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index d94e889d73b6..29b981249f8c 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -287,10 +287,6 @@ class EventServiceProvider extends ServiceProvider * * @return void */ - // public function boot() - // { - // parent::boot(); - // } public function boot() { diff --git a/app/Providers/NinjaTranslationServiceProvider.php b/app/Providers/NinjaTranslationServiceProvider.php new file mode 100644 index 000000000000..bf7d0c73243c --- /dev/null +++ b/app/Providers/NinjaTranslationServiceProvider.php @@ -0,0 +1,37 @@ +app->singleton('translator', function($app) + { + $loader = $app['translation.loader']; + $locale = $app['config']['app.locale']; + + $trans = new NinjaTranslator($loader, $locale); + + $trans->setFallback($app['config']['app.fallback_locale']); + + return $trans; + }); + + } +} + diff --git a/config/app.php b/config/app.php index 33ddaf7a8049..bbd643def594 100644 --- a/config/app.php +++ b/config/app.php @@ -180,6 +180,7 @@ return [ App\Providers\ComposerServiceProvider::class, App\Providers\MultiDBProvider::class, App\Providers\ClientPortalServiceProvider::class, + App\Providers\NinjaTranslationServiceProvider::class, ], /* diff --git a/tests/Unit/TranslationTest.php b/tests/Unit/TranslationTest.php new file mode 100644 index 000000000000..3b99a0c8182e --- /dev/null +++ b/tests/Unit/TranslationTest.php @@ -0,0 +1,31 @@ +assertEquals('test', trans('texts.test_translation_string')); + + } + + + +} From ab5dc13662d64fecec150493cb62750ec620ac6b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 11 Aug 2020 11:03:49 +1000 Subject: [PATCH 2/3] 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')); + + } } From c29c6ba480637ee751550ffeed737a242ec7f9b6 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 11 Aug 2020 11:13:49 +1000 Subject: [PATCH 3/3] Cleanup for Custom Translations --- app/Providers/NinjaTranslationServiceProvider.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/Providers/NinjaTranslationServiceProvider.php b/app/Providers/NinjaTranslationServiceProvider.php index bf7d0c73243c..df58fcabc6fd 100644 --- a/app/Providers/NinjaTranslationServiceProvider.php +++ b/app/Providers/NinjaTranslationServiceProvider.php @@ -18,8 +18,21 @@ class NinjaTranslationServiceProvider extends TranslationServiceProvider { public function boot() { - //parent::boot(); + /** + * To reset the translator instance we call + * + * App::forgetInstance('translator'); + * + * Why? As the translator is a singleton it persists for its + * lifecycle + * + * We _must_ reset the singleton when shifting between + * clients/companies otherwise translations will + * persist. + * + */ + $this->app->singleton('translator', function($app) { $loader = $app['translation.loader'];