You've already forked matrix-react-sdk
mirror of
https://github.com/matrix-org/matrix-react-sdk.git
synced 2025-11-08 21:42:24 +03:00
fallback properly with pluralized strings (#7495)
Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
@@ -41,6 +41,7 @@ counterpart.setSeparator('|');
|
||||
|
||||
// see `translateWithFallback` for an explanation of fallback handling
|
||||
const FALLBACK_LOCALE = 'en';
|
||||
counterpart.setFallbackLocale(FALLBACK_LOCALE);
|
||||
|
||||
interface ITranslatableError extends Error {
|
||||
translatedMessage: string;
|
||||
@@ -79,12 +80,12 @@ export function _td(s: string): string { // eslint-disable-line @typescript-esli
|
||||
* should be wrapped with an appropriate `lang='en'` attribute
|
||||
* counterpart's `translate` doesn't expose a way to determine if the resulting translation
|
||||
* is in the target locale or a fallback locale
|
||||
* for this reason, we do not set a fallback via `counterpart.setFallbackLocale`
|
||||
* for this reason, force fallbackLocale === locale in the first call to translate
|
||||
* and fallback 'manually' so we can mark fallback strings appropriately
|
||||
* */
|
||||
const translateWithFallback = (text: string, options?: object): { translated?: string, isFallback?: boolean } => {
|
||||
const translated = counterpart.translate(text, options);
|
||||
if (/^missing translation:/.test(translated)) {
|
||||
const translated = counterpart.translate(text, { ...options, fallbackLocale: counterpart.getLocale() });
|
||||
if (!translated || /^missing translation:/.test(translated)) {
|
||||
const fallbackTranslated = counterpart.translate(text, { ...options, locale: FALLBACK_LOCALE });
|
||||
return { translated: fallbackTranslated, isFallback: true };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user