so://phie

outline

A web-based drawing app
git clone git://soophie.de/outline

blob: a181e1944cd3f54dfd11e6f71ea3391115c026eb (1196 bytes)[doc][blame][raw]

1const LINE_BREAK_PATTERN = /\r\n|\r|\n/;

2const PROPERTY_PATTERN = /^([\w+.-]+)=([^=]*)$/;

3

4/**

5 Reads all properties files in the given directory and specified locale code.

6

7 @async

8 @function load

9 @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*/

13async 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}

32

33/**

34 Converts properties text to a locale object.

35

36 @function parse

37 @param {string} text - Raw properties text data.

38 @return {Object} The locale object.

39*/

40function 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}

50

51export { load, parse };