simple-translation
Advanced tools
{ | ||
"name": "simple-translation", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A super basic and simple way to do language translations. Auto detects language from the browser. Supports compiled strings. Aims to be an uncomplicated translation package", | ||
@@ -5,0 +5,0 @@ "main": "simple-translation.js", |
@@ -11,3 +11,4 @@ | ||
- Supports static and dynamic strings (can pass variables into text) | ||
- Decent error handling | ||
- Basic error handling | ||
- Minimal, you should be able to fork it and modify it without much investment | ||
@@ -54,7 +55,7 @@ | ||
Option A - *specify each language file individually using the registerLanguage() method* | ||
**Option A** - *specify each language file individually using the registerLanguage() method* | ||
```translate.registerLanguage(english)``` | ||
Option B - *specify the language files upon new translate, which will be automatically registered* | ||
**Option B** - *specify the language files upon new translate, which will be automatically registered* | ||
@@ -78,2 +79,12 @@ ```let translate = new SimpleTranslation(english, french)``` | ||
# Additional Helper Methods | ||
## Get list of supported Languages | ||
This method would be handy if you want to, for example, render a drop down list of supported languages in your app and then allow the user to choose which language to display. | ||
```translate.getSupportedLanguages()``` | ||
returns: ```["en", "fr"]``` | ||
## Get entire language object | ||
@@ -90,1 +101,10 @@ ```translate.getLocale('en')``` | ||
}``` | ||
---------- | ||
*author: Richard Bettridge (ssshake)* | ||
*web: http://daggasoft.com* | ||
*twitter: @richbettridge* |
@@ -26,16 +26,27 @@ export default class SimpleTranslation{ | ||
getLocale(laguageCode){ | ||
if (!this.localeData[laguageCode]){ | ||
console.error(`Simple-Translation: Language definition of '${laguageCode}' Not Found`) | ||
getLocale(languageCode){ | ||
if (!this.localeData[languageCode]){ | ||
console.error(`Simple-Translation: Language definition of '${languageCode}' Not Found`) | ||
return false | ||
} | ||
return this.localeData[laguageCode] | ||
return this.localeData[languageCode] | ||
} | ||
getSupportedLanguages(){ | ||
return Object.keys(this.localeData) | ||
} | ||
message(key, languageCode = this.browserLanguageCode){ | ||
if (!this.getLocale(languageCode)){ | ||
return `<span style="color:red;">Missing Translation File '${languageCode}'</span>` | ||
} | ||
if (!this.localeData[languageCode].messages[key]){ | ||
console.error(`Simple-Translation: The message '${key}' for language code '${languageCode}' was not found`) | ||
return '<span style="color:red;">Missing Translation</span>' | ||
return `<span style="color:red;">Missing Translation for '${key}' for language '${this.localeData[languageCode].language}'</span>` | ||
} | ||
return this.localeData[languageCode].messages[key] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
9041
34.12%60
13.21%107
22.99%