allow l10n of plugins market registry

This commit is contained in:
Pig Fang 2020-04-07 16:09:30 +08:00
parent f0e3b6a866
commit 1669269ca3
3 changed files with 65 additions and 27 deletions

View File

@ -76,8 +76,13 @@ class MarketController extends Controller
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));

View File

@ -33,6 +33,16 @@ return [
*/
'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'],
];

View File

@ -17,8 +17,9 @@ class MarketControllerTest extends TestCase
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 @@ class MarketControllerTest extends TestCase
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 @@ class MarketControllerTest extends TestCase
'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');
}
}