Switch to another translations loader

This commit is contained in:
Pig Fang 2019-09-06 18:52:34 +08:00
parent c567b12adc
commit 5d1dce347f
15 changed files with 193 additions and 136 deletions

View File

@ -1,16 +0,0 @@
<?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

@ -206,12 +206,13 @@ class PluginManager
*/
protected function loadViewsAndTranslations(Plugin $plugin)
{
$translations = $this->app->make('translation.loader');
$view = $this->app->make('view');
$namespace = $plugin->namespace;
$path = $plugin->getPath();
$translations = $this->app->make('translation.loader');
$translations->addNamespace($namespace, $path.'/lang');
$view = $this->app->make('view');
$view->addNamespace($namespace, $path.'/views');
}

View File

@ -1,41 +0,0 @@
<?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

@ -0,0 +1,19 @@
<?php
namespace App\Services\Translations;
use Spatie\TranslationLoader\TranslationLoaderManager;
class Loader extends TranslationLoaderManager
{
protected function loadPath($path, $locale, $group)
{
$translations = parent::loadPath($path, $locale, $group);
$full = "{$path}/{$locale}/{$group}.yml";
return count($translations) === 0 && $this->files->exists($full)
? resolve(Yaml::class)->loadYaml($full)
: [];
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace App\Services\Translations;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Contracts\Cache\Repository;
use Symfony\Component\Yaml\Yaml as YamlParser;
use Spatie\TranslationLoader\TranslationLoaders\TranslationLoader;
class Yaml implements TranslationLoader
{
/** @var Filesystem */
protected $files;
/** @var Repository */
protected $cache;
public function __construct(Filesystem $files, Repository $cache)
{
$this->files = $files;
$this->cache = $cache;
}
public function loadTranslations(string $locale, string $group): array
{
$path = resource_path("lang/$locale/$group.yml");
return file_exists($path) ? $this->loadYaml($path) : [];
}
public function loadYaml(string $path): array
{
$key = 'yaml-trans-'.md5($path).'-'.filemtime($path);
return $this->cache->rememberForever($key, function () use ($path) {
return YamlParser::parseFile($path);
});
}
}

View File

@ -17,14 +17,15 @@
"doctrine/inflector": "1.1.0",
"laravel/framework": "5.8.*",
"nesbot/carbon": "^2.0",
"devitek/yaml-translation": "^4.1.0",
"composer/semver": "^1.4",
"gregwar/captcha": "1.*",
"guzzlehttp/guzzle": "^6.3",
"doctrine/dbal": "^2.9",
"tymon/jwt-auth": "dev-develop",
"laravel/passport": "^7.3",
"composer/ca-bundle": "^1.2"
"composer/ca-bundle": "^1.2",
"spatie/laravel-translation-loader": "^2.4",
"symfony/yaml": "^4.3"
},
"require-dev": {
"fzaninotto/faker": "~1.8",

123
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "6a5e7188b9f242f23c36e54da0dada9b",
"content-hash": "b867f3af8526a05f22ad605d89ca0f26",
"packages": [
{
"name": "composer/ca-bundle",
@ -187,52 +187,6 @@
],
"time": "2018-07-24T23:27:56+00:00"
},
{
"name": "devitek/yaml-translation",
"version": "4.1.0",
"source": {
"type": "git",
"url": "https://github.com/Devitek/laravel-yaml-translation.git",
"reference": "2b7e5bcef203e77cfa887471bd4016114b4caaf4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Devitek/laravel-yaml-translation/zipball/2b7e5bcef203e77cfa887471bd4016114b4caaf4",
"reference": "2b7e5bcef203e77cfa887471bd4016114b4caaf4",
"shasum": ""
},
"require": {
"illuminate/support": "~5.5",
"illuminate/translation": "~5.5",
"php": ">=7.0",
"symfony/yaml": "3.*"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Devitek\\Core\\Translation\\TranslationServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Devitek\\": "src/Devitek"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Thomas SIEFFERT",
"email": "thomas.sieffert@devitek.fr"
}
],
"description": "Add YAML file support to Laravel TranslationServiceProvider",
"time": "2017-09-28T12:37:17+00:00"
},
{
"name": "doctrine/cache",
"version": "v1.8.0",
@ -2395,6 +2349,67 @@
],
"time": "2018-07-19T23:38:55+00:00"
},
{
"name": "spatie/laravel-translation-loader",
"version": "2.4.0",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-translation-loader.git",
"reference": "ea131d50aea0fcb8854244fa028dc0f587b76260"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-translation-loader/zipball/ea131d50aea0fcb8854244fa028dc0f587b76260",
"reference": "ea131d50aea0fcb8854244fa028dc0f587b76260",
"shasum": ""
},
"require": {
"illuminate/translation": "~5.8.0|^6.0",
"php": "^7.1"
},
"require-dev": {
"orchestra/testbench": "~3.8.0|^4.0",
"phpunit/phpunit": "^8.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Spatie\\TranslationLoader\\TranslationServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Spatie\\TranslationLoader\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "Store your language lines in the database, yaml or other sources",
"homepage": "https://github.com/spatie/laravel-translation-loader",
"keywords": [
"database",
"db",
"i8n",
"language",
"laravel",
"laravel-translation-loader",
"spatie",
"translate"
],
"time": "2019-09-04T08:06:16+00:00"
},
{
"name": "swiftmailer/swiftmailer",
"version": "v6.2.1",
@ -3942,20 +3957,20 @@
},
{
"name": "symfony/yaml",
"version": "v3.4.30",
"version": "v4.3.4",
"source": {
"type": "git",
"url": "https://github.com/symfony/yaml.git",
"reference": "051d045c684148060ebfc9affb7e3f5e0899d40b"
"reference": "5a0b7c32dc3ec56fd4abae8a4a71b0cf05013686"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/yaml/zipball/051d045c684148060ebfc9affb7e3f5e0899d40b",
"reference": "051d045c684148060ebfc9affb7e3f5e0899d40b",
"url": "https://api.github.com/repos/symfony/yaml/zipball/5a0b7c32dc3ec56fd4abae8a4a71b0cf05013686",
"reference": "5a0b7c32dc3ec56fd4abae8a4a71b0cf05013686",
"shasum": ""
},
"require": {
"php": "^5.5.9|>=7.0.8",
"php": "^7.1.3",
"symfony/polyfill-ctype": "~1.8"
},
"conflict": {
@ -3970,7 +3985,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.4-dev"
"dev-master": "4.3-dev"
}
},
"autoload": {
@ -3997,7 +4012,7 @@
],
"description": "Symfony Yaml Component",
"homepage": "https://symfony.com",
"time": "2019-07-24T13:01:31+00:00"
"time": "2019-08-20T14:27:59+00:00"
},
{
"name": "tijsverkoyen/css-to-inline-styles",

View File

@ -176,7 +176,6 @@ return [
App\Providers\PluginServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\ResponseMacroServiceProvider::class,
App\Providers\TranslationServiceProvider::class,
App\Providers\ValidatorExtendServiceProvider::class,
],

View File

@ -0,0 +1,25 @@
<?php
return [
/*
* Language lines will be fetched by these loaders. You can put any class here that implements
* the Spatie\TranslationLoader\TranslationLoaders\TranslationLoader-interface.
*/
'translation_loaders' => [
Spatie\TranslationLoader\TranslationLoaders\Db::class,
App\Services\Translations\Yaml::class,
],
/*
* This is the model used by the Db Translation loader. You can put any model here
* that extends Spatie\TranslationLoader\LanguageLine.
*/
'model' => Spatie\TranslationLoader\LanguageLine::class,
/*
* This is the translation manager which overrides the default Laravel `translation.loader`
*/
'translation_manager' => App\Services\Translations\Loader::class,
];

View File

@ -1,15 +0,0 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Array handling
|--------------------------------------------------------------------------
|
| Shall this package use the array_dot schema of Laravel?
| If true, this will flatten all array contents into a string.
*/
'dot_syntax' => false,
];

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateLanguageLinesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('language_lines', function (Blueprint $table) {
$table->increments('id');
$table->string('group');
$table->index('group');
$table->string('key');
$table->text('text');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('language_lines');
}
}

View File

@ -34,7 +34,6 @@ forgot:
unregistered: The email address is not registered.
success: Mail sent, please check your inbox. The link will be expired in 1 hour.
failed: Failed to send verification mail. :msg
message: You are receiving this email because this email address was used to reset your password on :sitename
ignore: If you haven't signed up on our site, please ignore this email. No unsubscribing is required.
reset: Reset your password
notice: This mail is sending automatically, no reponses will be sent if you reply.

View File

@ -1,3 +0,0 @@
*
!public/
!.gitignore

View File

@ -30,6 +30,7 @@ class BsInstallCommandTest extends TestCase
'oauth_refresh_tokens',
'notifications',
'jobs',
'language_lines',
];
array_walk($tables, function ($table) {
Schema::dropIfExists($table);

View File

@ -260,7 +260,7 @@ class PluginManagerTest extends TestCase
->withArgs(['Chitanda', '/chitanda/views'])
->once();
});
$this->instance('translation.loader', \Mockery::mock(\App\Services\TranslationLoader::class, function ($mock) {
$this->instance('translation.loader', \Mockery::mock(\App\Services\Translations\Loader::class, function ($mock) {
$mock->shouldReceive('addNamespace')
->withArgs(['Mayaka', '/mayaka/lang'])
->once();