mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
fix(locale): always fall back to English for missing translations
When _APP_LOCALE was set to a partial locale (e.g. fr), Locale fallback
matched that same incomplete set, so missing email keys rendered as
{{emails.*}} instead of English. Keep instance default from _APP_LOCALE
but always fall back to en, which is the complete translation catalog.
Co-authored-by: Chirag Aggarwal <chiragaggarwal5k@gmail.com>
This commit is contained in:
1 parent
2bd1442d33
commit
8a497b9286
6 files changed
+57
-5
No files matched your search
@@ -118,7 +118,10 @@ return function (Container $context): void {
|
||||
|
||||
$context->set('locale', function () {
|
||||
$locale = new Locale(System::getEnv('_APP_LOCALE', 'en'));
|
||||
$locale->setFallback(System::getEnv('_APP_LOCALE', 'en'));
|
||||
// Always fall back to English — it is the only complete translation set.
|
||||
// Using _APP_LOCALE here left missing keys as {{emails.*}} when the
|
||||
// instance default was a partial locale (e.g. fr). See #12448.
|
||||
$locale->setFallback('en');
|
||||
|
||||
return $locale;
|
||||
});
|
||||
|
||||
@@ -61,7 +61,7 @@ class Get extends Action
|
||||
$locale = $locale ?: System::getEnv('_APP_LOCALE', 'en');
|
||||
|
||||
$localeObj = new Locale($locale);
|
||||
$localeObj->setFallback(System::getEnv('_APP_LOCALE', 'en'));
|
||||
$localeObj->setFallback('en');
|
||||
|
||||
$response->dynamic(new Document([
|
||||
'templateId' => $templateId,
|
||||
|
||||
@@ -73,7 +73,7 @@ class Get extends Action
|
||||
|
||||
// Prepare default tempaltes
|
||||
$localeObj = new Locale($locale);
|
||||
$localeObj->setFallback(System::getEnv('_APP_LOCALE', 'en'));
|
||||
$localeObj->setFallback('en');
|
||||
|
||||
$defaultSubject = $localeObj->getText('emails.' . $templateId . '.subject');
|
||||
$defaultMessage = $this->getDefaultMessage($templateId, $localeObj);
|
||||
|
||||
@@ -543,7 +543,7 @@ class Certificates extends Action
|
||||
Console::warning('Cannot renew domain (' . $domain . ') on attempt no. ' . $attempt . ' certificate: ' . $errorMessage);
|
||||
|
||||
$locale = new Locale(System::getEnv('_APP_LOCALE', 'en'));
|
||||
$locale->setFallback(System::getEnv('_APP_LOCALE', 'en'));
|
||||
$locale->setFallback('en');
|
||||
|
||||
// Send mail to administrator mail
|
||||
$template = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-certificate-failed.tpl');
|
||||
|
||||
@@ -978,7 +978,7 @@ class Migrations extends Action
|
||||
}
|
||||
|
||||
$locale = new Locale(System::getEnv('_APP_LOCALE', 'en'));
|
||||
$locale->setFallback(System::getEnv('_APP_LOCALE', 'en'));
|
||||
$locale->setFallback('en');
|
||||
|
||||
$emailType = $success
|
||||
? 'success'
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Locale;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Locale\Locale;
|
||||
|
||||
final class FallbackTest extends TestCase
|
||||
{
|
||||
private string $translationsDir;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->translationsDir = __DIR__ . '/../../../app/config/locale/translations';
|
||||
Locale::$exceptions = false;
|
||||
Locale::setLanguageFromJSON('en', $this->translationsDir . '/en.json');
|
||||
Locale::setLanguageFromJSON('fr', $this->translationsDir . '/fr.json');
|
||||
}
|
||||
|
||||
public function testIncompleteLocaleFallsBackToEnglishForMissingEmailKeys(): void
|
||||
{
|
||||
$locale = new Locale('fr');
|
||||
$locale->setFallback('en');
|
||||
|
||||
$preview = $locale->getText('emails.verification.preview');
|
||||
|
||||
$this->assertNotSame('{{emails.verification.preview}}', $preview);
|
||||
$this->assertSame(
|
||||
'Verify your email to activate your {{project}} account.',
|
||||
$preview
|
||||
);
|
||||
// Keys present in fr stay French.
|
||||
$this->assertSame('Vérification du compte', $locale->getText('emails.verification.subject'));
|
||||
}
|
||||
|
||||
public function testFallbackMatchingIncompleteLocaleLeaksRawKeys(): void
|
||||
{
|
||||
$locale = new Locale('fr');
|
||||
// Mirrors the previous bug: fallback = _APP_LOCALE when that env is fr.
|
||||
$locale->setFallback('fr');
|
||||
|
||||
$this->assertSame(
|
||||
'{{emails.verification.preview}}',
|
||||
$locale->getText('emails.verification.preview')
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user