1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Merge pull request #3976 from turbo124/v2

Custom Translations
This commit is contained in:
David Bomba 2020-08-11 11:14:35 +10:00 committed by GitHub
commit f19bb5706c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 161 additions and 18 deletions

View File

@ -0,0 +1,50 @@
<?php
namespace App\Helpers\Language;
use Illuminate\Support\Arr;
use Illuminate\Translation\Translator;
class NinjaTranslator extends Translator
{
/**
* Set translation.
*
* @param string $key
* @param mixed $value
* @param string $locale
* @return void
*/
public function set($key, $value, $locale = null)
{
list($namespace, $group, $item) = $this->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);
}
public function replace($items, $locale = null)
{
if(null === $locale)
$locale = $this->locale;
foreach($items as $key => $value)
{
list($namespace, $group, $item) = $this->parseKey($key);
$this->load($namespace, $group, $locale);
Arr::set($this->loaded[$namespace][$group][$locale], $item, $value);
}
}
}

View File

@ -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
{

View File

@ -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();
});
}
}

View File

@ -287,10 +287,6 @@ class EventServiceProvider extends ServiceProvider
*
* @return void
*/
// public function boot()
// {
// parent::boot();
// }
public function boot()
{

View File

@ -0,0 +1,50 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Providers;
use App\Helpers\Language\NinjaTranslator;
use Illuminate\Translation\TranslationServiceProvider;
class NinjaTranslationServiceProvider extends TranslationServiceProvider
{
public function 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'];
$locale = $app['config']['app.locale'];
$trans = new NinjaTranslator($loader, $locale);
$trans->setFallback($app['config']['app.fallback_locale']);
return $trans;
});
}
}

View File

@ -180,6 +180,7 @@ return [
App\Providers\ComposerServiceProvider::class,
App\Providers\MultiDBProvider::class,
App\Providers\ClientPortalServiceProvider::class,
App\Providers\NinjaTranslationServiceProvider::class,
],
/*

View File

@ -0,0 +1,54 @@
<?php
namespace Tests\Unit;
use Illuminate\Support\Facades\Lang;
use Tests\TestCase;
/**
* @test
*/
class TranslationTest extends TestCase
{
public function setUp() :void
{
parent::setUp();
}
public function testAddTranslation()
{
Lang::set('texts.test_translation_string', 'test');
$this->assertEquals('test', trans('texts.test_translation_string'));
}
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'));
}
}