A web-based drawing app
git clone git://soophie.de/outline
blob: 6f1c09f37c1ca8bb411ccc02352bf266380ad882 (2944 bytes)[doc][blame][raw]
1
import { LOCALES } from '../constant.js';3
const LINE_BREAK_PATTERN = /\r\n|\r|\n/;4
const PROPERTY_PATTERN = /^([\w+.-]+)=([^=]*)$/;6
let lang = {};7
let default_lang;8
let current_lang;10
/**11
Sets the default and current language.13
@function init14
@constructor15
*/16
function init() {17
let code = 'en';18
if(typeof navigator !== 'undefined') {19
if(LOCALES.includes(navigator.language)) {20
code = navigator.language;21
}22
else {23
for(let locale of navigator.languages) {24
if(/^\w{2}-\w{2}$/.test(locale)) {25
locale = locale.substr(0, 2);26
}27
if(LOCALES.includes(locale)) {28
code = locale;29
break;30
}31
}32
}33
}34
default_lang = code;35
current_lang = code;36
}38
/**39
Reads all properties files in the given directory and specified locale code.41
@async42
@function load43
@param {string} path - The path to the locale files.44
@param {string[]} locales - The list containing all locales.45
@return {Object} The locale object.46
*/47
async function load(path, locales, callback) {48
for(let code of locales) {49
let text = await fetch(`${path}/${code}.properties`)50
.then(function(response) {51
if(!response.ok) {52
throw new Error('(404) Not Found');53
}54
return response.text();55
})56
.catch(function(error) {57
console.error(`No properties file found for locale '${code}'!`, error);58
});59
if(text) {60
lang[code] = parse(text);61
}62
}63
if(callback) {64
callback();65
}66
change(current_lang);67
}69
/**70
Converts properties text to a locale object.72
@function parse73
@param {string} text - Raw properties text data.74
@return {Object} The locale object.75
*/76
function parse(text) {77
let result = {};78
for(let line of text.split(LINE_BREAK_PATTERN)) {79
let match = PROPERTY_PATTERN.exec(line);80
if(match) {81
result[match[1]] = match[2];82
}83
}84
return result;85
}87
/**88
Returns the current locale.90
@function get_current91
@return {string} The current locale.92
*/93
function get_current() {94
return current_lang;95
}97
/**98
Returns the locale object by the given locale code.100
@function change101
@return {Object} The locale object.102
*/103
function get() {104
return lang[current_lang];105
}107
/**108
Changes language text from all HTMLElements with the specified datatag.110
@function change111
@param {string} code - The local code.112
*/113
function change(code) {114
if(lang[code]) {115
current_lang = code;116
let elements = document.querySelectorAll('[data-lang]');117
for(let item of elements) {118
let content = lang[code][item.dataset.lang];119
if(content) {120
let type = item.dataset.type || 'content';121
let badge;122
if(item.dataset.badge === 'true') {123
badge = item.querySelector('.badge');124
}125
switch(type) {126
case 'content':127
item.textContent = content;128
if(badge) {129
item.append(badge);130
}131
break;132
case 'tooltip':133
item.dataset.title = content;134
break;135
default:136
break;137
}138
}139
}140
}141
}143
export default {144
init,145
load,146
parse,147
get_current,148
get,149
change150
};