From 5fe0be722b6514692a68e920ee8058c5d572237d Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 15 Mar 2018 21:59:45 -0230 Subject: Handle i18n with redux. --- ui/i18n-helper.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 ui/i18n-helper.js (limited to 'ui/i18n-helper.js') diff --git a/ui/i18n-helper.js b/ui/i18n-helper.js new file mode 100644 index 000000000..7ad8cd040 --- /dev/null +++ b/ui/i18n-helper.js @@ -0,0 +1,37 @@ +// cross-browser connection to extension i18n API +const extension = require('extensionizer') +const log = require('loglevel') + +const getMessage = (locale, key, substitutions) => { + // check locale is loaded + if (!locale) { + // throw new Error('Translator - has not loaded a locale yet.') + return '' + } + // check entry is present + const entry = locale[key] + if (!entry) { + log.error(`Translator - Unable to find value for "${key}"`) + throw new Error(`Translator - Unable to find value for "${key}"`) + } + let phrase = entry.message + // perform substitutions + if (substitutions && substitutions.length) { + phrase = phrase.replace(/\$1/g, substitutions[0]) + if (substitutions.length > 1) { + phrase = phrase.replace(/\$2/g, substitutions[1]) + } + } + return phrase +} + +async function fetchLocale (localeName) { + const response = await fetch(`/_locales/${localeName}/messages.json`) + const locale = await response.json() + return locale +} + +module.exports = { + getMessage, + fetchLocale, +} -- cgit From 09260f9c5e0b2c460a214f00b87c8fafe0470419 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 19 Mar 2018 16:23:54 -0230 Subject: Fixed t() calls where localeMessages is missing; and fix incorrect connect reference. --- ui/i18n-helper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ui/i18n-helper.js') diff --git a/ui/i18n-helper.js b/ui/i18n-helper.js index 7ad8cd040..345e83f00 100644 --- a/ui/i18n-helper.js +++ b/ui/i18n-helper.js @@ -12,7 +12,7 @@ const getMessage = (locale, key, substitutions) => { const entry = locale[key] if (!entry) { log.error(`Translator - Unable to find value for "${key}"`) - throw new Error(`Translator - Unable to find value for "${key}"`) + // throw new Error(`Translator - Unable to find value for "${key}"`) } let phrase = entry.message // perform substitutions -- cgit From 2ddc2cc1fbe5249f70d80e2a74146cb87dcc8421 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 19 Mar 2018 16:53:06 -0230 Subject: Lint fixes. --- ui/i18n-helper.js | 1 - 1 file changed, 1 deletion(-) (limited to 'ui/i18n-helper.js') diff --git a/ui/i18n-helper.js b/ui/i18n-helper.js index 345e83f00..10147b0f6 100644 --- a/ui/i18n-helper.js +++ b/ui/i18n-helper.js @@ -1,5 +1,4 @@ // cross-browser connection to extension i18n API -const extension = require('extensionizer') const log = require('loglevel') const getMessage = (locale, key, substitutions) => { -- cgit From d24a0590d363dbe88d383c8faec8eb28809242f0 Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 21 Mar 2018 22:11:47 -0230 Subject: i18n redux solution doesn't require importing t() and passing state to each t() call; t is just available on props. --- ui/i18n-helper.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'ui/i18n-helper.js') diff --git a/ui/i18n-helper.js b/ui/i18n-helper.js index 10147b0f6..70555f239 100644 --- a/ui/i18n-helper.js +++ b/ui/i18n-helper.js @@ -2,13 +2,15 @@ const log = require('loglevel') const getMessage = (locale, key, substitutions) => { + console.log(`locale, key, substitutions`, locale, key, substitutions); // check locale is loaded if (!locale) { // throw new Error('Translator - has not loaded a locale yet.') return '' } // check entry is present - const entry = locale[key] + const { current, en } = locale + const entry = current[key] || en[key] if (!entry) { log.error(`Translator - Unable to find value for "${key}"`) // throw new Error(`Translator - Unable to find value for "${key}"`) -- cgit From 3c144302d65053fc3082b8b24662fc3e0231ca3a Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 21 Mar 2018 22:57:09 -0230 Subject: Remove console.logs --- ui/i18n-helper.js | 1 - 1 file changed, 1 deletion(-) (limited to 'ui/i18n-helper.js') diff --git a/ui/i18n-helper.js b/ui/i18n-helper.js index 70555f239..dc83f45c9 100644 --- a/ui/i18n-helper.js +++ b/ui/i18n-helper.js @@ -2,7 +2,6 @@ const log = require('loglevel') const getMessage = (locale, key, substitutions) => { - console.log(`locale, key, substitutions`, locale, key, substitutions); // check locale is loaded if (!locale) { // throw new Error('Translator - has not loaded a locale yet.') -- cgit From 0d71dd7ca0c0c3178670a9882c34d180495a7031 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 23 Mar 2018 14:31:15 -0230 Subject: i18n helper fetchLocale handles 404 gracefully --- ui/i18n-helper.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'ui/i18n-helper.js') diff --git a/ui/i18n-helper.js b/ui/i18n-helper.js index dc83f45c9..3ce24ddfb 100644 --- a/ui/i18n-helper.js +++ b/ui/i18n-helper.js @@ -25,10 +25,18 @@ const getMessage = (locale, key, substitutions) => { return phrase } -async function fetchLocale (localeName) { - const response = await fetch(`/_locales/${localeName}/messages.json`) - const locale = await response.json() - return locale +function fetchLocale (localeName) { + return new Promise((resolve, reject) => { + return fetch(`/_locales/${localeName}/messages.json`) + .then(response => response.json()) + .then( + locale => resolve(locale), + error => { + log.error(`failed to fetch ${localeName} locale because of ${error}`) + resolve({}) + } + ) + }) } module.exports = { -- cgit From 8760dc6bdac3e20a426853bc3f26b4b74a7bde95 Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 28 Mar 2018 21:53:08 -0700 Subject: ui - use relative url for i18n-helper fetching locales --- ui/i18n-helper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ui/i18n-helper.js') diff --git a/ui/i18n-helper.js b/ui/i18n-helper.js index 3ce24ddfb..db2fd2dc4 100644 --- a/ui/i18n-helper.js +++ b/ui/i18n-helper.js @@ -27,7 +27,7 @@ const getMessage = (locale, key, substitutions) => { function fetchLocale (localeName) { return new Promise((resolve, reject) => { - return fetch(`/_locales/${localeName}/messages.json`) + return fetch(`./_locales/${localeName}/messages.json`) .then(response => response.json()) .then( locale => resolve(locale), -- cgit From ecbab14cae659cdcec9e59dc0a3f450069a6a05f Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 3 Apr 2018 10:33:10 -0700 Subject: app - warn on fetch errors instead of spamming sentry --- ui/i18n-helper.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'ui/i18n-helper.js') diff --git a/ui/i18n-helper.js b/ui/i18n-helper.js index db2fd2dc4..3eee55ae9 100644 --- a/ui/i18n-helper.js +++ b/ui/i18n-helper.js @@ -25,18 +25,15 @@ const getMessage = (locale, key, substitutions) => { return phrase } -function fetchLocale (localeName) { - return new Promise((resolve, reject) => { - return fetch(`./_locales/${localeName}/messages.json`) - .then(response => response.json()) - .then( - locale => resolve(locale), - error => { - log.error(`failed to fetch ${localeName} locale because of ${error}`) - resolve({}) - } - ) - }) +async function fetchLocale (localeName) { + try { + const response = await fetch(`./_locales/${localeName}/messages.json`) + const locale = await response.json() + return locale + } catch (error) { + log.error(`failed to fetch ${localeName} locale because of ${error}`) + return {} + } } module.exports = { -- cgit