forked from Markus-Rost/discord-wiki-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.js
258 lines (247 loc) · 7.56 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const {defaultSettings} = require('./default.json');
const i18n = require('../i18n/allLangs.json');
Object.keys(i18n.allLangs.names).forEach( lang => i18n[lang] = require('../i18n/' + lang + '.json') );
const defaultAliases = ( i18n?.[defaultSettings.lang]?.aliases || {} );
/**
* A language.
* @class
*/
export default class Lang {
/**
* Creates a new language.
* @param {String} [lang] - The language code.
* @param {String} [namespace] - The namespace for the language.
* @constructs Lang
*/
constructor(lang = defaultSettings.lang, namespace = '') {
if ( typeof lang !== 'string' ) lang = defaultSettings.lang;
else if ( lang === 'allLangs' || !i18n.hasOwnProperty(lang) ) {
if ( i18n.allLangs.map.hasOwnProperty(lang.toLowerCase()) ) {
lang = i18n.allLangs.map[lang.toLowerCase()];
}
else lang = defaultSettings.lang;
}
this.lang = lang;
this.namespace = namespace;
this.fallback = ( i18n?.[lang]?.fallback.slice() || [defaultSettings.lang] ).filter( fb => fb.trim() );
this.localNames = {};
this.aliases = {};
let aliases = ( i18n?.[lang]?.aliases || {} );
Object.keys(aliases).forEach( cmd => {
if ( aliases[cmd][0].trim() && !this.localNames.hasOwnProperty(cmd) ) {
this.localNames[cmd] = aliases[cmd][0];
}
aliases[cmd].forEach( alias => {
if ( alias.trim() && !this.aliases.hasOwnProperty(alias) ) this.aliases[alias] = cmd;
} );
} );
Object.keys(defaultAliases).forEach( cmd => {
if ( defaultAliases[cmd][0].trim() && !this.localNames.hasOwnProperty(cmd) ) {
this.localNames[cmd] = defaultAliases[cmd][0];
}
defaultAliases[cmd].forEach( alias => {
if ( alias.trim() && !this.aliases.hasOwnProperty(alias) ) this.aliases[alias] = cmd;
} );
} );
}
/**
* Change the message language.
* @param {String[]} languageOverwrites - Arguments for the message.
* @returns {Lang}
*/
uselang(...languageOverwrites) {
languageOverwrites = languageOverwrites.map( lang => {
if ( typeof lang !== 'string' ) return;
if ( lang === 'allLangs' || !i18n.hasOwnProperty(lang) ) {
if ( /^[a-z]{2}(?:-[a-z]{2,4})?$/.test(lang) && i18n.allLangs.map.hasOwnProperty(lang) ) {
return i18n.allLangs.map[lang];
}
return;
}
return lang;
} ).filter( lang => lang );
if ( !languageOverwrites.length || ( languageOverwrites.length === 1 && languageOverwrites[0] === this.lang ) ) return this;
var newLang = new Lang(this.lang, this.namespace);
newLang.fallback.unshift(...languageOverwrites.slice(1), newLang.lang);
newLang.lang = languageOverwrites[0];
return newLang;
}
/**
* Get a localized message.
* @param {String} message - Name of the message.
* @param {String[]} args - Arguments for the message.
* @returns {String}
*/
get(message = '', ...args) {
if ( this.namespace.length ) message = this.namespace + '.' + message;
let keys = ( message.length ? message.split('.') : [] );
let lang = this.lang;
let text = i18n?.[lang];
let fallback = 0;
for ( let n = 0; n < keys.length; n++ ) {
if ( text ) {
text = text?.[keys[n]];
if ( typeof text === 'string' ) text = text.trim();
}
if ( !text ) {
if ( fallback < this.fallback.length ) {
lang = this.fallback[fallback];
fallback++;
text = i18n?.[lang];
n = -1;
}
else {
n = keys.length;
}
}
}
if ( typeof text === 'string' ) {
args.forEach( (arg, i) => {
text = text.replaceAllSafe( `$${i + 1}`, arg );
} );
if ( text.includes( 'GENDER:' ) ) text = text.replace( /{{\s*GENDER:\s*([a-z]+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, type, cases) => {
return gender(type, cases.split(/\s*\|\s*/));
} );
if ( text.includes( 'PLURAL:' ) ) text = text.replace( /{{\s*PLURAL:\s*[+-]?(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
} );
}
return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
}
// /**
// * Get a localized message.
// * @param {String} message - Name of the message.
// * @param {String[]} args - Arguments for the message.
// * @returns {String}
// */
// get(message = '', ...args) {
// if ( this.namespace.length ) message = this.namespace + '.' + message;
// let lang = this.lang;
// let text = i18n?.[lang]?.[message];
// let fallback = 0;
// while ( !text ) {
// if ( fallback < this.fallback.length ) {
// lang = this.fallback[fallback];
// fallback++;
// text = i18n?.[lang]?.[message];
// }
// else break;
// }
// if ( typeof text === 'string' ) {
// args.forEach( (arg, i) => {
// text = text.replaceAllSafe( `$${i + 1}`, arg );
// } );
// if ( text.includes( 'GENDER:' ) ) text = text.replace( /{{\s*GENDER:\s*([a-z]+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, type, cases) => {
// return gender(type, cases.split(/\s*\|\s*/));
// } );
// if ( text.includes( 'PLURAL:' ) ) text = text.replace( /{{\s*PLURAL:\s*[+-]?(\d+)\s*\|\s*([^\{\}]*?)\s*}}/g, (m, number, cases) => {
// return plural(lang, parseInt(number, 10), cases.split(/\s*\|\s*/));
// } );
// }
// return ( text || '⧼' + message + ( isDebug && args.length ? ': ' + args.join(', ') : '' ) + '⧽' );
// }
/**
* Get names for all languages.
* @param {Boolean} isRcGcDw - Get the languages for RcGcDw?
* @static
*/
static allLangs(isRcGcDw = false) {
if ( isRcGcDw ) return i18n.RcGcDw;
return i18n.allLangs;
}
}
/**
* Parse gender text.
* @param {String} gender - The gender.
* @param {String[]} args - The possible text.
* @returns {String}
*/
function gender(gender, args) {
var text = args[0];
switch ( gender ) {
case 'male':
if ( args.length > 0 ) text = args[0];
break;
case 'female':
if ( args.length > 1 ) text = args[1];
break;
case 'unknown':
default:
if ( args.length > 2 ) text = args[2];
}
return text;
}
/**
* Parse plural text.
* @param {String} lang - The language code.
* @param {Number} number - The amount.
* @param {String[]} args - The possible text.
* @returns {String}
*/
function plural(lang, number, args) {
// https://translatewiki.net/wiki/Plural/Mediawiki_plural_rules
var text = args[args.length - 1];
switch ( lang ) {
case 'fr':
case 'hi':
if ( number <= 1 ) text = getArg(args, 0);
else text = getArg(args, 1);
break;
case 'pl':
if ( number === 1 ) text = getArg(args, 0);
else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
text = getArg(args, 1);
}
else text = getArg(args, 2);
break;
case 'ru':
case 'sr':
case 'uk':
if ( args.length > 2 ) {
if ( number % 10 === 1 && number % 100 !== 11 ) text = getArg(args, 0);
else if ( [2,3,4].includes( number % 10 ) && ![12,13,14].includes( number % 100 ) ) {
text = getArg(args, 1);
}
else text = getArg(args, 2);
}
else {
if ( number === 1 ) text = getArg(args, 0);
else text = getArg(args, 1);
}
break;
case 'bn':
case 'de':
case 'el':
case 'en':
case 'es':
case 'id':
case 'it':
case 'ja':
case 'ko':
case 'lzh':
case 'nl':
case 'pt-br':
case 'th':
case 'sv':
case 'tr':
case 'vi':
case 'zh-hans':
case 'zh-hant':
default:
if ( number === 1 ) text = getArg(args, 0);
else text = getArg(args, 1);
}
return text;
}
/**
* Get text option.
* @param {String[]} args - The list of options.
* @param {Number} index - The preferred option.
* @returns {String}
*/
function getArg(args, index) {
return ( args.length > index ? args[index] : args[args.length - 1] );
}
export const allLangs = Lang.allLangs;