-
Notifications
You must be signed in to change notification settings - Fork 43
/
i18n.js
147 lines (135 loc) · 4.35 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
/*
* Copyright (c) [2023] SUSE LLC
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, contact SUSE LLC.
*
* To contact SUSE LLC about this file by physical or electronic mail, you may
* find current contact information at www.suse.com.
*/
/**
* This is a wrapper module for i18n functions. Currently it uses the cockpit
* implementation but the wrapper allows easy transition to another backend if
* needed.
*/
import cockpit from "./lib/cockpit";
/**
* Tests whether a special testing language is used.
*
* @returns {boolean} true if the testing language is set
*/
const isTestingLanguage = () => cockpit.language === "xx";
/**
* "Translate" the string to special "xx" testing language.
* It just replaces all alpha characters with "x".
* It keeps the percent placeholders like "%s" or "%d" unmodified.
*
* @param {string} str input string
* @returns {string} "translated" string
*/
const xTranslate = (str) => {
let result = "";
let wasPercent = false;
for (let index = 0; index < str.length; index++) {
const char = str[index];
if (wasPercent) {
result += char;
wasPercent = false;
} else {
if (char === "%") {
result += char;
wasPercent = true;
} else {
result += char.replace(/[a-z]/, "x").replace(/[A-Z]/, "X");
}
}
}
return result;
};
/**
* Returns a translated text in the current locale or the original text if the
* translation is not found.
*
* @param {string} str the input string to translate
* @return {string} translated or original text
*/
const _ = (str) => isTestingLanguage() ? xTranslate(str) : cockpit.gettext(str);
/**
* Similar to the _() function. This variant returns singular or plural form
* depending on an additional "num" argument.
*
* @see {@link _} for further information
* @param {string} str1 the input string in the singular form
* @param {string} strN the input string in the plural form
* @param {number} n the actual number which decides whether to use the
* singular or plural form
* @return {string} translated or original text
*/
const n_ = (str1, strN, n) => {
return isTestingLanguage()
? xTranslate((n === 1) ? str1 : strN)
: cockpit.ngettext(str1, strN, n);
};
/**
* This is a no-op function, it can be used only for marking the text for
* translation so it is extracted to the POT file but the text itself is not
* translated. It needs to be translated by the _() function later.
*
* @example <caption>Error messages</caption>
* try {
* ...
* // the exception contains untranslated string
* throw(N_("Download failed"));
* } catch (error) {
* // log the untranslated error
* console.log(error);
* // for users display the translated error
* return <div>Error: {_(error)}</div>;
* }
*
* @example <caption>Constants</caption>
* // ERROR_MSG will not be translated, but the string will be found
* // by gettext when creating the POT file
* const RESULT = {
* ERROR: N_("Download failed"),
* OK: N_("Success")
* };
*
* // assume that "result" contains one of the constants above
* const result = ...;
* // here the string will be translated using the current locale
* return <div>Result: {_(result)}</div>;
*
* @param {string} str the input string
* @return {string} the input string
*/
const N_ = (str) => str;
/**
* Similar to the N_() function, but for the singular and plural form.
*
* @see {@link N_} for further information
* @param {string} str1 the input string in the singular form
* @param {string} strN the input string in the plural form
* @param {number} n the actual number which decides whether to use the
* singular or plural form
* @return {string} the original text, either "string1" or "stringN" depending
* on the value "num"
*/
const Nn_ = (str1, strN, n) => (n === 1) ? str1 : strN;
export {
_,
n_,
N_,
Nn_
};