-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLocalization.js
More file actions
127 lines (101 loc) · 3.03 KB
/
Localization.js
File metadata and controls
127 lines (101 loc) · 3.03 KB
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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-purple; icon-glyph: language;
const { Files } = importModule("Files");
/**
* Helper class used to get translations
* for the key based on the current system language.
*
* @class Locale
*/
class Locale {
static #DEFAULT_LOCALE = "en";
static #instance;
#translations;
constructor() {
this.#translations = this.#loadTranslations();
}
/**
* Used to get instance of Locale..
*
* @static
* @return {Locale} instance of locale class
* @memberof Locale
*/
static getInstance() {
if (!this.#instance) {
this.#instance = new Locale();
}
return this.#instance;
}
/**
* Used to retrieve translation for provided key
* based on the current system language.
*
* @static
* @param {String} key text resource key
* @param {String} args text resource argument list
* @return {String} translation corresponding provided key
* @memberof Locale
*/
tr(key, args) {
let translation = this.#translations[key];
if (translation === undefined) {
console.warn(`Translation with key '${key}' doesn't exist.`);
return "";
}
// Update all value placeholders.
for (let argId = 0; argId < args.length; argId++) {
translation = translation.replaceAll(`%${argId + 1}`, args[argId]);
}
return translation;
}
/**
* Loads all available translations
* for user locale (or default - en - if doesn't exists).
*
* @static
* @memberof Locale
*/
#loadTranslations() {
let translations = {};
const languageCode = this.#getLanguageCode();
const localeDirectories = Files.findLocaleDirectories();
for (const directory of localeDirectories) {
let localeContent = {};
const customLocaleExists = Files.localeExists(directory, languageCode);
const defaultLocaleExists = Files.localeExists(directory, Locale.#DEFAULT_LOCALE);
if (customLocaleExists) {
localeContent = Files.readLocale(directory, languageCode);
} else if (defaultLocaleExists) {
localeContent = Files.readLocale(directory, Locale.#DEFAULT_LOCALE);
}
translations = {
...translations,
...localeContent
};
}
return translations;
}
/**
* Used to get current system
* language code.
*
* Value is taken by analyzing user preferred
* languages.
*
* @static
* @return {String} language code
* @memberof Locale
*/
#getLanguageCode() {
const locale = Device.preferredLanguages()[0];
return locale.substring(0, locale.indexOf('-'));
}
}
function tr(key, ...args) {
return Locale.getInstance().tr(key, args);
}
module.exports = {
tr
};