feat(multi-language): Add multi-language support and routing updates
Add configuration for multiple languages in the Website Manager
This commit is contained in:
@@ -7,6 +7,17 @@ $pageTranslationServiceConfig = [
|
||||
'language' => 'en',
|
||||
],
|
||||
];
|
||||
$pageTranslationServiceSettings = ['languages' => ['en', 'fr']];
|
||||
$pageTranslationServiceUrl = '/page';
|
||||
|
||||
class PageTranslationTestSettings
|
||||
{
|
||||
public static function get(string $key): mixed
|
||||
{
|
||||
global $pageTranslationServiceSettings;
|
||||
return $pageTranslationServiceSettings[$key] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
function phpb_config(?string $key = null): mixed
|
||||
{
|
||||
@@ -22,10 +33,66 @@ function phpb_config(?string $key = null): mixed
|
||||
}
|
||||
$value = $value[$segment];
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
function phpb_trans(string $key): mixed
|
||||
{
|
||||
$languages = ['en' => 'English', 'fr' => 'French'];
|
||||
return $key === 'languages' ? $languages : ($languages[substr($key, 10)] ?? '');
|
||||
}
|
||||
|
||||
function phpb_instance(string $name, array $params = []): mixed
|
||||
{
|
||||
return PageTranslationTestSettings::class;
|
||||
}
|
||||
|
||||
function phpb_current_relative_url(bool $removeParameters = false): string
|
||||
{
|
||||
global $pageTranslationServiceUrl;
|
||||
return $pageTranslationServiceUrl;
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/../src/Core/helpers.php';
|
||||
require_once __DIR__ . '/../src/Contracts/SettingRepositoryContract.php';
|
||||
require_once __DIR__ . '/../src/Repositories/BaseRepository.php';
|
||||
require_once __DIR__ . '/../src/Repositories/SettingRepository.php';
|
||||
require_once __DIR__ . '/../src/Contracts/SettingContract.php';
|
||||
require_once __DIR__ . '/../src/Setting.php';
|
||||
require_once __DIR__ . '/../src/Plugi.php';
|
||||
|
||||
class PageTranslationTestSettingRepository extends Plugi\Repositories\SettingRepository
|
||||
{
|
||||
public array $saved = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = new class {
|
||||
public function transaction(callable $callback): mixed
|
||||
{
|
||||
return $callback();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public function findWhere($column, $value): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function create(array $data): object
|
||||
{
|
||||
$this->saved[$data['setting']] = $data;
|
||||
return new stdClass();
|
||||
}
|
||||
|
||||
protected function update($instance, array $data): bool
|
||||
{
|
||||
$this->saved[$data['setting']] = $data;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function assertPageTranslationService(bool $condition, string $message): void
|
||||
{
|
||||
if (! $condition) {
|
||||
@@ -81,6 +148,108 @@ $cases = [
|
||||
'Locale validation should accept active languages only and must not apply fallback.'
|
||||
);
|
||||
},
|
||||
'routes normalize slash variants and static-segment case' => function () use ($service): void {
|
||||
assertPageTranslationService(
|
||||
$service->normalizeRoute('/About//US/') === '/about/us'
|
||||
&& $service->normalizeRoute('/About/{ProductID}/') === '/about/{ProductID}'
|
||||
&& $service->getRouteKey('/ABOUT/{productid}') === $service->getRouteKey('/about/{ProductID}'),
|
||||
'Route normalization should canonicalize slashes and case without changing parameter names.'
|
||||
);
|
||||
},
|
||||
'generated route prefix follows the default-language convention' => function () use ($service): void {
|
||||
assertPageTranslationService(
|
||||
$service->getGeneratedRoutePrefix('en', true) === ''
|
||||
&& $service->getGeneratedRoutePrefix('de', false) === '/de',
|
||||
'Generated routes should leave the default locale unprefixed and prefix later locales.'
|
||||
);
|
||||
},
|
||||
'matched page locale takes precedence over URL inference' => function (): void {
|
||||
global $pageTranslationServiceUrl, $phpb_request_language;
|
||||
$pageTranslationServiceUrl = '/page';
|
||||
$phpb_request_language = 'fr';
|
||||
|
||||
assertPageTranslationService(
|
||||
phpb_current_language() === 'fr',
|
||||
'The matched translation locale should be authoritative on resolved page requests.'
|
||||
);
|
||||
unset($GLOBALS['phpb_request_language']);
|
||||
},
|
||||
'unresolved requests retain URL and configured-default fallback' => function (): void {
|
||||
global $pageTranslationServiceUrl, $pageTranslationServiceConfig, $phpb_request_language;
|
||||
unset($GLOBALS['phpb_request_language']);
|
||||
$pageTranslationServiceConfig['general']['language'] = 'en';
|
||||
$pageTranslationServiceUrl = '/fr/page';
|
||||
assertPageTranslationService(
|
||||
phpb_current_language() === 'fr',
|
||||
'An unresolved locale-prefixed URL should retain URL-based language detection.'
|
||||
);
|
||||
|
||||
$pageTranslationServiceUrl = '/page';
|
||||
assertPageTranslationService(
|
||||
phpb_current_language() === 'en',
|
||||
'An unresolved URL without a locale prefix should fall back to the configured default.'
|
||||
);
|
||||
},
|
||||
'default-language settings reject a disabled locale' => function (): void {
|
||||
$settingRepository = new PageTranslationTestSettingRepository();
|
||||
|
||||
assertPageTranslationService(
|
||||
! $settingRepository->updateSettings([
|
||||
'languages' => ['en'],
|
||||
'default_language' => 'fr',
|
||||
]) && $settingRepository->saved === [],
|
||||
'A default locale must be among enabled languages and invalid settings must not be persisted.'
|
||||
);
|
||||
},
|
||||
'default-language settings accept an enabled locale' => function (): void {
|
||||
$settingRepository = new PageTranslationTestSettingRepository();
|
||||
|
||||
assertPageTranslationService(
|
||||
$settingRepository->updateSettings([
|
||||
'languages' => ['en', 'fr'],
|
||||
'default_language' => 'fr',
|
||||
]) && ($settingRepository->saved['default_language']['value'] ?? null) === 'fr',
|
||||
'A selected default locale should be saved when it is enabled.'
|
||||
);
|
||||
},
|
||||
'legacy language input cannot disable an existing default' => function (): void {
|
||||
global $pageTranslationServiceSettings;
|
||||
$pageTranslationServiceSettings['default_language'] = 'en';
|
||||
$settingRepository = new PageTranslationTestSettingRepository();
|
||||
|
||||
assertPageTranslationService(
|
||||
! $settingRepository->updateSettings([
|
||||
'languages' => ['fr'],
|
||||
'admin_language' => 'fr',
|
||||
]) && $settingRepository->saved === [],
|
||||
'Legacy language input must not allow disabling the saved site default without replacement.'
|
||||
);
|
||||
unset($pageTranslationServiceSettings['default_language']);
|
||||
},
|
||||
'new default language takes precedence over the legacy setting' => function (): void {
|
||||
$settings = new ReflectionProperty(Plugi\Setting::class, 'settings');
|
||||
$settings->setValue(null, ['default_language' => 'fr', 'admin_language' => 'en']);
|
||||
$GLOBALS['phpb_config'] = ['general' => ['language' => 'en'], 'theme' => []];
|
||||
|
||||
(new Plugi\Plugi([]))->hydrateConfig();
|
||||
|
||||
assertPageTranslationService(
|
||||
$GLOBALS['phpb_config']['general']['language'] === 'fr',
|
||||
'The distinct default-language setting should take precedence over the legacy setting.'
|
||||
);
|
||||
},
|
||||
'legacy admin language remains a compatibility fallback' => function (): void {
|
||||
$settings = new ReflectionProperty(Plugi\Setting::class, 'settings');
|
||||
$settings->setValue(null, ['admin_language' => 'fr']);
|
||||
$GLOBALS['phpb_config'] = ['general' => ['language' => 'en'], 'theme' => []];
|
||||
|
||||
(new Plugi\Plugi([]))->hydrateConfig();
|
||||
|
||||
assertPageTranslationService(
|
||||
$GLOBALS['phpb_config']['general']['language'] === 'fr',
|
||||
'Installations without a saved default-language setting should continue using admin_language.'
|
||||
);
|
||||
},
|
||||
'translation values follow the same fallback order and preserve empty strings' => function () use ($service): void {
|
||||
global $pageTranslationServiceConfig;
|
||||
$pageTranslationServiceConfig['general']['language'] = 'fr';
|
||||
|
||||
Reference in New Issue
Block a user