customTranslate.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // import translations from "./zh";
  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. export default function customTranslate(translations) {
  24. return function(template, replacements) {
  25. replacements = replacements || {};
  26. // Translate
  27. template = translations[template] || template;
  28. // Replace
  29. return template.replace(/{([^}]+)}/g, function(_, key) {
  30. let str = replacements[key];
  31. if (translations[replacements[key]] !== null && translations[replacements[key]] !== undefined) {
  32. // eslint-disable-next-line no-mixed-spaces-and-tabs
  33. str = translations[replacements[key]];
  34. // eslint-disable-next-line no-mixed-spaces-and-tabs
  35. }
  36. return str || "{" + key + "}";
  37. });
  38. };
  39. }