Skip to content

Commit c52ec3e

Browse files
authored
Guard against counterpart.translate returning non-string values (#29959)
* Guard against counterpart.translate returning non-string values Signed-off-by: Michael Telatynski <[email protected]> * Outdent and fixup comment, remove stale development-only behaviour Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent 785a12a commit c52ec3e

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/languageHandler.tsx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export function _td(s: TranslationKey): TranslationKey {
113113
return s;
114114
}
115115

116+
function isValidTranslation(translated: string): boolean {
117+
return typeof translated === "string" && !translated.startsWith("missing translation:");
118+
}
119+
116120
/**
117121
* to improve screen reader experience translations that are not in the main page language
118122
* eg a translation that fell back to english from another language
@@ -124,30 +128,26 @@ export function _td(s: TranslationKey): TranslationKey {
124128
* */
125129
const translateWithFallback = (text: string, options?: IVariables): { translated: string; isFallback?: boolean } => {
126130
const translated = counterpart.translate(text, { ...options, fallbackLocale: counterpart.getLocale() });
127-
if (!translated || translated.startsWith("missing translation:")) {
128-
const fallbackTranslated = counterpart.translate(text, { ...options, locale: FALLBACK_LOCALE });
129-
if (
130-
(!fallbackTranslated || fallbackTranslated.startsWith("missing translation:")) &&
131-
process.env.NODE_ENV !== "development"
132-
) {
133-
// Even the translation via FALLBACK_LOCALE failed; this can happen if
134-
//
135-
// 1. The string isn't in the translations dictionary, usually because you're in develop
136-
// and haven't run yarn i18n
137-
// 2. Loading the translation resources over the network failed, which can happen due to
138-
// to network or if the client tried to load a translation that's been removed from the
139-
// server.
140-
//
141-
// At this point, its the lesser evil to show the untranslated text, which
142-
// will be in English, so the user can still make out *something*, rather than an opaque
143-
// "missing translation" error.
144-
//
145-
// Don't do this in develop so people remember to run yarn i18n.
146-
return { translated: text, isFallback: true };
147-
}
131+
if (isValidTranslation(translated)) {
132+
return { translated };
133+
}
134+
135+
const fallbackTranslated = counterpart.translate(text, { ...options, locale: FALLBACK_LOCALE });
136+
if (isValidTranslation(fallbackTranslated)) {
148137
return { translated: fallbackTranslated, isFallback: true };
149138
}
150-
return { translated };
139+
140+
// Even the translation via FALLBACK_LOCALE failed; this can happen if
141+
//
142+
// 1. The string isn't in the translations dictionary, usually because you're in develop
143+
// and haven't run yarn i18n
144+
// 2. Loading the translation resources over the network failed, which can happen due to
145+
// to network or if the client tried to load a translation that's been removed from the
146+
// server.
147+
//
148+
// At this point, its the lesser evil to show the i18n key which will be in English but not human-friendly,
149+
// so the user can still make out *something*, rather than an opaque possibly-untranslated "missing translation" error.
150+
return { translated: text, isFallback: true };
151151
};
152152

153153
// Wrapper for counterpart's translation function so that it handles nulls and undefineds properly

0 commit comments

Comments
 (0)