so://phie

outline

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

blob: 6f1c09f37c1ca8bb411ccc02352bf266380ad882 (2944 bytes)[doc][blame][raw]

1import { LOCALES } from '../constant.js';

2

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

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

5

6let lang = {};

7let default_lang;

8let current_lang;

9

10/**

11 Sets the default and current language.

12

13 @function init

14 @constructor

15*/

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

37

38/**

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

40

41 @async

42 @function load

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

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

68

69/**

70 Converts properties text to a locale object.

71

72 @function parse

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

74 @return {Object} The locale object.

75*/

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

86

87/**

88 Returns the current locale.

89

90 @function get_current

91 @return {string} The current locale.

92*/

93function get_current() {

94 return current_lang;

95}

96

97/**

98 Returns the locale object by the given locale code.

99

100 @function change

101 @return {Object} The locale object.

102*/

103function get() {

104 return lang[current_lang];

105}

106

107/**

108 Changes language text from all HTMLElements with the specified datatag.

109

110 @function change

111 @param {string} code - The local code.

112*/

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

142

143export default {

144 init,

145 load,

146 parse,

147 get_current,

148 get,

149 change

150};