From 1669269ca3884ac17ff9e2830be9712e2d6e652b Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Tue, 7 Apr 2020 16:09:30 +0800 Subject: [PATCH] allow l10n of plugins market registry --- app/Http/Controllers/MarketController.php | 7 +- config/plugins.php | 12 ++- .../ControllersTest/MarketControllerTest.php | 73 ++++++++++++------- 3 files changed, 65 insertions(+), 27 deletions(-) diff --git a/app/Http/Controllers/MarketController.php b/app/Http/Controllers/MarketController.php index 7ac87330f..d3857fd74 100644 --- a/app/Http/Controllers/MarketController.php +++ b/app/Http/Controllers/MarketController.php @@ -76,8 +76,13 @@ public function download(Request $request, PluginManager $manager, Unzip $unzip) protected function fetch(): Collection { + $lang = in_array(app()->getLocale(), config('plugins.locales')) + ? app()->getLocale() + : config('app.fallback_locale'); + $plugins = collect(explode(',', config('plugins.registry'))) - ->map(function ($registry) { + ->map(function ($registry) use ($lang) { + $registry = str_replace('{lang}', $lang, $registry); $response = Http::withOptions([ 'verify' => CaBundle::getSystemCaRootBundlePath(), ])->get(trim($registry)); diff --git a/config/plugins.php b/config/plugins.php index f30a37283..b9e83cc8d 100644 --- a/config/plugins.php +++ b/config/plugins.php @@ -33,6 +33,16 @@ */ 'registry' => env( 'PLUGINS_REGISTRY', - 'https://cdn.jsdelivr.net/gh/bs-community/plugins-dist@latest/registry-preview.json' + 'https://cdn.jsdelivr.net/gh/bs-community/plugins-dist@latest/registry-preview_{lang}.json' ), + + /* + |-------------------------------------------------------------------------- + | Plugins Market Localization + |-------------------------------------------------------------------------- + | + | Supported languages of plugins market registry will be listed here. + | + */ + 'locales' => ['en', 'zh_CN'], ]; diff --git a/tests/HttpTest/ControllersTest/MarketControllerTest.php b/tests/HttpTest/ControllersTest/MarketControllerTest.php index 51272c946..01366826f 100644 --- a/tests/HttpTest/ControllersTest/MarketControllerTest.php +++ b/tests/HttpTest/ControllersTest/MarketControllerTest.php @@ -17,8 +17,9 @@ protected function setUp(): void public function testDownload() { + $registryUrl = str_replace('{lang}', 'en', config('plugins.registry')); Http::fake([ - config('plugins.registry') => Http::sequence() + $registryUrl => Http::sequence() ->push(['version' => 1, 'packages' => []]) ->push([ 'version' => 1, @@ -82,44 +83,49 @@ public function testDownload() public function testMarketData() { + $registry = [ + 'version' => 1, + 'packages' => [ + [ + 'name' => 'fake1', + 'title' => 'Fake', + 'version' => '1.0.0', + 'description' => '', + 'author' => '', + 'dist' => [], + 'require' => [], + ], + [ + 'name' => 'fake2', + 'title' => 'Fake', + 'version' => '0.0.0', + 'description' => '', + 'author' => '', + 'dist' => [], + 'require' => [], + ], + ], + ]; + Http::fakeSequence() ->pushStatus(404) - ->push([ - 'version' => 1, - 'packages' => [ - [ - 'name' => 'fake1', - 'title' => 'Fake', - 'version' => '1.0.0', - 'description' => '', - 'author' => '', - 'dist' => [], - 'require' => [], - ], - [ - 'name' => 'fake2', - 'title' => 'Fake', - 'version' => '0.0.0', - 'description' => '', - 'author' => '', - 'dist' => [], - 'require' => [], - ], - ], - ]); + ->push($registry) + ->push($registry); $this->getJson('/admin/plugins/market/list')->assertStatus(500); $this->mock(PluginManager::class, function ($mock) { $mock->shouldReceive('get') ->with('fake1') + ->atLeast() ->once() ->andReturn(new Plugin('', ['name' => 'fake1', 'version' => '0.0.1'])); $mock->shouldReceive('get') ->with('fake2') + ->atLeast() ->once() ->andReturn(null); - $mock->shouldReceive('getUnsatisfied')->twice(); + $mock->shouldReceive('getUnsatisfied')->atLeast()->once(); }); $this->getJson('/admin/plugins/market/list') ->assertJsonStructure([ @@ -134,5 +140,22 @@ public function testMarketData() 'dependencies', ], ]); + + // with fallback locale + app()->setLocale('es_ES'); + $this->getJson('/admin/plugins/market/list') + ->assertJsonStructure([ + [ + 'name', + 'title', + 'version', + 'installed', + 'description', + 'author', + 'dist', + 'dependencies', + ], + ]); + app()->setLocale('en'); } }