From 2a84246f0124143e7c9bec673dfbc9cbf66d2911 Mon Sep 17 00:00:00 2001 From: James Salter Date: Mon, 16 May 2022 11:15:54 +0100 Subject: [PATCH 1/5] Fall back to translation input rather than showing missing translation --- src/languageHandler.tsx | 16 ++++++++++++++++ test/i18n-test/languageHandler-test.tsx | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/languageHandler.tsx b/src/languageHandler.tsx index 6dfef6d1aeb..4b257449fc1 100644 --- a/src/languageHandler.tsx +++ b/src/languageHandler.tsx @@ -90,6 +90,22 @@ const translateWithFallback = (text: string, options?: object): { translated?: s const translated = counterpart.translate(text, { ...options, fallbackLocale: counterpart.getLocale() }); if (!translated || /^missing translation:/.test(translated)) { const fallbackTranslated = counterpart.translate(text, { ...options, locale: FALLBACK_LOCALE }); + if (!fallbackTranslated || /^missing translation:/.test(translated) && 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 }; diff --git a/test/i18n-test/languageHandler-test.tsx b/test/i18n-test/languageHandler-test.tsx index b2349f7d1d8..ea40b8b2c60 100644 --- a/test/i18n-test/languageHandler-test.tsx +++ b/test/i18n-test/languageHandler-test.tsx @@ -11,6 +11,10 @@ import { import { stubClient } from '../test-utils'; describe('languageHandler', function() { + /* + See /__mocks__/browser-request.js/ for how we are stubbing out translations + to provide fixture data for these tests + */ const basicString = 'Rooms'; const selfClosingTagSub = 'Accept to continue:'; const textInTagSub = 'Upgrade to your own domain'; @@ -19,6 +23,7 @@ describe('languageHandler', function() { type TestCase = [string, string, Record, Record, TranslatedString]; const testCasesEn: TestCase[] = [ + // description of the test case, translationString, variables, tags, expected result ['translates a basic string', basicString, {}, undefined, 'Rooms'], [ 'handles plurals when count is 0', @@ -201,4 +206,17 @@ describe('languageHandler', function() { }); }); }); + + describe('when languages dont load', () => { + it('_t', async () => { + const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary"; + expect(_t(STRING_NOT_IN_THE_DICTIONARY, {}, undefined)).toEqual(STRING_NOT_IN_THE_DICTIONARY); + }); + + it('_tDom', async () => { + const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary"; + expect(_t(STRING_NOT_IN_THE_DICTIONARY, {}, undefined)).toEqual( + { STRING_NOT_IN_THE_DICTIONARY }); + }); + }); }); From 4505b00bbf47d450953325c1cb99fb07523c8caa Mon Sep 17 00:00:00 2001 From: James Salter Date: Mon, 16 May 2022 11:35:00 +0100 Subject: [PATCH 2/5] use startsWith --- src/languageHandler.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/languageHandler.tsx b/src/languageHandler.tsx index 4b257449fc1..e75bc2aec44 100644 --- a/src/languageHandler.tsx +++ b/src/languageHandler.tsx @@ -88,9 +88,9 @@ 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 || /^missing translation:/.test(translated) && process.env.NODE_ENV !== "development") { + 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 From 251f4580b1ee92e5a16732572c21078b43786072 Mon Sep 17 00:00:00 2001 From: James Salter Date: Mon, 16 May 2022 11:36:05 +0100 Subject: [PATCH 3/5] break line --- src/languageHandler.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/languageHandler.tsx b/src/languageHandler.tsx index e75bc2aec44..7d1c6fdddda 100644 --- a/src/languageHandler.tsx +++ b/src/languageHandler.tsx @@ -90,7 +90,8 @@ const translateWithFallback = (text: string, options?: object): { translated?: s const translated = counterpart.translate(text, { ...options, fallbackLocale: counterpart.getLocale() }); 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") { + 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 From e0476168db6d2ae772d7852ecac2a85498c80386 Mon Sep 17 00:00:00 2001 From: James Salter Date: Mon, 16 May 2022 11:44:45 +0100 Subject: [PATCH 4/5] Update snapshot tests --- .../__snapshots__/KeyboardShortcut-test.tsx.snap | 6 +++--- .../KeyboardUserSettingsTab-test.tsx.snap | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/components/views/settings/__snapshots__/KeyboardShortcut-test.tsx.snap b/test/components/views/settings/__snapshots__/KeyboardShortcut-test.tsx.snap index e4468c802f0..23062d79809 100644 --- a/test/components/views/settings/__snapshots__/KeyboardShortcut-test.tsx.snap +++ b/test/components/views/settings/__snapshots__/KeyboardShortcut-test.tsx.snap @@ -32,7 +32,7 @@ exports[`KeyboardShortcut doesn't render same modifier twice 1`] = ` > - missing translation: en|Ctrl + Ctrl + @@ -70,7 +70,7 @@ exports[`KeyboardShortcut doesn't render same modifier twice 2`] = ` > - missing translation: en|Ctrl + Ctrl + @@ -95,7 +95,7 @@ exports[`KeyboardShortcut renders alternative key name 1`] = ` > - missing translation: en|Page Down + Page Down + diff --git a/test/components/views/settings/tabs/user/__snapshots__/KeyboardUserSettingsTab-test.tsx.snap b/test/components/views/settings/tabs/user/__snapshots__/KeyboardUserSettingsTab-test.tsx.snap index caeea350f7b..71bab7f6129 100644 --- a/test/components/views/settings/tabs/user/__snapshots__/KeyboardUserSettingsTab-test.tsx.snap +++ b/test/components/views/settings/tabs/user/__snapshots__/KeyboardUserSettingsTab-test.tsx.snap @@ -8,7 +8,7 @@ exports[`KeyboardUserSettingsTab renders list of keyboard shortcuts 1`] = `
- missing translation: en|Keyboard + Keyboard
- missing translation: en|Composer + Composer
@@ -59,7 +59,7 @@ exports[`KeyboardUserSettingsTab renders list of keyboard shortcuts 1`] = ` > - missing translation: en|Ctrl + Ctrl + @@ -103,7 +103,7 @@ exports[`KeyboardUserSettingsTab renders list of keyboard shortcuts 1`] = ` > - missing translation: en|Ctrl + Ctrl + @@ -145,7 +145,7 @@ exports[`KeyboardUserSettingsTab renders list of keyboard shortcuts 1`] = `
- missing translation: en|Navigation + Navigation
@@ -173,7 +173,7 @@ exports[`KeyboardUserSettingsTab renders list of keyboard shortcuts 1`] = ` > - missing translation: en|Enter + Enter From b2670977a556f0acdd0b735bf5bcc45b11c4f607 Mon Sep 17 00:00:00 2001 From: James Salter Date: Mon, 16 May 2022 11:47:12 +0100 Subject: [PATCH 5/5] Fix second test and add parentheses --- src/languageHandler.tsx | 2 +- test/i18n-test/languageHandler-test.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/languageHandler.tsx b/src/languageHandler.tsx index 7d1c6fdddda..0b33cd6a185 100644 --- a/src/languageHandler.tsx +++ b/src/languageHandler.tsx @@ -90,7 +90,7 @@ const translateWithFallback = (text: string, options?: object): { translated?: s const translated = counterpart.translate(text, { ...options, fallbackLocale: counterpart.getLocale() }); if (!translated || translated.startsWith("missing translation:")) { const fallbackTranslated = counterpart.translate(text, { ...options, locale: FALLBACK_LOCALE }); - if (!fallbackTranslated || fallbackTranslated.startsWith("missing translation:") + if ((!fallbackTranslated || fallbackTranslated.startsWith("missing translation:")) && process.env.NODE_ENV !== "development") { // Even the translation via FALLBACK_LOCALE failed; this can happen if // diff --git a/test/i18n-test/languageHandler-test.tsx b/test/i18n-test/languageHandler-test.tsx index d0ec881c559..9c15bfd3feb 100644 --- a/test/i18n-test/languageHandler-test.tsx +++ b/test/i18n-test/languageHandler-test.tsx @@ -231,7 +231,7 @@ describe('languageHandler', function() { it('_tDom', async () => { const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary"; - expect(_t(STRING_NOT_IN_THE_DICTIONARY, {}, undefined)).toEqual( + expect(_tDom(STRING_NOT_IN_THE_DICTIONARY, {}, undefined)).toEqual( { STRING_NOT_IN_THE_DICTIONARY }); }); });