customTranslate.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 (
  32. translations[replacements[key]] !== null &&
  33. translations[replacements[key]] !== undefined
  34. ) {
  35. // eslint-disable-next-line no-mixed-spaces-and-tabs
  36. str = translations[replacements[key]]
  37. // eslint-disable-next-line no-mixed-spaces-and-tabs
  38. }
  39. return str || '{' + key + '}'
  40. })
  41. }
  42. }