From 0af89f7d323b0b3beb91ed9a3a18be1e5e3b9321 Mon Sep 17 00:00:00 2001 From: Pppery Date: Wed, 19 Jun 2024 15:54:09 -0400 Subject: [PATCH] Add fallback languages for locale files Summary: Upstream version of https://gerrit.wikimedia.org/r/c/phabricator/translations/+/1047593 Test Plan: Set any languages and observe untranslated strings display proper PLURAL rules With the downstream "translations" extension installed, set the language to traditional Chinese and see Simplified Chinese rather than English translations if they exist, like "Foo added/removed a project" Reviewers: O1 Blessed Committers, avivey Reviewed By: O1 Blessed Committers, avivey Subscribers: avivey, tobiaswiese, valerio.bozzolan, Matthew, Cigaryno Differential Revision: https://we.phorge.it/D25695 --- src/internationalization/PhutilLocale.php | 5 ++++- .../locales/PhutilPortugueseBrazilLocale.php | 9 +++++++++ .../locales/PhutilPortuguesePortugalLocale.php | 10 ++++++++++ .../locales/PhutilSimplifiedChineseLocale.php | 9 +++++++++ .../locales/PhutilTraditionalChineseLocale.php | 4 ++++ .../locales/PhutilUSEnglishLocale.php | 6 ++++++ 6 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/internationalization/PhutilLocale.php b/src/internationalization/PhutilLocale.php index 3ef2524e..5ed03a92 100644 --- a/src/internationalization/PhutilLocale.php +++ b/src/internationalization/PhutilLocale.php @@ -30,11 +30,14 @@ abstract class PhutilLocale extends Phobject { * For locales like "English (Great Britain)", missing translations can be * sourced from "English (US)". * + * Languages with no other fallback use en_US because that's better + * than proto-English for untranslated strings. + * * @return string|null Locale code of fallback locale, or null if there is * no fallback locale. */ public function getFallbackLocaleCode() { - return null; + return 'en_US'; } diff --git a/src/internationalization/locales/PhutilPortugueseBrazilLocale.php b/src/internationalization/locales/PhutilPortugueseBrazilLocale.php index 2c1ffbea..259ed541 100644 --- a/src/internationalization/locales/PhutilPortugueseBrazilLocale.php +++ b/src/internationalization/locales/PhutilPortugueseBrazilLocale.php @@ -13,4 +13,13 @@ final class PhutilPortugueseBrazilLocale extends PhutilLocale { return pht('Portuguese (Brazil)'); } + public function getFallbackLocaleCode() { + // Phabricator does not support bidirectional + // fallbacks (pt_BR -> pt and pt -> pt_BR simultaneously) + // since Translatewiki calls pt_PT "Portugese" without a country + // it makes slightly more sense to fall back in this direction + // than the other one + return 'pt_PT'; + } + } diff --git a/src/internationalization/locales/PhutilPortuguesePortugalLocale.php b/src/internationalization/locales/PhutilPortuguesePortugalLocale.php index f893b77f..f5a9eb71 100644 --- a/src/internationalization/locales/PhutilPortuguesePortugalLocale.php +++ b/src/internationalization/locales/PhutilPortuguesePortugalLocale.php @@ -13,4 +13,14 @@ final class PhutilPortuguesePortugalLocale extends PhutilLocale { return pht('Portuguese (Portugal)'); } + public function getFallbackLocaleCode() { + // Ideally this would be pt_BR but Phabricator does not support + // bidirectional fallbacks (pt_BR -> pt and pt -> pt_BR simultaneously) + // since Translatewiki calls pt_PT "Portugese" without a country + // it makes slightly more sense to fall back in the other direction + // In the mean time return `en_US` so users don't see Proto-English + // unncecessarily + return 'en_US'; + } + } diff --git a/src/internationalization/locales/PhutilSimplifiedChineseLocale.php b/src/internationalization/locales/PhutilSimplifiedChineseLocale.php index 6b0c709b..4f763323 100644 --- a/src/internationalization/locales/PhutilSimplifiedChineseLocale.php +++ b/src/internationalization/locales/PhutilSimplifiedChineseLocale.php @@ -13,4 +13,13 @@ final class PhutilSimplifiedChineseLocale extends PhutilLocale { return pht('Chinese (Simplified)'); } + public function getFallbackLocaleCode() { + // Ideally this would be zh_Hant but Phabricator does not support + // bidirectional fallbacks + // (zh_Hant -> zh_Hans and zh_Hans -> zh_Hant simultaneously) + // arbitrarily choose to fall back in the other direction instead + // In the mean time return `en_US` so users don't see Proto-English + return 'en_US'; + } + } diff --git a/src/internationalization/locales/PhutilTraditionalChineseLocale.php b/src/internationalization/locales/PhutilTraditionalChineseLocale.php index f8879506..19ca3edb 100644 --- a/src/internationalization/locales/PhutilTraditionalChineseLocale.php +++ b/src/internationalization/locales/PhutilTraditionalChineseLocale.php @@ -13,4 +13,8 @@ final class PhutilTraditionalChineseLocale extends PhutilLocale { return pht('Chinese (Traditional)'); } + public function getFallbackLocaleCode() { + return 'zh_Hans'; + } + } diff --git a/src/internationalization/locales/PhutilUSEnglishLocale.php b/src/internationalization/locales/PhutilUSEnglishLocale.php index ef1633ed..1bd7430c 100644 --- a/src/internationalization/locales/PhutilUSEnglishLocale.php +++ b/src/internationalization/locales/PhutilUSEnglishLocale.php @@ -13,4 +13,10 @@ final class PhutilUSEnglishLocale extends PhutilLocale { return pht('English (US)'); } + public function getFallbackLocaleCode() { + // The default fallback is en_US, explicitly return null here + // to avoid a fallback loop + return null; + } + }