4ee7469319e05506d1b4e0f9a47987d2f996ce1d.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. 'use strict'; // var forEach = require('for-each');
  2. // var warning = require('warning');
  3. // var has = require('has');
  4. // var trim = require('string.prototype.trim');
  5. System.register(["cc"], function (_export, _context) {
  6. "use strict";
  7. var _cclegacy, Polyglot, _crd, forEach, warning, has, warn, replace, split, delimiter, russianPluralGroups, defaultPluralRules, defaultTokenRegex;
  8. function trim(x) {
  9. return x.replace(/^\s+|\s+$/gm, '');
  10. }
  11. function langToTypeMap(mapping) {
  12. var ret = {};
  13. forEach(mapping, function (langs, type) {
  14. forEach(langs, function (lang) {
  15. ret[lang] = type;
  16. }, this);
  17. }, this);
  18. return ret;
  19. }
  20. function pluralTypeName(pluralRules, locale) {
  21. var langToPluralType = langToTypeMap(pluralRules.pluralTypeToLanguages); //@ts-ignore
  22. return langToPluralType[locale] || langToPluralType[split.call(locale, /-/, 1)[0]] || langToPluralType.en;
  23. }
  24. function pluralTypeIndex(pluralRules, locale, count) {
  25. return pluralRules.pluralTypes[pluralTypeName(pluralRules, locale)](count);
  26. }
  27. function escape(token) {
  28. return token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  29. }
  30. function constructTokenRegex(opts) {
  31. var prefix = opts && opts.prefix || '%{';
  32. var suffix = opts && opts.suffix || '}';
  33. if (prefix === delimiter || suffix === delimiter) {
  34. throw new RangeError('"' + delimiter + '" token is reserved for pluralization');
  35. }
  36. return new RegExp(escape(prefix) + '(.*?)' + escape(suffix), 'g');
  37. }
  38. // ### transformPhrase(phrase, substitutions, locale)
  39. //
  40. // Takes a phrase string and transforms it by choosing the correct
  41. // plural form and interpolating it.
  42. //
  43. // transformPhrase('Hello, %{name}!', {name: 'Spike'});
  44. // // "Hello, Spike!"
  45. //
  46. // The correct plural form is selected if substitutions.smart_count
  47. // is set. You can pass in a number instead of an Object as `substitutions`
  48. // as a shortcut for `smart_count`.
  49. //
  50. // transformPhrase('%{smart_count} new messages |||| 1 new message', {smart_count: 1}, 'en');
  51. // // "1 new message"
  52. //
  53. // transformPhrase('%{smart_count} new messages |||| 1 new message', {smart_count: 2}, 'en');
  54. // // "2 new messages"
  55. //
  56. // transformPhrase('%{smart_count} new messages |||| 1 new message', 5, 'en');
  57. // // "5 new messages"
  58. //
  59. // You should pass in a third argument, the locale, to specify the correct plural type.
  60. // It defaults to `'en'` with 2 plural forms.
  61. function transformPhrase(phrase, substitutions, locale, tokenRegex, pluralRules) {
  62. if (typeof phrase !== 'string') {
  63. throw new TypeError('Polyglot.transformPhrase expects argument #1 to be string');
  64. }
  65. if (substitutions == null) {
  66. return phrase;
  67. }
  68. var result = phrase;
  69. var interpolationRegex = tokenRegex || defaultTokenRegex;
  70. var pluralRulesOrDefault = pluralRules || defaultPluralRules; // allow number as a pluralization shortcut
  71. var options = typeof substitutions === 'number' ? {
  72. smart_count: substitutions
  73. } : substitutions; // Select plural form: based on a phrase text that contains `n`
  74. // plural forms separated by `delimiter`, a `locale`, and a `substitutions.smart_count`,
  75. // choose the correct plural form. This is only done if `count` is set.
  76. if (options.smart_count != null && result) {
  77. var texts = split.call(result, delimiter);
  78. result = trim(texts[pluralTypeIndex(pluralRulesOrDefault, locale || 'en', options.smart_count)] || texts[0]);
  79. } // Interpolate: Creates a `RegExp` object for each interpolation placeholder.
  80. result = replace.call(result, interpolationRegex, function (expression, argument) {
  81. if (!has(options, argument) || options[argument] == null) {
  82. return expression;
  83. }
  84. return options[argument];
  85. });
  86. return result;
  87. } // ### Polyglot class constructor
  88. function polyglot(options) {
  89. return new Polyglot(options);
  90. }
  91. _export({
  92. Polyglot: void 0,
  93. polyglot: polyglot
  94. });
  95. return {
  96. setters: [function (_cc) {
  97. _cclegacy = _cc.cclegacy;
  98. }],
  99. execute: function () {
  100. _crd = true;
  101. _cclegacy._RF.push({}, "26839JcVWpGabE9xA7/uWJw", "polyglot.min", undefined);
  102. forEach = function forEach(arr, fn, target) {
  103. // var arr = Array.isArray(arguments[0]) ? arguments[0] : new Array;
  104. arr = arguments[0];
  105. fn = typeof arguments[1] === 'function' ? arguments[1] : new Function();
  106. if (Array.isArray(arr)) {
  107. arr.forEach((item, idx, array) => {
  108. fn(item, idx);
  109. });
  110. } else {
  111. for (var key in arr) {
  112. if (arr.hasOwnProperty(key)) {
  113. var element = arr[key];
  114. fn(element, key);
  115. }
  116. }
  117. }
  118. };
  119. warning = function warning(message) {
  120. console.warn(message);
  121. };
  122. has = function has(obj, key) {
  123. return obj.hasOwnProperty(key);
  124. };
  125. warn = function warn(message) {
  126. warning(message);
  127. };
  128. replace = String.prototype.replace;
  129. split = String.prototype.split; // #### Pluralization methods
  130. // The string that separates the different phrase possibilities.
  131. delimiter = '||||';
  132. russianPluralGroups = function russianPluralGroups(n) {
  133. var lastTwo = n % 100;
  134. var end = lastTwo % 10;
  135. if (lastTwo !== 11 && end === 1) {
  136. return 0;
  137. }
  138. if (2 <= end && end <= 4 && !(lastTwo >= 12 && lastTwo <= 14)) {
  139. return 1;
  140. }
  141. return 2;
  142. };
  143. defaultPluralRules = {
  144. // Mapping from pluralization group plural logic.
  145. pluralTypes: {
  146. arabic: function arabic(n) {
  147. // http://www.arabeyes.org/Plural_Forms
  148. if (n < 3) {
  149. return n;
  150. }
  151. var lastTwo = n % 100;
  152. if (lastTwo >= 3 && lastTwo <= 10) return 3;
  153. return lastTwo >= 11 ? 4 : 5;
  154. },
  155. bosnian_serbian: russianPluralGroups,
  156. chinese: function chinese() {
  157. return 0;
  158. },
  159. croatian: russianPluralGroups,
  160. french: function french(n) {
  161. return n > 1 ? 1 : 0;
  162. },
  163. german: function german(n) {
  164. return n !== 1 ? 1 : 0;
  165. },
  166. russian: russianPluralGroups,
  167. lithuanian: function lithuanian(n) {
  168. if (n % 10 === 1 && n % 100 !== 11) {
  169. return 0;
  170. }
  171. return n % 10 >= 2 && n % 10 <= 9 && (n % 100 < 11 || n % 100 > 19) ? 1 : 2;
  172. },
  173. czech: function czech(n) {
  174. if (n === 1) {
  175. return 0;
  176. }
  177. return n >= 2 && n <= 4 ? 1 : 2;
  178. },
  179. polish: function polish(n) {
  180. if (n === 1) {
  181. return 0;
  182. }
  183. var end = n % 10;
  184. return 2 <= end && end <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2;
  185. },
  186. icelandic: function icelandic(n) {
  187. return n % 10 !== 1 || n % 100 === 11 ? 1 : 0;
  188. },
  189. slovenian: function slovenian(n) {
  190. var lastTwo = n % 100;
  191. if (lastTwo === 1) {
  192. return 0;
  193. }
  194. if (lastTwo === 2) {
  195. return 1;
  196. }
  197. if (lastTwo === 3 || lastTwo === 4) {
  198. return 2;
  199. }
  200. return 3;
  201. }
  202. },
  203. // Mapping from pluralization group to individual language codes/locales.
  204. // Will look up based on exact match, if not found and it's a locale will parse the locale
  205. // for language code, and if that does not exist will default to 'en'
  206. pluralTypeToLanguages: {
  207. arabic: ['ar'],
  208. bosnian_serbian: ['bs-Latn-BA', 'bs-Cyrl-BA', 'srl-RS', 'sr-RS'],
  209. chinese: ['id', 'id-ID', 'ja', 'ko', 'ko-KR', 'lo', 'ms', 'th', 'th-TH', 'zh'],
  210. croatian: ['hr', 'hr-HR'],
  211. german: ['fa', 'da', 'de', 'en', 'es', 'fi', 'el', 'he', 'hi-IN', 'hu', 'hu-HU', 'it', 'nl', 'no', 'pt', 'sv', 'tr'],
  212. french: ['fr', 'tl', 'pt-br'],
  213. russian: ['ru', 'ru-RU'],
  214. lithuanian: ['lt'],
  215. czech: ['cs', 'cs-CZ', 'sk'],
  216. polish: ['pl'],
  217. icelandic: ['is'],
  218. slovenian: ['sl-SL']
  219. }
  220. };
  221. defaultTokenRegex = /%\{(.*?)\}/g;
  222. _export("Polyglot", Polyglot = class Polyglot {
  223. static transformPhrase(phrase, substitutions, locale) {
  224. //@ts-ignore
  225. return transformPhrase(phrase, substitutions, locale);
  226. }
  227. constructor(options) {
  228. this.phrases = {};
  229. this.pluralRules = {};
  230. this.currentLocale = '';
  231. this.onMissingKey = null;
  232. this.warn = null;
  233. this.tokenRegex = void 0;
  234. var opts = options || {};
  235. this.extend(opts.phrases || {});
  236. this.currentLocale = opts.locale || 'en';
  237. var allowMissing = opts.allowMissing ? transformPhrase : null;
  238. this.onMissingKey = typeof opts.onMissingKey === 'function' ? opts.onMissingKey : allowMissing;
  239. this.warn = opts.warn || warn;
  240. this.tokenRegex = constructTokenRegex(opts.interpolation);
  241. this.pluralRules = opts.pluralRules || defaultPluralRules;
  242. } // ### polyglot.has(key)
  243. //
  244. // Check if polyglot has a translation for given key
  245. has(key) {
  246. return has(this.phrases, key);
  247. } // ### polyglot.t(key, options)
  248. //
  249. // The most-used method. Provide a key, and `t` will return the
  250. // phrase.
  251. //
  252. // polyglot.t("hello");
  253. // => "Hello"
  254. //
  255. // The phrase value is provided first by a call to `polyglot.extend()` or
  256. // `polyglot.replace()`.
  257. //
  258. // Pass in an object as the second argument to perform interpolation.
  259. //
  260. // polyglot.t("hello_name", {name: "Spike"});
  261. // => "Hello, Spike"
  262. //
  263. // If you like, you can provide a default value in case the phrase is missing.
  264. // Use the special option key "_" to specify a default.
  265. //
  266. // polyglot.t("i_like_to_write_in_language", {
  267. // _: "I like to write in %{language}.",
  268. // language: "JavaScript"
  269. // });
  270. // => "I like to write in JavaScript."
  271. //
  272. t(key, options) {
  273. var phrase, result;
  274. var opts = options == null ? {} : options;
  275. if (typeof this.phrases[key] === 'string') {
  276. phrase = this.phrases[key];
  277. } else if (typeof opts._ === 'string') {
  278. phrase = opts._;
  279. } else if (this.onMissingKey) {
  280. var onMissingKey = this.onMissingKey;
  281. result = onMissingKey(key, opts, this.currentLocale, this.tokenRegex, this.pluralRules);
  282. } else {
  283. this.warn('Missing translation for key: "' + key + '"');
  284. result = key;
  285. }
  286. if (typeof phrase === 'string') {
  287. result = transformPhrase(phrase, opts, this.currentLocale, this.tokenRegex, this.pluralRules);
  288. }
  289. return result;
  290. } // ### polyglot.replace(phrases)
  291. //
  292. // Completely replace the existing phrases with a new set of phrases.
  293. // Normally, just use `extend` to add more phrases, but under certain
  294. // circumstances, you may want to make sure no old phrases are lying around.
  295. replace(newPhrases) {
  296. this.clear();
  297. this.extend(newPhrases);
  298. } // ### polyglot.clear()
  299. //
  300. // Clears all phrases. Useful for special cases, such as freeing
  301. // up memory if you have lots of phrases but no longer need to
  302. // perform any translation. Also used internally by `replace`.
  303. clear() {
  304. this.phrases = {};
  305. } // ### polyglot.locale([locale])
  306. //
  307. // Get or set locale. Internally, Polyglot only uses locale for pluralization.
  308. locale(newLocale) {
  309. if (newLocale) this.currentLocale = newLocale;
  310. return this.currentLocale;
  311. } // ### polyglot.extend(phrases)
  312. //
  313. // Use `extend` to tell Polyglot how to translate a given key.
  314. //
  315. // polyglot.extend({
  316. // "hello": "Hello",
  317. // "hello_name": "Hello, %{name}"
  318. // });
  319. //
  320. // The key can be any string. Feel free to call `extend` multiple times;
  321. // it will override any phrases with the same key, but leave existing phrases
  322. // untouched.
  323. //
  324. // It is also possible to pass nested phrase objects, which get flattened
  325. // into an object with the nested keys concatenated using dot notation.
  326. //
  327. // polyglot.extend({
  328. // "nav": {
  329. // "hello": "Hello",
  330. // "hello_name": "Hello, %{name}",
  331. // "sidebar": {
  332. // "welcome": "Welcome"
  333. // }
  334. // }
  335. // });
  336. //
  337. // console.log(polyglot.phrases);
  338. // // {
  339. // // 'nav.hello': 'Hello',
  340. // // 'nav.hello_name': 'Hello, %{name}',
  341. // // 'nav.sidebar.welcome': 'Welcome'
  342. // // }
  343. //
  344. // `extend` accepts an optional second argument, `prefix`, which can be used
  345. // to prefix every key in the phrases object with some string, using dot
  346. // notation.
  347. //
  348. // polyglot.extend({
  349. // "hello": "Hello",
  350. // "hello_name": "Hello, %{name}"
  351. // }, "nav");
  352. //
  353. // console.log(polyglot.phrases);
  354. // // {
  355. // // 'nav.hello': 'Hello',
  356. // // 'nav.hello_name': 'Hello, %{name}'
  357. // // }
  358. //
  359. // This feature is used internally to support nested phrase objects.
  360. extend(morePhrases, prefix) {
  361. var _this = this;
  362. forEach(morePhrases, function (phrase, key) {
  363. var prefixedKey = prefix ? prefix + '.' + key : key;
  364. if (typeof phrase === 'object') {
  365. _this.extend(phrase, prefixedKey);
  366. } else {
  367. _this.phrases[prefixedKey] = phrase;
  368. }
  369. }, this);
  370. } // ### polyglot.unset(phrases)
  371. // Use `unset` to selectively remove keys from a polyglot instance.
  372. //
  373. // polyglot.unset("some_key");
  374. // polyglot.unset({
  375. // "hello": "Hello",
  376. // "hello_name": "Hello, %{name}"
  377. // });
  378. //
  379. // The unset method can take either a string (for the key), or an object hash with
  380. // the keys that you would like to unset.
  381. unset(morePhrases, prefix) {
  382. if (typeof morePhrases === 'string') {
  383. delete this.phrases[morePhrases];
  384. } else {
  385. forEach(morePhrases, function (phrase, key) {
  386. var prefixedKey = prefix ? prefix + '.' + key : key;
  387. if (typeof phrase === 'object') {
  388. this.unset(phrase, prefixedKey);
  389. } else {
  390. delete this.phrases[prefixedKey];
  391. }
  392. }, this);
  393. }
  394. }
  395. });
  396. _cclegacy._RF.pop();
  397. _crd = false;
  398. }
  399. };
  400. });
  401. //# sourceMappingURL=4ee7469319e05506d1b4e0f9a47987d2f996ce1d.js.map