-
Notifications
You must be signed in to change notification settings - Fork 29
/
i18n.js
127 lines (107 loc) · 4.67 KB
/
i18n.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Copyright (c) 2014-2015 F-Secure
See LICENSE for details
*/
'use strict';
var logger = require('./logger');
var fs = require('fs');
// Implementation of i18n module
var I18N = function () {
var _getDefaultLanguage = function () {
return 'en-US';
};
this.getDefaultLanguage = function () {
return _getDefaultLanguage();
};
var languages = [
_getDefaultLanguage(),
'ru-RU',
'fi-FI',
'es-ES'
];
this.languages = languages;
var loadStrings = function () {
var languageStrings = {};
for (var l in languages) {
if (languages.hasOwnProperty(l)) {
var lang = languages[l];
var file = './i18n/resources-locale_' + lang + '.json';
try {
var data = fs.readFileSync(file, 'utf8');
languageStrings[lang] = JSON.parse(data);
} catch (err) {
logger.warn('Failed to load or parse localization file: ' + file + '. Error: ' + err.message);
}
}
}
return languageStrings;
};
var strings = loadStrings();
var formatString = function (text, fieldName1, fieldValue1, fieldName2, fieldValue2, fieldName3, fieldValue3) {
var textBeforeReplace;
if (fieldName1 !== undefined && fieldValue1 !== undefined) {
textBeforeReplace = text;
text = text.replace('%' + fieldName1 + '%', fieldValue1);
if (textBeforeReplace === text) {
logger.warn('LOC: string [' + text + '] does not have field %' + fieldName1 + '% to replace to ' + fieldValue1);
}
}
if (fieldName2 !== undefined && fieldValue2 !== undefined) {
textBeforeReplace = text;
text = text.replace('%' + fieldName2 + '%', fieldValue2);
if (textBeforeReplace === text) {
logger.warn('LOC: string [' + text + '] does not have field %' + fieldName2 + '% to replace to ' + fieldValue2);
}
}
if (fieldName3 !== undefined && fieldValue3 !== undefined) {
textBeforeReplace = text;
text = text.replace('%' + fieldName3 + '%', fieldValue3);
if (textBeforeReplace === text) {
logger.warn('LOC: string [' + text + '] does not have field %' + fieldName3 + '% to replace to ' + fieldValue3);
}
}
return text;
};
// Returns localized string from language lang and with key key.
// If fieldNameX and fieldValueX are defined then replaces %fieldNameX% with fieldValueX.
// For instance for "notification.userHasBeenInvitedBy": "%userName% has been invited to your family by %invitingUserName%":
// getLocalizedString("en-US", "notification.userHasBeenInvitedBy", "userName", "Oleg", "invitingUserName", "Miguel") returns:
// "Oleg has been invited to your family by Miguel"
this.getLocalizedString = function (lang, key, fieldName1, fieldValue1, fieldName2, fieldValue2, fieldName3, fieldValue3) {
if (!strings.hasOwnProperty(lang)) {
if (typeof lang === 'string' && lang.length === 2) {
// Compatibility for 2 letter language codes, grab first full code with matching 2 first letters.
var foundLang = false;
for (var i = 0; i < languages.length; i++) {
var langCode = languages[i];
if (langCode.substring(0, 2) === lang) {
foundLang = true;
lang = langCode;
break;
}
}
if (!foundLang) {
logger.warn('LOC: unknown language: ' + lang + ' requested string: ' + key);
lang = _getDefaultLanguage();
}
} else {
logger.warn('LOC: unknown language: ' + lang + ' requested string: ' + key);
lang = _getDefaultLanguage();
}
}
if (!strings[lang].hasOwnProperty(key)) {
// fallback to default language
if (!strings[_getDefaultLanguage()].hasOwnProperty(key)) {
logger.warn('LOC: unknown string requested: ' + key);
return '';
}
return formatString(strings[_getDefaultLanguage()][key], fieldName1, fieldValue1, fieldName2, fieldValue2, fieldName3, fieldValue3);
}
return formatString(strings[lang][key], fieldName1, fieldValue1, fieldName2, fieldValue2, fieldName3, fieldValue3);
};
// INTERNAL USE ONLY
this._addString = function (lang, key, string) {
strings[lang][key] = string;
};
};
module.exports = I18N;