so://phie

outline

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

blob: 462273705d49a1626ef936379799d732ffeafea7 (3063 bytes)[doc][blame][raw]

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

2

3import cookie from './cookie.js';

4

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

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

7

8let lang = {};

9let current_lang;

10

11/**

12 Sets the default and current language.

13

14 @function init

15 @constructor

16*/

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

44

45/**

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

47

48 @async

49 @function load

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

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

72

73/**

74 Converts properties text to a locale object.

75

76 @function parse

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

78 @return {Object} The locale object.

79*/

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

90

91/**

92 Returns the current locale.

93

94 @function get_current

95 @return {string} The current locale.

96*/

97function get_current() {

98 return current_lang;

99}

100

101/**

102 Returns the locale object by the given locale code.

103

104 @function change

105 @return {Object} The locale object.

106*/

107function get() {

108 return lang[current_lang];

109}

110

111/**

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

113

114 @function change

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

116*/

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

146

147export default {

148 init,

149 load,

150 parse,

151 get_current,

152 get,

153 change

154};