-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding translator class to handle multiple languages (#2131)
* Adding translator class to handle multiple languages * Cleanup translator.js wrong sectioned comments --------- Co-authored-by: Fu Zhen <[email protected]>
- Loading branch information
Showing
6 changed files
with
164 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"distancetool": { | ||
"start": "Start", | ||
"units": { | ||
"mile": " mi", | ||
"feet": " ft", | ||
"kilometer": " km", | ||
"meter": " m" | ||
} | ||
}, | ||
"areatool": { | ||
"units": { | ||
"mile": " mi²", | ||
"feet": " ft²", | ||
"kilometer": " km²", | ||
"meter": " m²" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"distancetool": { | ||
"start": "Inicio", | ||
"units": { | ||
"mile": " mi", | ||
"feet": " ft", | ||
"kilometer": " km", | ||
"meter": " m" | ||
} | ||
}, | ||
"areatool": { | ||
"units": { | ||
"mile": " mi²", | ||
"feet": " ft²", | ||
"kilometer": " km²", | ||
"meter": " m²" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"distancetool": { | ||
"start": "起点", | ||
"units": { | ||
"mile": " 米", | ||
"feet": " 公里", | ||
"kilometer": " 英尺", | ||
"meter": " 英里" | ||
} | ||
}, | ||
"areatool": { | ||
"units": { | ||
"mile": " 平方英里", | ||
"feet": " 平方英尺", | ||
"kilometer": " 平方公里", | ||
"meter": " 平方米" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import Class from '../core/Class'; | ||
|
||
import zhCn from '../lang/languages/zh-CN.json'; | ||
import esMX from '../lang/languages/es-MX.json'; | ||
import enUS from '../lang/languages/en-US.json'; | ||
|
||
/** | ||
* Maptalks text's language | ||
*/ | ||
export class TranslatorError extends Error { | ||
constructor(msg) { | ||
super('Translator: ' + msg); | ||
this.name = 'TranslatorError'; | ||
} | ||
} | ||
|
||
class Translator extends Class { | ||
constructor(lang) { | ||
super(); | ||
|
||
this.languages = { | ||
'zh-CN': zhCn, | ||
'es-MX': esMX, | ||
'en-US': enUS | ||
}; | ||
this.nodes = {}; | ||
this.setLang(lang || 'zh-CN'); | ||
} | ||
|
||
/** | ||
* Method to update the language of maptalks | ||
* @param {string} lang - Available Langs (zh-CN, en-US, es-MX) | ||
* @example setLang('zh-CN') | ||
*/ | ||
setLang(lang) { | ||
const newLanguageNodes = this.languages[lang]; | ||
if (!newLanguageNodes) throw new TranslatorError('Setted Lang does not exist'); | ||
this.nodes = newLanguageNodes; | ||
} | ||
|
||
_validateNestedProps(nestedProps) { | ||
nestedProps.forEach(p => { | ||
if (p === '') throw new TranslatorError('Any of sides of a dot "." cannot be empty'); | ||
}); | ||
} | ||
/** | ||
* method to return the text of the current language available on lang json's | ||
* @param {string} textNode - Accesible property with the current language text. | ||
* @return {string} Text to show in screen | ||
* @example document.write(translate('areatool.units.kilometer')) | ||
*/ | ||
translate(textNode = null) { | ||
if (textNode == null) | ||
throw new TranslatorError('Missing parameter textNode'); | ||
if (typeof textNode === 'string') { | ||
let translatedText = null; | ||
if (textNode.includes('.')) { | ||
const nestedProps = textNode.split('.'); | ||
if (nestedProps.length > 3) throw new TranslatorError(`Translate function can only access through 3 nested properties, trying to access -> ${nestedProps.length}`); | ||
this._validateNestedProps(nestedProps); | ||
|
||
try { | ||
let translatedText = null; | ||
switch (nestedProps.length) { | ||
case 2 : | ||
translatedText = this.nodes[nestedProps[0]][nestedProps[1]]; | ||
break; | ||
case 3 : | ||
translatedText = this.nodes[nestedProps[0]][nestedProps[1]][nestedProps[2]]; | ||
break; | ||
} | ||
return translatedText; | ||
} catch (err) { | ||
throw new TranslatorError('Unable to find the text translated in lang json' + err.message); | ||
} | ||
} else { | ||
translatedText = this.nodes[textNode]; | ||
return translatedText; | ||
} | ||
} else { | ||
throw new TranslatorError('Param passed has to be a String'); | ||
} | ||
} | ||
} | ||
|
||
export default Translator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters