From 60d47931112cb98e3efb644bdf57dbb1bee0659e Mon Sep 17 00:00:00 2001 From: gaba Date: Fri, 2 Dec 2016 16:12:24 -0800 Subject: [PATCH 1/4] Adds a variable to the possible translations. --- client/coral-framework/modules/i18n/i18n.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/client/coral-framework/modules/i18n/i18n.js b/client/coral-framework/modules/i18n/i18n.js index afe8606d1..a4a22f5b8 100644 --- a/client/coral-framework/modules/i18n/i18n.js +++ b/client/coral-framework/modules/i18n/i18n.js @@ -46,9 +46,11 @@ class i18n { * it takes a string with the translation key and returns * the translation value or the key itself if not found * it works with nested translations (my.page.title) + * + * the second parameter is optional and replaces a variable marked by {0} in the translation. */ - this.t = (key) => { + this.t = (key, att) => { const arr = key.split('.'); let translation = this.translations; try { @@ -58,8 +60,9 @@ class i18n { return key; } - const val = String(translation); + let val = String(translation); if (val) { + val = val.replace('/\{0\/g', att); return val; } else { console.warn(`${key} language key not set`); From 31c87cb93f018357eb81da5789fee0b36cab16f9 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Tue, 6 Dec 2016 14:17:01 -1000 Subject: [PATCH 2/4] make the translation function accept n arguments. ninja bugfix to permalink --- .../coral-embed-stream/src/CommentStream.js | 2 +- client/coral-framework/modules/i18n/i18n.js | 26 ++++++++----------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/client/coral-embed-stream/src/CommentStream.js b/client/coral-embed-stream/src/CommentStream.js index 0fda9444d..bb1d4e6e0 100644 --- a/client/coral-embed-stream/src/CommentStream.js +++ b/client/coral-embed-stream/src/CommentStream.js @@ -70,7 +70,7 @@ class CommentStream extends Component { const path = /https?\:\/\/([^?#]+)/.exec(this.pym.parentUrl); this.props.getStream(path[1] || window.location); - this.path = path; + this.path = path[1] || window.location; this.pym.sendMessage('childReady'); } diff --git a/client/coral-framework/modules/i18n/i18n.js b/client/coral-framework/modules/i18n/i18n.js index a4a22f5b8..2bab752db 100644 --- a/client/coral-framework/modules/i18n/i18n.js +++ b/client/coral-framework/modules/i18n/i18n.js @@ -1,5 +1,7 @@ import timeago from 'timeago.js'; import esTA from '../../../../node_modules/timeago.js/locales/es'; +import has from 'lodash/has'; +import get from 'lodash/get'; /** * Default locales, this should be overriden by config file @@ -47,23 +49,17 @@ class i18n { * the translation value or the key itself if not found * it works with nested translations (my.page.title) * - * the second parameter is optional and replaces a variable marked by {0} in the translation. + * any extra parameters are optional and replace a variable marked by {0}, {1}, etc in the translation. */ - this.t = (key, att) => { - const arr = key.split('.'); - let translation = this.translations; - try { - for (let i = 0; i < arr.length; i++) {translation = translation[arr[i]];} - } catch (error) { - console.warn(`${key} language key not set`); - return key; - } - - let val = String(translation); - if (val) { - val = val.replace('/\{0\/g', att); - return val; + this.t = (key, ...replacements) => { + if (has(this.translations, key)) { + let translation = get(this.translations, key); + // replace any {n} with the arguments passed to this method + replacements.forEach((str, i) => { + translation = translation.replace(`{${i}}`, str); + }); + return translation; } else { console.warn(`${key} language key not set`); return key; From f2ded3f2a7daabbc6041671471cded0da57653cb Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Tue, 6 Dec 2016 14:34:27 -1000 Subject: [PATCH 3/4] replace things multiple times --- client/coral-framework/modules/i18n/i18n.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/coral-framework/modules/i18n/i18n.js b/client/coral-framework/modules/i18n/i18n.js index 2bab752db..1d81e885c 100644 --- a/client/coral-framework/modules/i18n/i18n.js +++ b/client/coral-framework/modules/i18n/i18n.js @@ -57,7 +57,7 @@ class i18n { let translation = get(this.translations, key); // replace any {n} with the arguments passed to this method replacements.forEach((str, i) => { - translation = translation.replace(`{${i}}`, str); + translation = translation.replace(new RegExp(`\\{${i}\\}`, 'g'), str); }); return translation; } else { From 12011965667963c57bd7a4bd0f7983555aac2c65 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Tue, 6 Dec 2016 15:51:22 -1000 Subject: [PATCH 4/4] un-sneaking in some stuff --- client/coral-embed-stream/src/CommentStream.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/coral-embed-stream/src/CommentStream.js b/client/coral-embed-stream/src/CommentStream.js index bb1d4e6e0..0fda9444d 100644 --- a/client/coral-embed-stream/src/CommentStream.js +++ b/client/coral-embed-stream/src/CommentStream.js @@ -70,7 +70,7 @@ class CommentStream extends Component { const path = /https?\:\/\/([^?#]+)/.exec(this.pym.parentUrl); this.props.getStream(path[1] || window.location); - this.path = path[1] || window.location; + this.path = path; this.pym.sendMessage('childReady'); }