提交 | 用户 | 时间
|
820397
|
1 |
// import translations from "./zh"; |
H |
2 |
// |
|
3 |
// export default function customTranslate(template, replacements) { |
|
4 |
// replacements = replacements || {}; |
|
5 |
// |
|
6 |
// // Translate |
|
7 |
// template = translations[template] || template; |
|
8 |
// |
|
9 |
// // Replace |
|
10 |
// return template.replace(/{([^}]+)}/g, function(_, key) { |
|
11 |
// let str = replacements[key]; |
|
12 |
// if ( |
|
13 |
// translations[replacements[key]] !== null && |
|
14 |
// translations[replacements[key]] !== "undefined" |
|
15 |
// ) { |
|
16 |
// // eslint-disable-next-line no-mixed-spaces-and-tabs |
|
17 |
// str = translations[replacements[key]]; |
|
18 |
// // eslint-disable-next-line no-mixed-spaces-and-tabs |
|
19 |
// } |
|
20 |
// return str || "{" + key + "}"; |
|
21 |
// }); |
|
22 |
// } |
|
23 |
|
|
24 |
export default function customTranslate(translations) { |
|
25 |
return function (template, replacements) { |
|
26 |
replacements = replacements || {} |
|
27 |
// Translate |
|
28 |
template = translations[template] || template |
|
29 |
|
|
30 |
// Replace |
|
31 |
return template.replace(/{([^}]+)}/g, function (_, key) { |
|
32 |
let str = replacements[key] |
|
33 |
if ( |
|
34 |
translations[replacements[key]] !== null && |
|
35 |
translations[replacements[key]] !== undefined |
|
36 |
) { |
|
37 |
// eslint-disable-next-line no-mixed-spaces-and-tabs |
|
38 |
str = translations[replacements[key]] |
|
39 |
// eslint-disable-next-line no-mixed-spaces-and-tabs |
|
40 |
} |
|
41 |
return str || '{' + key + '}' |
|
42 |
}) |
|
43 |
} |
|
44 |
} |