Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit fcc4d62

Browse files
authored
Fall back to untranslated string rather than showing missing translation error (#8609)
1 parent fb30b67 commit fcc4d62

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

src/languageHandler.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,25 @@ export function _td(s: string): string { // eslint-disable-line @typescript-esli
8888
* */
8989
const translateWithFallback = (text: string, options?: object): { translated?: string, isFallback?: boolean } => {
9090
const translated = counterpart.translate(text, { ...options, fallbackLocale: counterpart.getLocale() });
91-
if (!translated || /^missing translation:/.test(translated)) {
91+
if (!translated || translated.startsWith("missing translation:")) {
9292
const fallbackTranslated = counterpart.translate(text, { ...options, locale: FALLBACK_LOCALE });
93+
if ((!fallbackTranslated || fallbackTranslated.startsWith("missing translation:"))
94+
&& process.env.NODE_ENV !== "development") {
95+
// Even the translation via FALLBACK_LOCALE failed; this can happen if
96+
//
97+
// 1. The string isn't in the translations dictionary, usually because you're in develop
98+
// and haven't run yarn i18n
99+
// 2. Loading the translation resources over the network failed, which can happen due to
100+
// to network or if the client tried to load a translation that's been removed from the
101+
// server.
102+
//
103+
// At this point, its the lesser evil to show the untranslated text, which
104+
// will be in English, so the user can still make out *something*, rather than an opaque
105+
// "missing translation" error.
106+
//
107+
// Don't do this in develop so people remember to run yarn i18n.
108+
return { translated: text, isFallback: true };
109+
}
93110
return { translated: fallbackTranslated, isFallback: true };
94111
}
95112
return { translated };

test/components/views/settings/__snapshots__/KeyboardShortcut-test.tsx.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ exports[`KeyboardShortcut doesn't render same modifier twice 1`] = `
3232
>
3333
<kbd>
3434
35-
missing translation: en|Ctrl
35+
Ctrl
3636
3737
</kbd>
3838
+
@@ -70,7 +70,7 @@ exports[`KeyboardShortcut doesn't render same modifier twice 2`] = `
7070
>
7171
<kbd>
7272
73-
missing translation: en|Ctrl
73+
Ctrl
7474
7575
</kbd>
7676
+
@@ -95,7 +95,7 @@ exports[`KeyboardShortcut renders alternative key name 1`] = `
9595
>
9696
<kbd>
9797
98-
missing translation: en|Page Down
98+
Page Down
9999
100100
</kbd>
101101
+

test/components/views/settings/tabs/user/__snapshots__/KeyboardUserSettingsTab-test.tsx.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports[`KeyboardUserSettingsTab renders list of keyboard shortcuts 1`] = `
88
<div
99
className="mx_SettingsTab_heading"
1010
>
11-
missing translation: en|Keyboard
11+
Keyboard
1212
</div>
1313
<KeyboardShortcutSection
1414
category={
@@ -30,7 +30,7 @@ exports[`KeyboardUserSettingsTab renders list of keyboard shortcuts 1`] = `
3030
<div
3131
className="mx_SettingsTab_subheading"
3232
>
33-
missing translation: en|Composer
33+
Composer
3434
</div>
3535
<div>
3636
@@ -59,7 +59,7 @@ exports[`KeyboardUserSettingsTab renders list of keyboard shortcuts 1`] = `
5959
>
6060
<kbd>
6161
62-
missing translation: en|Ctrl
62+
Ctrl
6363
6464
</kbd>
6565
+
@@ -103,7 +103,7 @@ exports[`KeyboardUserSettingsTab renders list of keyboard shortcuts 1`] = `
103103
>
104104
<kbd>
105105
106-
missing translation: en|Ctrl
106+
Ctrl
107107
108108
</kbd>
109109
+
@@ -145,7 +145,7 @@ exports[`KeyboardUserSettingsTab renders list of keyboard shortcuts 1`] = `
145145
<div
146146
className="mx_SettingsTab_subheading"
147147
>
148-
missing translation: en|Navigation
148+
Navigation
149149
</div>
150150
<div>
151151
@@ -173,7 +173,7 @@ exports[`KeyboardUserSettingsTab renders list of keyboard shortcuts 1`] = `
173173
>
174174
<kbd>
175175
176-
missing translation: en|Enter
176+
Enter
177177
178178
</kbd>
179179
</KeyboardKey>

test/i18n-test/languageHandler-test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import {
2727
import { stubClient } from '../test-utils';
2828

2929
describe('languageHandler', function() {
30+
/*
31+
See /__mocks__/browser-request.js/ for how we are stubbing out translations
32+
to provide fixture data for these tests
33+
*/
3034
const basicString = 'Rooms';
3135
const selfClosingTagSub = 'Accept <policyLink /> to continue:';
3236
const textInTagSub = '<a>Upgrade</a> to your own domain';
@@ -35,6 +39,7 @@ describe('languageHandler', function() {
3539

3640
type TestCase = [string, string, Record<string, unknown>, Record<string, unknown>, TranslatedString];
3741
const testCasesEn: TestCase[] = [
42+
// description of the test case, translationString, variables, tags, expected result
3843
['translates a basic string', basicString, {}, undefined, 'Rooms'],
3944
[
4045
'handles plurals when count is 0',
@@ -217,4 +222,17 @@ describe('languageHandler', function() {
217222
});
218223
});
219224
});
225+
226+
describe('when languages dont load', () => {
227+
it('_t', async () => {
228+
const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary";
229+
expect(_t(STRING_NOT_IN_THE_DICTIONARY, {}, undefined)).toEqual(STRING_NOT_IN_THE_DICTIONARY);
230+
});
231+
232+
it('_tDom', async () => {
233+
const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary";
234+
expect(_tDom(STRING_NOT_IN_THE_DICTIONARY, {}, undefined)).toEqual(
235+
<span lang="en">{ STRING_NOT_IN_THE_DICTIONARY }</span>);
236+
});
237+
});
220238
});

0 commit comments

Comments
 (0)