Fall back to untranslated string rather than showing missing translation error (#8609)

This commit is contained in:
James Salter 2022-05-16 13:28:24 +01:00 committed by GitHub
parent fb30b67b14
commit fcc4d62ede
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 10 deletions

View file

@ -88,8 +88,25 @@ export function _td(s: string): string { // eslint-disable-line @typescript-esli
* */
const translateWithFallback = (text: string, options?: object): { translated?: string, isFallback?: boolean } => {
const translated = counterpart.translate(text, { ...options, fallbackLocale: counterpart.getLocale() });
if (!translated || /^missing translation:/.test(translated)) {
if (!translated || translated.startsWith("missing translation:")) {
const fallbackTranslated = counterpart.translate(text, { ...options, locale: FALLBACK_LOCALE });
if ((!fallbackTranslated || fallbackTranslated.startsWith("missing translation:"))
&& process.env.NODE_ENV !== "development") {
// Even the translation via FALLBACK_LOCALE failed; this can happen if
//
// 1. The string isn't in the translations dictionary, usually because you're in develop
// and haven't run yarn i18n
// 2. Loading the translation resources over the network failed, which can happen due to
// to network or if the client tried to load a translation that's been removed from the
// server.
//
// At this point, its the lesser evil to show the untranslated text, which
// will be in English, so the user can still make out *something*, rather than an opaque
// "missing translation" error.
//
// Don't do this in develop so people remember to run yarn i18n.
return { translated: text, isFallback: true };
}
return { translated: fallbackTranslated, isFallback: true };
}
return { translated };