A web-based drawing app
git clone git://soophie.de/outline
blob: 462273705d49a1626ef936379799d732ffeafea7 (3063 bytes)[doc][blame][raw]
1
import { LOCALES } from '../constant.js';3
import cookie from './cookie.js';5
const LINE_BREAK_PATTERN = /\r\n|\r|\n/;6
const PROPERTY_PATTERN = /^([\w+.-]+)=([^=]*)$/;8
let lang = {};9
let current_lang;11
/**12
Sets the default and current language.14
@function init15
@constructor16
*/17
function init() {18
let code = 'en';19
if(cookie.hasAccepted()) {20
let lang = localStorage.getItem('outline.custom.lang');21
if(lang && LOCALES.includes(lang)) {22
current_lang = lang;23
return;24
}25
}26
if(typeof navigator !== 'undefined') {27
if(LOCALES.includes(navigator.language)) {28
code = navigator.language;29
}30
else {31
for(let locale of navigator.languages) {32
if(/^\w{2}-\w{2}$/.test(locale)) {33
locale = locale.substr(0, 2);34
}35
if(LOCALES.includes(locale)) {36
code = locale;37
break;38
}39
}40
}41
}42
current_lang = code;43
}45
/**46
Reads all properties files in the given directory and specified locale code.48
@async49
@function load50
@param {string} path - The path to the locale files.51
@param {string[]} locales - The list containing all locales.52
@return {Object} The locale object.53
*/54
async function load(path, locales) {55
for(let code of locales) {56
let text = await fetch(`${path}/${code}.properties`)57
.then(function(response) {58
if(!response.ok) {59
throw new Error('(404) Not Found');60
}61
return response.text();62
})63
.catch(function(error) {64
console.error(`No properties file found for locale '${code}'!`, error);65
});66
if(text) {67
lang[code] = parse(text);68
}69
}70
change(current_lang);71
}73
/**74
Converts properties text to a locale object.76
@function parse77
@param {string} text - Raw properties text data.78
@return {Object} The locale object.79
*/80
function parse(text) {81
let result = {};82
for(let line of text.split(LINE_BREAK_PATTERN)) {83
let match = PROPERTY_PATTERN.exec(line);84
if(match) {85
result[match[1]] = match[2];86
}87
}88
return result;89
}91
/**92
Returns the current locale.94
@function get_current95
@return {string} The current locale.96
*/97
function get_current() {98
return current_lang;99
}101
/**102
Returns the locale object by the given locale code.104
@function change105
@return {Object} The locale object.106
*/107
function get() {108
return lang[current_lang];109
}111
/**112
Changes language text from all HTMLElements with the specified datatag.114
@function change115
@param {string} code - The local code.116
*/117
function change(code) {118
if(lang[code]) {119
current_lang = code;120
let elements = document.querySelectorAll('[data-lang]');121
for(let item of elements) {122
let content = lang[code][item.dataset.lang];123
if(content) {124
let type = item.dataset.type || 'content';125
let badge;126
if(item.dataset.badge === 'true') {127
badge = item.querySelector('.badge');128
}129
switch(type) {130
case 'content':131
item.textContent = content;132
if(badge) {133
item.append(badge);134
}135
break;136
case 'tooltip':137
item.dataset.title = content;138
break;139
default:140
break;141
}142
}143
}144
}145
}147
export default {148
init,149
load,150
parse,151
get_current,152
get,153
change154
};