A web-based drawing app
git clone git://soophie.de/outline
blob: a181e1944cd3f54dfd11e6f71ea3391115c026eb (1196 bytes)[doc][blame][raw]
1
const LINE_BREAK_PATTERN = /\r\n|\r|\n/;2
const PROPERTY_PATTERN = /^([\w+.-]+)=([^=]*)$/;4
/**5
Reads all properties files in the given directory and specified locale code.7
@async8
@function load9
@param {string} path - The path to the locale files.10
@param {string[]} locales - The list containing all locales.11
@return {Object} The locale object.12
*/13
async function load(path, locales) {14
let result = {};15
for(let code of locales) {16
let text = await fetch(`${path}/${code}.properties`)17
.then(function(response) {18
if(!response.ok) {19
throw new Error('(404) Not Found');20
}21
return response.text();22
})23
.catch(function(error) {24
console.error(`No properties file found for locale '${code}'!`, error);25
});26
if(text) {27
result[code] = parse(text);28
}29
}30
return result;31
}33
/**34
Converts properties text to a locale object.36
@function parse37
@param {string} text - Raw properties text data.38
@return {Object} The locale object.39
*/40
function parse(text) {41
let result = {};42
for(let line of text.split(LINE_BREAK_PATTERN)) {43
let match = PROPERTY_PATTERN.exec(line);44
if(match) {45
result[match[1]] = match[2];46
}47
}48
return result;49
}51
export { load, parse };