提交 | 用户 | 时间
|
722af2
|
1 |
// import translations from "./zh"; |
X |
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 (translations[replacements[key]] !== null && translations[replacements[key]] !== undefined) { |
|
34 |
// eslint-disable-next-line no-mixed-spaces-and-tabs |
|
35 |
str = translations[replacements[key]]; |
|
36 |
// eslint-disable-next-line no-mixed-spaces-and-tabs |
|
37 |
} |
|
38 |
return str || "{" + key + "}"; |
|
39 |
}); |
|
40 |
}; |
|
41 |
} |