-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (73 loc) · 2.26 KB
/
index.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
/*
* USAGE EXAMPLE
*/
// Suppose these are your objects containing localized strings
const en = {
"simple-string": "Simple string",
"string-with-variable": "My name is ${name}",
"string-with-variables": "My full name is ${firstName} ${lastName}",
"parent-level": {
"child-level": "Second level",
"child-level-with-variable": "I was born in ${year}",
},
};
const it = {
"simple-string": "Stringa semplice",
"string-with-variable": "Il mio nome è ${name}",
"string-with-variables": "Il mio nome completo è ${firstName} ${lastName}",
"parent-level": {
"child-level": "Secondo livello",
"child-level-with-variable": "Io sono nato nel ${year}",
},
};
// Set your current language dictionary
const currentLanguageDictionary = en;
// t() function
const t = (key, ...variables) => {
// Find reference in currentLanguage object
const keys = key.split(".");
let stringToReturn = keys.reduce(
(obj, k) => obj[k],
currentLanguageDictionary
);
// Replace variables with passed values
// Accumulators are all named stringToReturn just to create confusion ( ͡° ͜ʖ ͡°)
return variables
.flat()
.reduce(
(stringToReturn, variable) =>
Object.entries(variable).reduce(
(stringToReturn, [variableKey, variableValue]) =>
stringToReturn.replace(`$\{${variableKey}}`, variableValue),
stringToReturn
),
stringToReturn
);
};
// Examples
const simpleString = t("simple-string");
console.log(simpleString);
const stringWithVariable = t("string-with-variable", { name: "Luca" });
console.log(stringWithVariable);
const stringWithArrayOfVariables = t("string-with-variables", [
{ firstName: "Mario" },
{ lastName: "Rossi" },
]);
console.log(stringWithArrayOfVariables);
const stringWithVariables = t(
"string-with-variables",
{ firstName: "Mario" },
{ lastName: "Rossi" }
);
console.log(stringWithVariables);
const objectWithMultipleVariables = t("string-with-variables", {
firstName: "Mario",
lastName: "Rossi",
});
console.log(objectWithMultipleVariables);
const parentAndChild = t("parent-level.child-level");
console.log(parentAndChild);
const parentChildAndVariable = t("parent-level.child-level-with-variable", {
year: "1990",
});
console.log(parentChildAndVariable);