From 5d1dce347f2351601c0649bd2e42cba2697c75c5 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Fri, 6 Sep 2019 18:52:34 +0800 Subject: [PATCH] Switch to another translations loader --- app/Providers/TranslationServiceProvider.php | 16 --- app/Services/PluginManager.php | 5 +- app/Services/TranslationLoader.php | 41 ------ app/Services/Translations/Loader.php | 19 +++ app/Services/Translations/Yaml.php | 38 ++++++ composer.json | 5 +- composer.lock | 123 ++++++++++-------- config/app.php | 1 - config/translation-loader.php | 25 ++++ config/yaml-translation.php | 15 --- ..._05_130811_create_language_lines_table.php | 34 +++++ resources/lang/en/auth.yml | 1 - storage/yaml-translation/.gitignore | 3 - tests/CommandsTest/BsInstallCommandTest.php | 1 + tests/ServicesTest/PluginManagerTest.php | 2 +- 15 files changed, 193 insertions(+), 136 deletions(-) delete mode 100644 app/Providers/TranslationServiceProvider.php delete mode 100644 app/Services/TranslationLoader.php create mode 100644 app/Services/Translations/Loader.php create mode 100644 app/Services/Translations/Yaml.php create mode 100644 config/translation-loader.php delete mode 100644 config/yaml-translation.php create mode 100644 database/migrations/2019_09_05_130811_create_language_lines_table.php delete mode 100644 storage/yaml-translation/.gitignore diff --git a/app/Providers/TranslationServiceProvider.php b/app/Providers/TranslationServiceProvider.php deleted file mode 100644 index d41252d0..00000000 --- a/app/Providers/TranslationServiceProvider.php +++ /dev/null @@ -1,16 +0,0 @@ -app->singleton('translation.loader', function ($app) { - return new TranslationLoader($app['files'], $app['path.lang']); - }); - } -} diff --git a/app/Services/PluginManager.php b/app/Services/PluginManager.php index c6c57d46..3a2aaf25 100644 --- a/app/Services/PluginManager.php +++ b/app/Services/PluginManager.php @@ -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'); } diff --git a/app/Services/TranslationLoader.php b/app/Services/TranslationLoader.php deleted file mode 100644 index f4f7a5cd..00000000 --- a/app/Services/TranslationLoader.php +++ /dev/null @@ -1,41 +0,0 @@ -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); - } -} diff --git a/app/Services/Translations/Loader.php b/app/Services/Translations/Loader.php new file mode 100644 index 00000000..723bb118 --- /dev/null +++ b/app/Services/Translations/Loader.php @@ -0,0 +1,19 @@ +files->exists($full) + ? resolve(Yaml::class)->loadYaml($full) + : []; + } +} diff --git a/app/Services/Translations/Yaml.php b/app/Services/Translations/Yaml.php new file mode 100644 index 00000000..0ebf6e0c --- /dev/null +++ b/app/Services/Translations/Yaml.php @@ -0,0 +1,38 @@ +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); + }); + } +} diff --git a/composer.json b/composer.json index e33c7f00..1ee664e1 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 33cf3055..7ccd21dc 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/config/app.php b/config/app.php index d5b677c2..d3be00da 100644 --- a/config/app.php +++ b/config/app.php @@ -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, ], diff --git a/config/translation-loader.php b/config/translation-loader.php new file mode 100644 index 00000000..09b975ba --- /dev/null +++ b/config/translation-loader.php @@ -0,0 +1,25 @@ + [ + 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, + +]; diff --git a/config/yaml-translation.php b/config/yaml-translation.php deleted file mode 100644 index aa95818e..00000000 --- a/config/yaml-translation.php +++ /dev/null @@ -1,15 +0,0 @@ - false, -]; diff --git a/database/migrations/2019_09_05_130811_create_language_lines_table.php b/database/migrations/2019_09_05_130811_create_language_lines_table.php new file mode 100644 index 00000000..e40eaae0 --- /dev/null +++ b/database/migrations/2019_09_05_130811_create_language_lines_table.php @@ -0,0 +1,34 @@ +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'); + } +} diff --git a/resources/lang/en/auth.yml b/resources/lang/en/auth.yml index 8030094d..3a888e01 100644 --- a/resources/lang/en/auth.yml +++ b/resources/lang/en/auth.yml @@ -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. diff --git a/storage/yaml-translation/.gitignore b/storage/yaml-translation/.gitignore deleted file mode 100644 index 8f4803c0..00000000 --- a/storage/yaml-translation/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -* -!public/ -!.gitignore diff --git a/tests/CommandsTest/BsInstallCommandTest.php b/tests/CommandsTest/BsInstallCommandTest.php index 6c3b8afe..4e9bf4bc 100644 --- a/tests/CommandsTest/BsInstallCommandTest.php +++ b/tests/CommandsTest/BsInstallCommandTest.php @@ -30,6 +30,7 @@ class BsInstallCommandTest extends TestCase 'oauth_refresh_tokens', 'notifications', 'jobs', + 'language_lines', ]; array_walk($tables, function ($table) { Schema::dropIfExists($table); diff --git a/tests/ServicesTest/PluginManagerTest.php b/tests/ServicesTest/PluginManagerTest.php index e3b2d76c..f3495861 100644 --- a/tests/ServicesTest/PluginManagerTest.php +++ b/tests/ServicesTest/PluginManagerTest.php @@ -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();