From f64f0b548ebf10aed421becdb2607d458aa06693 Mon Sep 17 00:00:00 2001 From: Victor Vuelma Date: Fri, 19 Mar 2021 10:05:03 -0300 Subject: [PATCH 1/2] feature: add i18n support on distance and length text with "pt" language --- src/config.js | 2 ++ src/i18n.js | 2 ++ src/index.js | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/config.js b/src/config.js index d035d48..f8311e3 100644 --- a/src/config.js +++ b/src/config.js @@ -19,4 +19,6 @@ export const Config = { getProp(langCode, 'tooltipText1'), tooltipText2: (langCode = defaultLangCode) => getProp(langCode, 'tooltipText2'), + distanceAndLength: (langCode = defaultLangCode) => + getProp(langCode, 'distanceAndLength'), }; diff --git a/src/i18n.js b/src/i18n.js index 0a9cf7c..5297cea 100644 --- a/src/i18n.js +++ b/src/i18n.js @@ -2,6 +2,7 @@ const i18n = { en: { tooltipText1: 'Drag to change, click to remove', tooltipText2: 'Drag to change', + distanceAndLength: (lenght, area) => `Total distance: ${lenght}; Total area: ${area}.` }, it: { tooltipText1: 'Trascina per modificare, fai clic per rimuovere', @@ -26,6 +27,7 @@ const i18n = { pt: { tooltipText1: 'Arraste para alterar, clique para remover', tooltipText2: 'Arraste para alterar', + distanceAndLength: (lenght, area) => `Distância total: ${lenght}; Área total: ${area}.` }, }; diff --git a/src/index.js b/src/index.js index 1ff50a2..17dfc52 100644 --- a/src/index.js +++ b/src/index.js @@ -1024,7 +1024,7 @@ export default class MeasureTool { this._nodeText .select(':last-child') .text( - `Total distance: ${this.lengthText}; Total area: ${this.areaText}.` + Config.distanceAndLength(this._options.language)(this.lengthText, this.areaText) ); } } From d499507d3dc274b4c5db4d0202e892ced931ce34 Mon Sep 17 00:00:00 2001 From: Victor Vuelma Date: Tue, 4 May 2021 11:21:52 -0300 Subject: [PATCH 2/2] add fallback to default language if prop doesnt exists --- src/config.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/config.js b/src/config.js index f8311e3..65c91d7 100644 --- a/src/config.js +++ b/src/config.js @@ -3,13 +3,15 @@ import i18n from './i18n'; const defaultLangCode = 'en'; const getProp = (langCode, prop) => { - if (i18n[langCode]) { + if (i18n[langCode] && i18n[langCode][prop]) { return i18n[langCode][prop]; } + const code = langCode.split('-')[0]; - if (i18n[code]) { + if (i18n[code] && i18n[code][prop]) { return i18n[code][prop]; } + return i18n[defaultLangCode][prop]; };