Commit a6ecf788 authored by Sandro Lutz's avatar Sandro Lutz
Browse files

Add language model documentation

parent 3aa66cf1
......@@ -5,6 +5,14 @@ import english from '../languages/en.json';
let _currentLanguage;
/**
* Change the current language.
*
* Sets the current lanugage to the given code if it is
* a valid code or to the default language `en` otherwise.
*
* @param {string} language two-letter language code
*/
function changeLanguage(language) {
i18n.translator.reset();
if (language === 'de') {
......@@ -17,10 +25,22 @@ function changeLanguage(language) {
localStorage.setItem('lanuage', _currentLanguage);
}
/**
* Check if a given language code is valid.
*
* @param {string} language two-letter language code
* @return `true` - if valid; `false` - otherwise
*/
function isLanguageValid(language) {
return ['en', 'de'].indexOf(language) > -1;
}
/**
* Load language from `LocalStorage` or from the browser
* language preference header.
*
* This function sets the current language accordingly.
*/
function loadLanguage() {
let lang = localStorage.getItem('lanuage');
if (!lang) {
......@@ -37,6 +57,13 @@ function loadLanguage() {
}
}
/**
* Switch the language of the current page.
*
* Behavior:
* - `en` => `de`
* - `de` => `en`
*/
function switchLanguage() {
if (_currentLanguage === 'en') {
_currentLanguage = 'de';
......@@ -46,6 +73,11 @@ function switchLanguage() {
m.route.set(`/${_currentLanguage}${m.route.get().substring(3)}`);
}
/**
* Get the current language.
*
* @return two-letter language code
*/
function currentLanguage() {
if (!_currentLanguage) {
loadLanguage();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment