peacestorm

i18n

Jun 12th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Internationalization: Language Support
  3.  * Thanks for testing i18n function: NenkaLab<[email protected]>, and ManDongI<[email protected]>
  4.  * @author Astro36<[email protected]>
  5.  * @function i18n
  6.  * @memberOf global
  7.  * @param {String} text
  8.  * @param {Array.<String>} [args]
  9.  * @returns {String}
  10.  */
  11. const i18n = (function () {
  12.     const lang = CONTEXT.getResources().getConfiguration().locale.getLanguage(),
  13.         langPath = PATH + "/lang/" + lang + ".json";
  14.     if (new File_(langPath).exists() && useCustomLangTest) {
  15.         const langObj = JSON.parse(getTextFromFile(langPath));
  16.         return function (text, args) {
  17.             if (text in langObj) {
  18.                 return langObj[text].replace(/(\{%\d+\})/g, function ($1) {
  19.                     return args[$1.match(/\{%(\d+)\}/)[1]];
  20.                 });
  21.             }
  22.             return text.replace(/(\{%\d+\})/g, function ($1) {
  23.                 return args[$1.match(/\{%(\d+)\}/)[1]];
  24.             });
  25.         };
  26.     }
  27.     return function (text, args) {
  28.         // especially this part needs to be changed
  29.         if(args != null) {
  30.             if(args[0] != null) {
  31.                 text = text.replace("%0", args[0]);
  32.             }
  33.             if(args[1] != null) {
  34.                 text = text.replace("%1", args[1]);
  35.             }
  36.             if(args[2] != null) {
  37.                 text = text.replace("%2", args[2]);
  38.             }
  39.         }
  40.         return text;
  41.         /* return text.replace(/(\{%\d+\})/g, function ($1) {
  42.             return args[$1.match(/\{%(\d+)\}/)[1]];
  43.         }); */
  44.     };
  45. })();
Advertisement
Add Comment
Please, Sign In to add comment