Allow overriding translations

This commit is contained in:
Pig Fang 2018-07-22 11:06:16 +08:00
parent 02ba6ed3df
commit aa149f4f3e
4 changed files with 60 additions and 1 deletions

View File

@ -0,0 +1,16 @@
<?php
namespace App\Providers;
use App\Services\TranslationLoader;
use Illuminate\Translation\TranslationServiceProvider as IlluminateTranslationServiceProvider;
class TranslationServiceProvider extends IlluminateTranslationServiceProvider
{
protected function registerLoader()
{
$this->app->singleton('translation.loader', function ($app) {
return new TranslationLoader($app['files'], $app['path.lang']);
});
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace App\Services;
use Devitek\Core\Translation\YamlFileLoader;
class TranslationLoader extends YamlFileLoader
{
/**
* Load the messages for the given locale.
*
* @param string $locale
* @param string $group
* @param string $namespace
* @return array
*/
public function load($locale, $group, $namespace = null)
{
if (is_null($namespace) || $namespace == '*') {
// Overrides original translations with custom ones
return array_replace_recursive(
$this->loadPath($this->path, $locale, $group),
$this->loadPathOverrides($locale, $group)
);
}
return $this->loadNamespaced($locale, $group, $namespace);
}
/**
* Load custom messages from /resources/lang/overrides path.
*
* @param string $locale
* @param string $group
* @return array
*/
protected function loadPathOverrides($locale, $group)
{
return $this->loadPath("$this->path/overrides", $locale, $group);
}
}

View File

@ -159,7 +159,6 @@ return [
/**
* Third-party libraries
*/
Devitek\Core\Translation\TranslationServiceProvider::class,
Swiggles\Memcache\MemcacheServiceProvider::class,
Yajra\Datatables\DatatablesServiceProvider::class,
Mews\Captcha\CaptchaServiceProvider::class,
@ -174,6 +173,7 @@ return [
App\Providers\MemoryServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\ResponseMacroServiceProvider::class,
App\Providers\TranslationServiceProvider::class,
App\Providers\ValidatorExtendServiceProvider::class,
],

2
resources/lang/overrides/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!.gitignore