vue-i18n.js 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  1. /*!
  2. * vue-i18n v8.27.2
  3. * (c) 2022 kazuya kawaguchi
  4. * Released under the MIT License.
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  8. typeof define === 'function' && define.amd ? define(factory) :
  9. (global.VueI18n = factory());
  10. }(this, (function () { 'use strict';
  11. /* */
  12. /**
  13. * constants
  14. */
  15. var numberFormatKeys = [
  16. 'compactDisplay',
  17. 'currency',
  18. 'currencyDisplay',
  19. 'currencySign',
  20. 'localeMatcher',
  21. 'notation',
  22. 'numberingSystem',
  23. 'signDisplay',
  24. 'style',
  25. 'unit',
  26. 'unitDisplay',
  27. 'useGrouping',
  28. 'minimumIntegerDigits',
  29. 'minimumFractionDigits',
  30. 'maximumFractionDigits',
  31. 'minimumSignificantDigits',
  32. 'maximumSignificantDigits'
  33. ];
  34. /**
  35. * utilities
  36. */
  37. function warn (msg, err) {
  38. if (typeof console !== 'undefined') {
  39. console.warn('[vue-i18n] ' + msg);
  40. /* istanbul ignore if */
  41. if (err) {
  42. console.warn(err.stack);
  43. }
  44. }
  45. }
  46. function error (msg, err) {
  47. if (typeof console !== 'undefined') {
  48. console.error('[vue-i18n] ' + msg);
  49. /* istanbul ignore if */
  50. if (err) {
  51. console.error(err.stack);
  52. }
  53. }
  54. }
  55. var isArray = Array.isArray;
  56. function isObject (obj) {
  57. return obj !== null && typeof obj === 'object'
  58. }
  59. function isBoolean (val) {
  60. return typeof val === 'boolean'
  61. }
  62. function isString (val) {
  63. return typeof val === 'string'
  64. }
  65. var toString = Object.prototype.toString;
  66. var OBJECT_STRING = '[object Object]';
  67. function isPlainObject (obj) {
  68. return toString.call(obj) === OBJECT_STRING
  69. }
  70. function isNull (val) {
  71. return val === null || val === undefined
  72. }
  73. function isFunction (val) {
  74. return typeof val === 'function'
  75. }
  76. function parseArgs () {
  77. var args = [], len = arguments.length;
  78. while ( len-- ) args[ len ] = arguments[ len ];
  79. var locale = null;
  80. var params = null;
  81. if (args.length === 1) {
  82. if (isObject(args[0]) || isArray(args[0])) {
  83. params = args[0];
  84. } else if (typeof args[0] === 'string') {
  85. locale = args[0];
  86. }
  87. } else if (args.length === 2) {
  88. if (typeof args[0] === 'string') {
  89. locale = args[0];
  90. }
  91. /* istanbul ignore if */
  92. if (isObject(args[1]) || isArray(args[1])) {
  93. params = args[1];
  94. }
  95. }
  96. return { locale: locale, params: params }
  97. }
  98. function looseClone (obj) {
  99. return JSON.parse(JSON.stringify(obj))
  100. }
  101. function remove (arr, item) {
  102. if (arr.delete(item)) {
  103. return arr
  104. }
  105. }
  106. function arrayFrom (arr) {
  107. var ret = [];
  108. arr.forEach(function (a) { return ret.push(a); });
  109. return ret
  110. }
  111. function includes (arr, item) {
  112. return !!~arr.indexOf(item)
  113. }
  114. var hasOwnProperty = Object.prototype.hasOwnProperty;
  115. function hasOwn (obj, key) {
  116. return hasOwnProperty.call(obj, key)
  117. }
  118. function merge (target) {
  119. var arguments$1 = arguments;
  120. var output = Object(target);
  121. for (var i = 1; i < arguments.length; i++) {
  122. var source = arguments$1[i];
  123. if (source !== undefined && source !== null) {
  124. var key = (void 0);
  125. for (key in source) {
  126. if (hasOwn(source, key)) {
  127. if (isObject(source[key])) {
  128. output[key] = merge(output[key], source[key]);
  129. } else {
  130. output[key] = source[key];
  131. }
  132. }
  133. }
  134. }
  135. }
  136. return output
  137. }
  138. function looseEqual (a, b) {
  139. if (a === b) { return true }
  140. var isObjectA = isObject(a);
  141. var isObjectB = isObject(b);
  142. if (isObjectA && isObjectB) {
  143. try {
  144. var isArrayA = isArray(a);
  145. var isArrayB = isArray(b);
  146. if (isArrayA && isArrayB) {
  147. return a.length === b.length && a.every(function (e, i) {
  148. return looseEqual(e, b[i])
  149. })
  150. } else if (!isArrayA && !isArrayB) {
  151. var keysA = Object.keys(a);
  152. var keysB = Object.keys(b);
  153. return keysA.length === keysB.length && keysA.every(function (key) {
  154. return looseEqual(a[key], b[key])
  155. })
  156. } else {
  157. /* istanbul ignore next */
  158. return false
  159. }
  160. } catch (e) {
  161. /* istanbul ignore next */
  162. return false
  163. }
  164. } else if (!isObjectA && !isObjectB) {
  165. return String(a) === String(b)
  166. } else {
  167. return false
  168. }
  169. }
  170. /**
  171. * Sanitizes html special characters from input strings. For mitigating risk of XSS attacks.
  172. * @param rawText The raw input from the user that should be escaped.
  173. */
  174. function escapeHtml(rawText) {
  175. return rawText
  176. .replace(/</g, '&lt;')
  177. .replace(/>/g, '&gt;')
  178. .replace(/"/g, '&quot;')
  179. .replace(/'/g, '&apos;')
  180. }
  181. /**
  182. * Escapes html tags and special symbols from all provided params which were returned from parseArgs().params.
  183. * This method performs an in-place operation on the params object.
  184. *
  185. * @param {any} params Parameters as provided from `parseArgs().params`.
  186. * May be either an array of strings or a string->any map.
  187. *
  188. * @returns The manipulated `params` object.
  189. */
  190. function escapeParams(params) {
  191. if(params != null) {
  192. Object.keys(params).forEach(function (key) {
  193. if(typeof(params[key]) == 'string') {
  194. params[key] = escapeHtml(params[key]);
  195. }
  196. });
  197. }
  198. return params
  199. }
  200. /* */
  201. function extend (Vue) {
  202. if (!Vue.prototype.hasOwnProperty('$i18n')) {
  203. // $FlowFixMe
  204. Object.defineProperty(Vue.prototype, '$i18n', {
  205. get: function get () { return this._i18n }
  206. });
  207. }
  208. Vue.prototype.$t = function (key) {
  209. var values = [], len = arguments.length - 1;
  210. while ( len-- > 0 ) values[ len ] = arguments[ len + 1 ];
  211. var i18n = this.$i18n;
  212. return i18n._t.apply(i18n, [ key, i18n.locale, i18n._getMessages(), this ].concat( values ))
  213. };
  214. Vue.prototype.$tc = function (key, choice) {
  215. var values = [], len = arguments.length - 2;
  216. while ( len-- > 0 ) values[ len ] = arguments[ len + 2 ];
  217. var i18n = this.$i18n;
  218. return i18n._tc.apply(i18n, [ key, i18n.locale, i18n._getMessages(), this, choice ].concat( values ))
  219. };
  220. Vue.prototype.$te = function (key, locale) {
  221. var i18n = this.$i18n;
  222. return i18n._te(key, i18n.locale, i18n._getMessages(), locale)
  223. };
  224. Vue.prototype.$d = function (value) {
  225. var ref;
  226. var args = [], len = arguments.length - 1;
  227. while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
  228. return (ref = this.$i18n).d.apply(ref, [ value ].concat( args ))
  229. };
  230. Vue.prototype.$n = function (value) {
  231. var ref;
  232. var args = [], len = arguments.length - 1;
  233. while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
  234. return (ref = this.$i18n).n.apply(ref, [ value ].concat( args ))
  235. };
  236. }
  237. /* */
  238. /**
  239. * Mixin
  240. *
  241. * If `bridge` mode, empty mixin is returned,
  242. * else regulary mixin implementation is returned.
  243. */
  244. function defineMixin (bridge) {
  245. if ( bridge === void 0 ) bridge = false;
  246. function mounted () {
  247. if (this !== this.$root && this.$options.__INTLIFY_META__ && this.$el) {
  248. this.$el.setAttribute('data-intlify', this.$options.__INTLIFY_META__);
  249. }
  250. }
  251. return bridge
  252. ? { mounted: mounted } // delegate `vue-i18n-bridge` mixin implementation
  253. : { // regulary
  254. beforeCreate: function beforeCreate () {
  255. var options = this.$options;
  256. options.i18n = options.i18n || ((options.__i18nBridge || options.__i18n) ? {} : null);
  257. if (options.i18n) {
  258. if (options.i18n instanceof VueI18n) {
  259. // init locale messages via custom blocks
  260. if ((options.__i18nBridge || options.__i18n)) {
  261. try {
  262. var localeMessages = options.i18n && options.i18n.messages ? options.i18n.messages : {};
  263. var _i18n = options.__i18nBridge || options.__i18n;
  264. _i18n.forEach(function (resource) {
  265. localeMessages = merge(localeMessages, JSON.parse(resource));
  266. });
  267. Object.keys(localeMessages).forEach(function (locale) {
  268. options.i18n.mergeLocaleMessage(locale, localeMessages[locale]);
  269. });
  270. } catch (e) {
  271. {
  272. error("Cannot parse locale messages via custom blocks.", e);
  273. }
  274. }
  275. }
  276. this._i18n = options.i18n;
  277. this._i18nWatcher = this._i18n.watchI18nData();
  278. } else if (isPlainObject(options.i18n)) {
  279. var rootI18n = this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n
  280. ? this.$root.$i18n
  281. : null;
  282. // component local i18n
  283. if (rootI18n) {
  284. options.i18n.root = this.$root;
  285. options.i18n.formatter = rootI18n.formatter;
  286. options.i18n.fallbackLocale = rootI18n.fallbackLocale;
  287. options.i18n.formatFallbackMessages = rootI18n.formatFallbackMessages;
  288. options.i18n.silentTranslationWarn = rootI18n.silentTranslationWarn;
  289. options.i18n.silentFallbackWarn = rootI18n.silentFallbackWarn;
  290. options.i18n.pluralizationRules = rootI18n.pluralizationRules;
  291. options.i18n.preserveDirectiveContent = rootI18n.preserveDirectiveContent;
  292. }
  293. // init locale messages via custom blocks
  294. if ((options.__i18nBridge || options.__i18n)) {
  295. try {
  296. var localeMessages$1 = options.i18n && options.i18n.messages ? options.i18n.messages : {};
  297. var _i18n$1 = options.__i18nBridge || options.__i18n;
  298. _i18n$1.forEach(function (resource) {
  299. localeMessages$1 = merge(localeMessages$1, JSON.parse(resource));
  300. });
  301. options.i18n.messages = localeMessages$1;
  302. } catch (e) {
  303. {
  304. warn("Cannot parse locale messages via custom blocks.", e);
  305. }
  306. }
  307. }
  308. var ref = options.i18n;
  309. var sharedMessages = ref.sharedMessages;
  310. if (sharedMessages && isPlainObject(sharedMessages)) {
  311. options.i18n.messages = merge(options.i18n.messages, sharedMessages);
  312. }
  313. this._i18n = new VueI18n(options.i18n);
  314. this._i18nWatcher = this._i18n.watchI18nData();
  315. if (options.i18n.sync === undefined || !!options.i18n.sync) {
  316. this._localeWatcher = this.$i18n.watchLocale();
  317. }
  318. if (rootI18n) {
  319. rootI18n.onComponentInstanceCreated(this._i18n);
  320. }
  321. } else {
  322. {
  323. warn("Cannot be interpreted 'i18n' option.");
  324. }
  325. }
  326. } else if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
  327. // root i18n
  328. this._i18n = this.$root.$i18n;
  329. } else if (options.parent && options.parent.$i18n && options.parent.$i18n instanceof VueI18n) {
  330. // parent i18n
  331. this._i18n = options.parent.$i18n;
  332. }
  333. },
  334. beforeMount: function beforeMount () {
  335. var options = this.$options;
  336. options.i18n = options.i18n || ((options.__i18nBridge || options.__i18n) ? {} : null);
  337. if (options.i18n) {
  338. if (options.i18n instanceof VueI18n) {
  339. // init locale messages via custom blocks
  340. this._i18n.subscribeDataChanging(this);
  341. this._subscribing = true;
  342. } else if (isPlainObject(options.i18n)) {
  343. this._i18n.subscribeDataChanging(this);
  344. this._subscribing = true;
  345. } else {
  346. {
  347. warn("Cannot be interpreted 'i18n' option.");
  348. }
  349. }
  350. } else if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
  351. this._i18n.subscribeDataChanging(this);
  352. this._subscribing = true;
  353. } else if (options.parent && options.parent.$i18n && options.parent.$i18n instanceof VueI18n) {
  354. this._i18n.subscribeDataChanging(this);
  355. this._subscribing = true;
  356. }
  357. },
  358. mounted: mounted,
  359. beforeDestroy: function beforeDestroy () {
  360. if (!this._i18n) { return }
  361. var self = this;
  362. this.$nextTick(function () {
  363. if (self._subscribing) {
  364. self._i18n.unsubscribeDataChanging(self);
  365. delete self._subscribing;
  366. }
  367. if (self._i18nWatcher) {
  368. self._i18nWatcher();
  369. self._i18n.destroyVM();
  370. delete self._i18nWatcher;
  371. }
  372. if (self._localeWatcher) {
  373. self._localeWatcher();
  374. delete self._localeWatcher;
  375. }
  376. });
  377. }
  378. }
  379. }
  380. /* */
  381. var interpolationComponent = {
  382. name: 'i18n',
  383. functional: true,
  384. props: {
  385. tag: {
  386. type: [String, Boolean, Object],
  387. default: 'span'
  388. },
  389. path: {
  390. type: String,
  391. required: true
  392. },
  393. locale: {
  394. type: String
  395. },
  396. places: {
  397. type: [Array, Object]
  398. }
  399. },
  400. render: function render (h, ref) {
  401. var data = ref.data;
  402. var parent = ref.parent;
  403. var props = ref.props;
  404. var slots = ref.slots;
  405. var $i18n = parent.$i18n;
  406. if (!$i18n) {
  407. {
  408. warn('Cannot find VueI18n instance!');
  409. }
  410. return
  411. }
  412. var path = props.path;
  413. var locale = props.locale;
  414. var places = props.places;
  415. var params = slots();
  416. var children = $i18n.i(
  417. path,
  418. locale,
  419. onlyHasDefaultPlace(params) || places
  420. ? useLegacyPlaces(params.default, places)
  421. : params
  422. );
  423. var tag = (!!props.tag && props.tag !== true) || props.tag === false ? props.tag : 'span';
  424. return tag ? h(tag, data, children) : children
  425. }
  426. };
  427. function onlyHasDefaultPlace (params) {
  428. var prop;
  429. for (prop in params) {
  430. if (prop !== 'default') { return false }
  431. }
  432. return Boolean(prop)
  433. }
  434. function useLegacyPlaces (children, places) {
  435. var params = places ? createParamsFromPlaces(places) : {};
  436. if (!children) { return params }
  437. // Filter empty text nodes
  438. children = children.filter(function (child) {
  439. return child.tag || child.text.trim() !== ''
  440. });
  441. var everyPlace = children.every(vnodeHasPlaceAttribute);
  442. if (everyPlace) {
  443. warn('`place` attribute is deprecated in next major version. Please switch to Vue slots.');
  444. }
  445. return children.reduce(
  446. everyPlace ? assignChildPlace : assignChildIndex,
  447. params
  448. )
  449. }
  450. function createParamsFromPlaces (places) {
  451. {
  452. warn('`places` prop is deprecated in next major version. Please switch to Vue slots.');
  453. }
  454. return Array.isArray(places)
  455. ? places.reduce(assignChildIndex, {})
  456. : Object.assign({}, places)
  457. }
  458. function assignChildPlace (params, child) {
  459. if (child.data && child.data.attrs && child.data.attrs.place) {
  460. params[child.data.attrs.place] = child;
  461. }
  462. return params
  463. }
  464. function assignChildIndex (params, child, index) {
  465. params[index] = child;
  466. return params
  467. }
  468. function vnodeHasPlaceAttribute (vnode) {
  469. return Boolean(vnode.data && vnode.data.attrs && vnode.data.attrs.place)
  470. }
  471. /* */
  472. var numberComponent = {
  473. name: 'i18n-n',
  474. functional: true,
  475. props: {
  476. tag: {
  477. type: [String, Boolean, Object],
  478. default: 'span'
  479. },
  480. value: {
  481. type: Number,
  482. required: true
  483. },
  484. format: {
  485. type: [String, Object]
  486. },
  487. locale: {
  488. type: String
  489. }
  490. },
  491. render: function render (h, ref) {
  492. var props = ref.props;
  493. var parent = ref.parent;
  494. var data = ref.data;
  495. var i18n = parent.$i18n;
  496. if (!i18n) {
  497. {
  498. warn('Cannot find VueI18n instance!');
  499. }
  500. return null
  501. }
  502. var key = null;
  503. var options = null;
  504. if (isString(props.format)) {
  505. key = props.format;
  506. } else if (isObject(props.format)) {
  507. if (props.format.key) {
  508. key = props.format.key;
  509. }
  510. // Filter out number format options only
  511. options = Object.keys(props.format).reduce(function (acc, prop) {
  512. var obj;
  513. if (includes(numberFormatKeys, prop)) {
  514. return Object.assign({}, acc, ( obj = {}, obj[prop] = props.format[prop], obj ))
  515. }
  516. return acc
  517. }, null);
  518. }
  519. var locale = props.locale || i18n.locale;
  520. var parts = i18n._ntp(props.value, locale, key, options);
  521. var values = parts.map(function (part, index) {
  522. var obj;
  523. var slot = data.scopedSlots && data.scopedSlots[part.type];
  524. return slot ? slot(( obj = {}, obj[part.type] = part.value, obj.index = index, obj.parts = parts, obj )) : part.value
  525. });
  526. var tag = (!!props.tag && props.tag !== true) || props.tag === false ? props.tag : 'span';
  527. return tag
  528. ? h(tag, {
  529. attrs: data.attrs,
  530. 'class': data['class'],
  531. staticClass: data.staticClass
  532. }, values)
  533. : values
  534. }
  535. };
  536. /* */
  537. function bind (el, binding, vnode) {
  538. if (!assert(el, vnode)) { return }
  539. t(el, binding, vnode);
  540. }
  541. function update (el, binding, vnode, oldVNode) {
  542. if (!assert(el, vnode)) { return }
  543. var i18n = vnode.context.$i18n;
  544. if (localeEqual(el, vnode) &&
  545. (looseEqual(binding.value, binding.oldValue) &&
  546. looseEqual(el._localeMessage, i18n.getLocaleMessage(i18n.locale)))) { return }
  547. t(el, binding, vnode);
  548. }
  549. function unbind (el, binding, vnode, oldVNode) {
  550. var vm = vnode.context;
  551. if (!vm) {
  552. warn('Vue instance does not exists in VNode context');
  553. return
  554. }
  555. var i18n = vnode.context.$i18n || {};
  556. if (!binding.modifiers.preserve && !i18n.preserveDirectiveContent) {
  557. el.textContent = '';
  558. }
  559. el._vt = undefined;
  560. delete el['_vt'];
  561. el._locale = undefined;
  562. delete el['_locale'];
  563. el._localeMessage = undefined;
  564. delete el['_localeMessage'];
  565. }
  566. function assert (el, vnode) {
  567. var vm = vnode.context;
  568. if (!vm) {
  569. warn('Vue instance does not exists in VNode context');
  570. return false
  571. }
  572. if (!vm.$i18n) {
  573. warn('VueI18n instance does not exists in Vue instance');
  574. return false
  575. }
  576. return true
  577. }
  578. function localeEqual (el, vnode) {
  579. var vm = vnode.context;
  580. return el._locale === vm.$i18n.locale
  581. }
  582. function t (el, binding, vnode) {
  583. var ref$1, ref$2;
  584. var value = binding.value;
  585. var ref = parseValue(value);
  586. var path = ref.path;
  587. var locale = ref.locale;
  588. var args = ref.args;
  589. var choice = ref.choice;
  590. if (!path && !locale && !args) {
  591. warn('value type not supported');
  592. return
  593. }
  594. if (!path) {
  595. warn('`path` is required in v-t directive');
  596. return
  597. }
  598. var vm = vnode.context;
  599. if (choice != null) {
  600. el._vt = el.textContent = (ref$1 = vm.$i18n).tc.apply(ref$1, [ path, choice ].concat( makeParams(locale, args) ));
  601. } else {
  602. el._vt = el.textContent = (ref$2 = vm.$i18n).t.apply(ref$2, [ path ].concat( makeParams(locale, args) ));
  603. }
  604. el._locale = vm.$i18n.locale;
  605. el._localeMessage = vm.$i18n.getLocaleMessage(vm.$i18n.locale);
  606. }
  607. function parseValue (value) {
  608. var path;
  609. var locale;
  610. var args;
  611. var choice;
  612. if (isString(value)) {
  613. path = value;
  614. } else if (isPlainObject(value)) {
  615. path = value.path;
  616. locale = value.locale;
  617. args = value.args;
  618. choice = value.choice;
  619. }
  620. return { path: path, locale: locale, args: args, choice: choice }
  621. }
  622. function makeParams (locale, args) {
  623. var params = [];
  624. locale && params.push(locale);
  625. if (args && (Array.isArray(args) || isPlainObject(args))) {
  626. params.push(args);
  627. }
  628. return params
  629. }
  630. var Vue;
  631. function install (_Vue, options) {
  632. if ( options === void 0 ) options = { bridge: false };
  633. /* istanbul ignore if */
  634. if (install.installed && _Vue === Vue) {
  635. warn('already installed.');
  636. return
  637. }
  638. install.installed = true;
  639. Vue = _Vue;
  640. var version = (Vue.version && Number(Vue.version.split('.')[0])) || -1;
  641. /* istanbul ignore if */
  642. if (version < 2) {
  643. warn(("vue-i18n (" + (install.version) + ") need to use Vue 2.0 or later (Vue: " + (Vue.version) + ")."));
  644. return
  645. }
  646. extend(Vue);
  647. Vue.mixin(defineMixin(options.bridge));
  648. Vue.directive('t', { bind: bind, update: update, unbind: unbind });
  649. Vue.component(interpolationComponent.name, interpolationComponent);
  650. Vue.component(numberComponent.name, numberComponent);
  651. // use simple mergeStrategies to prevent i18n instance lose '__proto__'
  652. var strats = Vue.config.optionMergeStrategies;
  653. strats.i18n = function (parentVal, childVal) {
  654. return childVal === undefined
  655. ? parentVal
  656. : childVal
  657. };
  658. }
  659. /* */
  660. var BaseFormatter = function BaseFormatter () {
  661. this._caches = Object.create(null);
  662. };
  663. BaseFormatter.prototype.interpolate = function interpolate (message, values) {
  664. if (!values) {
  665. return [message]
  666. }
  667. var tokens = this._caches[message];
  668. if (!tokens) {
  669. tokens = parse(message);
  670. this._caches[message] = tokens;
  671. }
  672. return compile(tokens, values)
  673. };
  674. var RE_TOKEN_LIST_VALUE = /^(?:\d)+/;
  675. var RE_TOKEN_NAMED_VALUE = /^(?:\w)+/;
  676. function parse (format) {
  677. var tokens = [];
  678. var position = 0;
  679. var text = '';
  680. while (position < format.length) {
  681. var char = format[position++];
  682. if (char === '{') {
  683. if (text) {
  684. tokens.push({ type: 'text', value: text });
  685. }
  686. text = '';
  687. var sub = '';
  688. char = format[position++];
  689. while (char !== undefined && char !== '}') {
  690. sub += char;
  691. char = format[position++];
  692. }
  693. var isClosed = char === '}';
  694. var type = RE_TOKEN_LIST_VALUE.test(sub)
  695. ? 'list'
  696. : isClosed && RE_TOKEN_NAMED_VALUE.test(sub)
  697. ? 'named'
  698. : 'unknown';
  699. tokens.push({ value: sub, type: type });
  700. } else if (char === '%') {
  701. // when found rails i18n syntax, skip text capture
  702. if (format[(position)] !== '{') {
  703. text += char;
  704. }
  705. } else {
  706. text += char;
  707. }
  708. }
  709. text && tokens.push({ type: 'text', value: text });
  710. return tokens
  711. }
  712. function compile (tokens, values) {
  713. var compiled = [];
  714. var index = 0;
  715. var mode = Array.isArray(values)
  716. ? 'list'
  717. : isObject(values)
  718. ? 'named'
  719. : 'unknown';
  720. if (mode === 'unknown') { return compiled }
  721. while (index < tokens.length) {
  722. var token = tokens[index];
  723. switch (token.type) {
  724. case 'text':
  725. compiled.push(token.value);
  726. break
  727. case 'list':
  728. compiled.push(values[parseInt(token.value, 10)]);
  729. break
  730. case 'named':
  731. if (mode === 'named') {
  732. compiled.push((values)[token.value]);
  733. } else {
  734. {
  735. warn(("Type of token '" + (token.type) + "' and format of value '" + mode + "' don't match!"));
  736. }
  737. }
  738. break
  739. case 'unknown':
  740. {
  741. warn("Detect 'unknown' type of token!");
  742. }
  743. break
  744. }
  745. index++;
  746. }
  747. return compiled
  748. }
  749. /* */
  750. /**
  751. * Path parser
  752. * - Inspired:
  753. * Vue.js Path parser
  754. */
  755. // actions
  756. var APPEND = 0;
  757. var PUSH = 1;
  758. var INC_SUB_PATH_DEPTH = 2;
  759. var PUSH_SUB_PATH = 3;
  760. // states
  761. var BEFORE_PATH = 0;
  762. var IN_PATH = 1;
  763. var BEFORE_IDENT = 2;
  764. var IN_IDENT = 3;
  765. var IN_SUB_PATH = 4;
  766. var IN_SINGLE_QUOTE = 5;
  767. var IN_DOUBLE_QUOTE = 6;
  768. var AFTER_PATH = 7;
  769. var ERROR = 8;
  770. var pathStateMachine = [];
  771. pathStateMachine[BEFORE_PATH] = {
  772. 'ws': [BEFORE_PATH],
  773. 'ident': [IN_IDENT, APPEND],
  774. '[': [IN_SUB_PATH],
  775. 'eof': [AFTER_PATH]
  776. };
  777. pathStateMachine[IN_PATH] = {
  778. 'ws': [IN_PATH],
  779. '.': [BEFORE_IDENT],
  780. '[': [IN_SUB_PATH],
  781. 'eof': [AFTER_PATH]
  782. };
  783. pathStateMachine[BEFORE_IDENT] = {
  784. 'ws': [BEFORE_IDENT],
  785. 'ident': [IN_IDENT, APPEND],
  786. '0': [IN_IDENT, APPEND],
  787. 'number': [IN_IDENT, APPEND]
  788. };
  789. pathStateMachine[IN_IDENT] = {
  790. 'ident': [IN_IDENT, APPEND],
  791. '0': [IN_IDENT, APPEND],
  792. 'number': [IN_IDENT, APPEND],
  793. 'ws': [IN_PATH, PUSH],
  794. '.': [BEFORE_IDENT, PUSH],
  795. '[': [IN_SUB_PATH, PUSH],
  796. 'eof': [AFTER_PATH, PUSH]
  797. };
  798. pathStateMachine[IN_SUB_PATH] = {
  799. "'": [IN_SINGLE_QUOTE, APPEND],
  800. '"': [IN_DOUBLE_QUOTE, APPEND],
  801. '[': [IN_SUB_PATH, INC_SUB_PATH_DEPTH],
  802. ']': [IN_PATH, PUSH_SUB_PATH],
  803. 'eof': ERROR,
  804. 'else': [IN_SUB_PATH, APPEND]
  805. };
  806. pathStateMachine[IN_SINGLE_QUOTE] = {
  807. "'": [IN_SUB_PATH, APPEND],
  808. 'eof': ERROR,
  809. 'else': [IN_SINGLE_QUOTE, APPEND]
  810. };
  811. pathStateMachine[IN_DOUBLE_QUOTE] = {
  812. '"': [IN_SUB_PATH, APPEND],
  813. 'eof': ERROR,
  814. 'else': [IN_DOUBLE_QUOTE, APPEND]
  815. };
  816. /**
  817. * Check if an expression is a literal value.
  818. */
  819. var literalValueRE = /^\s?(?:true|false|-?[\d.]+|'[^']*'|"[^"]*")\s?$/;
  820. function isLiteral (exp) {
  821. return literalValueRE.test(exp)
  822. }
  823. /**
  824. * Strip quotes from a string
  825. */
  826. function stripQuotes (str) {
  827. var a = str.charCodeAt(0);
  828. var b = str.charCodeAt(str.length - 1);
  829. return a === b && (a === 0x22 || a === 0x27)
  830. ? str.slice(1, -1)
  831. : str
  832. }
  833. /**
  834. * Determine the type of a character in a keypath.
  835. */
  836. function getPathCharType (ch) {
  837. if (ch === undefined || ch === null) { return 'eof' }
  838. var code = ch.charCodeAt(0);
  839. switch (code) {
  840. case 0x5B: // [
  841. case 0x5D: // ]
  842. case 0x2E: // .
  843. case 0x22: // "
  844. case 0x27: // '
  845. return ch
  846. case 0x5F: // _
  847. case 0x24: // $
  848. case 0x2D: // -
  849. return 'ident'
  850. case 0x09: // Tab
  851. case 0x0A: // Newline
  852. case 0x0D: // Return
  853. case 0xA0: // No-break space
  854. case 0xFEFF: // Byte Order Mark
  855. case 0x2028: // Line Separator
  856. case 0x2029: // Paragraph Separator
  857. return 'ws'
  858. }
  859. return 'ident'
  860. }
  861. /**
  862. * Format a subPath, return its plain form if it is
  863. * a literal string or number. Otherwise prepend the
  864. * dynamic indicator (*).
  865. */
  866. function formatSubPath (path) {
  867. var trimmed = path.trim();
  868. // invalid leading 0
  869. if (path.charAt(0) === '0' && isNaN(path)) { return false }
  870. return isLiteral(trimmed) ? stripQuotes(trimmed) : '*' + trimmed
  871. }
  872. /**
  873. * Parse a string path into an array of segments
  874. */
  875. function parse$1 (path) {
  876. var keys = [];
  877. var index = -1;
  878. var mode = BEFORE_PATH;
  879. var subPathDepth = 0;
  880. var c;
  881. var key;
  882. var newChar;
  883. var type;
  884. var transition;
  885. var action;
  886. var typeMap;
  887. var actions = [];
  888. actions[PUSH] = function () {
  889. if (key !== undefined) {
  890. keys.push(key);
  891. key = undefined;
  892. }
  893. };
  894. actions[APPEND] = function () {
  895. if (key === undefined) {
  896. key = newChar;
  897. } else {
  898. key += newChar;
  899. }
  900. };
  901. actions[INC_SUB_PATH_DEPTH] = function () {
  902. actions[APPEND]();
  903. subPathDepth++;
  904. };
  905. actions[PUSH_SUB_PATH] = function () {
  906. if (subPathDepth > 0) {
  907. subPathDepth--;
  908. mode = IN_SUB_PATH;
  909. actions[APPEND]();
  910. } else {
  911. subPathDepth = 0;
  912. if (key === undefined) { return false }
  913. key = formatSubPath(key);
  914. if (key === false) {
  915. return false
  916. } else {
  917. actions[PUSH]();
  918. }
  919. }
  920. };
  921. function maybeUnescapeQuote () {
  922. var nextChar = path[index + 1];
  923. if ((mode === IN_SINGLE_QUOTE && nextChar === "'") ||
  924. (mode === IN_DOUBLE_QUOTE && nextChar === '"')) {
  925. index++;
  926. newChar = '\\' + nextChar;
  927. actions[APPEND]();
  928. return true
  929. }
  930. }
  931. while (mode !== null) {
  932. index++;
  933. c = path[index];
  934. if (c === '\\' && maybeUnescapeQuote()) {
  935. continue
  936. }
  937. type = getPathCharType(c);
  938. typeMap = pathStateMachine[mode];
  939. transition = typeMap[type] || typeMap['else'] || ERROR;
  940. if (transition === ERROR) {
  941. return // parse error
  942. }
  943. mode = transition[0];
  944. action = actions[transition[1]];
  945. if (action) {
  946. newChar = transition[2];
  947. newChar = newChar === undefined
  948. ? c
  949. : newChar;
  950. if (action() === false) {
  951. return
  952. }
  953. }
  954. if (mode === AFTER_PATH) {
  955. return keys
  956. }
  957. }
  958. }
  959. var I18nPath = function I18nPath () {
  960. this._cache = Object.create(null);
  961. };
  962. /**
  963. * External parse that check for a cache hit first
  964. */
  965. I18nPath.prototype.parsePath = function parsePath (path) {
  966. var hit = this._cache[path];
  967. if (!hit) {
  968. hit = parse$1(path);
  969. if (hit) {
  970. this._cache[path] = hit;
  971. }
  972. }
  973. return hit || []
  974. };
  975. /**
  976. * Get path value from path string
  977. */
  978. I18nPath.prototype.getPathValue = function getPathValue (obj, path) {
  979. if (!isObject(obj)) { return null }
  980. var paths = this.parsePath(path);
  981. if (paths.length === 0) {
  982. return null
  983. } else {
  984. var length = paths.length;
  985. var last = obj;
  986. var i = 0;
  987. while (i < length) {
  988. var value = last[paths[i]];
  989. if (value === undefined || value === null) {
  990. return null
  991. }
  992. last = value;
  993. i++;
  994. }
  995. return last
  996. }
  997. };
  998. /* */
  999. var htmlTagMatcher = /<\/?[\w\s="/.':;#-\/]+>/;
  1000. var linkKeyMatcher = /(?:@(?:\.[a-zA-Z]+)?:(?:[\w\-_|./]+|\([\w\-_:|./]+\)))/g;
  1001. var linkKeyPrefixMatcher = /^@(?:\.([a-zA-Z]+))?:/;
  1002. var bracketsMatcher = /[()]/g;
  1003. var defaultModifiers = {
  1004. 'upper': function (str) { return str.toLocaleUpperCase(); },
  1005. 'lower': function (str) { return str.toLocaleLowerCase(); },
  1006. 'capitalize': function (str) { return ("" + (str.charAt(0).toLocaleUpperCase()) + (str.substr(1))); }
  1007. };
  1008. var defaultFormatter = new BaseFormatter();
  1009. var VueI18n = function VueI18n (options) {
  1010. var this$1 = this;
  1011. if ( options === void 0 ) options = {};
  1012. // Auto install if it is not done yet and `window` has `Vue`.
  1013. // To allow users to avoid auto-installation in some cases,
  1014. // this code should be placed here. See #290
  1015. /* istanbul ignore if */
  1016. if (!Vue && typeof window !== 'undefined' && window.Vue) {
  1017. install(window.Vue);
  1018. }
  1019. var locale = options.locale || 'en-US';
  1020. var fallbackLocale = options.fallbackLocale === false
  1021. ? false
  1022. : options.fallbackLocale || 'en-US';
  1023. var messages = options.messages || {};
  1024. var dateTimeFormats = options.dateTimeFormats || options.datetimeFormats || {};
  1025. var numberFormats = options.numberFormats || {};
  1026. this._vm = null;
  1027. this._formatter = options.formatter || defaultFormatter;
  1028. this._modifiers = options.modifiers || {};
  1029. this._missing = options.missing || null;
  1030. this._root = options.root || null;
  1031. this._sync = options.sync === undefined ? true : !!options.sync;
  1032. this._fallbackRoot = options.fallbackRoot === undefined
  1033. ? true
  1034. : !!options.fallbackRoot;
  1035. this._fallbackRootWithEmptyString = options.fallbackRootWithEmptyString === undefined
  1036. ? true
  1037. : !!options.fallbackRootWithEmptyString;
  1038. this._formatFallbackMessages = options.formatFallbackMessages === undefined
  1039. ? false
  1040. : !!options.formatFallbackMessages;
  1041. this._silentTranslationWarn = options.silentTranslationWarn === undefined
  1042. ? false
  1043. : options.silentTranslationWarn;
  1044. this._silentFallbackWarn = options.silentFallbackWarn === undefined
  1045. ? false
  1046. : !!options.silentFallbackWarn;
  1047. this._dateTimeFormatters = {};
  1048. this._numberFormatters = {};
  1049. this._path = new I18nPath();
  1050. this._dataListeners = new Set();
  1051. this._componentInstanceCreatedListener = options.componentInstanceCreatedListener || null;
  1052. this._preserveDirectiveContent = options.preserveDirectiveContent === undefined
  1053. ? false
  1054. : !!options.preserveDirectiveContent;
  1055. this.pluralizationRules = options.pluralizationRules || {};
  1056. this._warnHtmlInMessage = options.warnHtmlInMessage || 'off';
  1057. this._postTranslation = options.postTranslation || null;
  1058. this._escapeParameterHtml = options.escapeParameterHtml || false;
  1059. if ('__VUE_I18N_BRIDGE__' in options) {
  1060. this.__VUE_I18N_BRIDGE__ = options.__VUE_I18N_BRIDGE__;
  1061. }
  1062. /**
  1063. * @param choice {number} a choice index given by the input to $tc: `$tc('path.to.rule', choiceIndex)`
  1064. * @param choicesLength {number} an overall amount of available choices
  1065. * @returns a final choice index
  1066. */
  1067. this.getChoiceIndex = function (choice, choicesLength) {
  1068. var thisPrototype = Object.getPrototypeOf(this$1);
  1069. if (thisPrototype && thisPrototype.getChoiceIndex) {
  1070. var prototypeGetChoiceIndex = (thisPrototype.getChoiceIndex);
  1071. return (prototypeGetChoiceIndex).call(this$1, choice, choicesLength)
  1072. }
  1073. // Default (old) getChoiceIndex implementation - english-compatible
  1074. var defaultImpl = function (_choice, _choicesLength) {
  1075. _choice = Math.abs(_choice);
  1076. if (_choicesLength === 2) {
  1077. return _choice
  1078. ? _choice > 1
  1079. ? 1
  1080. : 0
  1081. : 1
  1082. }
  1083. return _choice ? Math.min(_choice, 2) : 0
  1084. };
  1085. if (this$1.locale in this$1.pluralizationRules) {
  1086. return this$1.pluralizationRules[this$1.locale].apply(this$1, [choice, choicesLength])
  1087. } else {
  1088. return defaultImpl(choice, choicesLength)
  1089. }
  1090. };
  1091. this._exist = function (message, key) {
  1092. if (!message || !key) { return false }
  1093. if (!isNull(this$1._path.getPathValue(message, key))) { return true }
  1094. // fallback for flat key
  1095. if (message[key]) { return true }
  1096. return false
  1097. };
  1098. if (this._warnHtmlInMessage === 'warn' || this._warnHtmlInMessage === 'error') {
  1099. Object.keys(messages).forEach(function (locale) {
  1100. this$1._checkLocaleMessage(locale, this$1._warnHtmlInMessage, messages[locale]);
  1101. });
  1102. }
  1103. this._initVM({
  1104. locale: locale,
  1105. fallbackLocale: fallbackLocale,
  1106. messages: messages,
  1107. dateTimeFormats: dateTimeFormats,
  1108. numberFormats: numberFormats
  1109. });
  1110. };
  1111. var prototypeAccessors = { vm: { configurable: true },messages: { configurable: true },dateTimeFormats: { configurable: true },numberFormats: { configurable: true },availableLocales: { configurable: true },locale: { configurable: true },fallbackLocale: { configurable: true },formatFallbackMessages: { configurable: true },missing: { configurable: true },formatter: { configurable: true },silentTranslationWarn: { configurable: true },silentFallbackWarn: { configurable: true },preserveDirectiveContent: { configurable: true },warnHtmlInMessage: { configurable: true },postTranslation: { configurable: true },sync: { configurable: true } };
  1112. VueI18n.prototype._checkLocaleMessage = function _checkLocaleMessage (locale, level, message) {
  1113. var paths = [];
  1114. var fn = function (level, locale, message, paths) {
  1115. if (isPlainObject(message)) {
  1116. Object.keys(message).forEach(function (key) {
  1117. var val = message[key];
  1118. if (isPlainObject(val)) {
  1119. paths.push(key);
  1120. paths.push('.');
  1121. fn(level, locale, val, paths);
  1122. paths.pop();
  1123. paths.pop();
  1124. } else {
  1125. paths.push(key);
  1126. fn(level, locale, val, paths);
  1127. paths.pop();
  1128. }
  1129. });
  1130. } else if (isArray(message)) {
  1131. message.forEach(function (item, index) {
  1132. if (isPlainObject(item)) {
  1133. paths.push(("[" + index + "]"));
  1134. paths.push('.');
  1135. fn(level, locale, item, paths);
  1136. paths.pop();
  1137. paths.pop();
  1138. } else {
  1139. paths.push(("[" + index + "]"));
  1140. fn(level, locale, item, paths);
  1141. paths.pop();
  1142. }
  1143. });
  1144. } else if (isString(message)) {
  1145. var ret = htmlTagMatcher.test(message);
  1146. if (ret) {
  1147. var msg = "Detected HTML in message '" + message + "' of keypath '" + (paths.join('')) + "' at '" + locale + "'. Consider component interpolation with '<i18n>' to avoid XSS. See https://bit.ly/2ZqJzkp";
  1148. if (level === 'warn') {
  1149. warn(msg);
  1150. } else if (level === 'error') {
  1151. error(msg);
  1152. }
  1153. }
  1154. }
  1155. };
  1156. fn(level, locale, message, paths);
  1157. };
  1158. VueI18n.prototype._initVM = function _initVM (data) {
  1159. var silent = Vue.config.silent;
  1160. Vue.config.silent = true;
  1161. this._vm = new Vue({ data: data, __VUE18N__INSTANCE__: true });
  1162. Vue.config.silent = silent;
  1163. };
  1164. VueI18n.prototype.destroyVM = function destroyVM () {
  1165. this._vm.$destroy();
  1166. };
  1167. VueI18n.prototype.subscribeDataChanging = function subscribeDataChanging (vm) {
  1168. this._dataListeners.add(vm);
  1169. };
  1170. VueI18n.prototype.unsubscribeDataChanging = function unsubscribeDataChanging (vm) {
  1171. remove(this._dataListeners, vm);
  1172. };
  1173. VueI18n.prototype.watchI18nData = function watchI18nData () {
  1174. var this$1 = this;
  1175. return this._vm.$watch('$data', function () {
  1176. var listeners = arrayFrom(this$1._dataListeners);
  1177. var i = listeners.length;
  1178. while(i--) {
  1179. Vue.nextTick(function () {
  1180. listeners[i] && listeners[i].$forceUpdate();
  1181. });
  1182. }
  1183. }, { deep: true })
  1184. };
  1185. VueI18n.prototype.watchLocale = function watchLocale (composer) {
  1186. if (!composer) {
  1187. /* istanbul ignore if */
  1188. if (!this._sync || !this._root) { return null }
  1189. var target = this._vm;
  1190. return this._root.$i18n.vm.$watch('locale', function (val) {
  1191. target.$set(target, 'locale', val);
  1192. target.$forceUpdate();
  1193. }, { immediate: true })
  1194. } else {
  1195. // deal with vue-i18n-bridge
  1196. if (!this.__VUE_I18N_BRIDGE__) { return null }
  1197. var self = this;
  1198. var target$1 = this._vm;
  1199. return this.vm.$watch('locale', function (val) {
  1200. target$1.$set(target$1, 'locale', val);
  1201. if (self.__VUE_I18N_BRIDGE__ && composer) {
  1202. composer.locale.value = val;
  1203. }
  1204. target$1.$forceUpdate();
  1205. }, { immediate: true })
  1206. }
  1207. };
  1208. VueI18n.prototype.onComponentInstanceCreated = function onComponentInstanceCreated (newI18n) {
  1209. if (this._componentInstanceCreatedListener) {
  1210. this._componentInstanceCreatedListener(newI18n, this);
  1211. }
  1212. };
  1213. prototypeAccessors.vm.get = function () { return this._vm };
  1214. prototypeAccessors.messages.get = function () { return looseClone(this._getMessages()) };
  1215. prototypeAccessors.dateTimeFormats.get = function () { return looseClone(this._getDateTimeFormats()) };
  1216. prototypeAccessors.numberFormats.get = function () { return looseClone(this._getNumberFormats()) };
  1217. prototypeAccessors.availableLocales.get = function () { return Object.keys(this.messages).sort() };
  1218. prototypeAccessors.locale.get = function () { return this._vm.locale };
  1219. prototypeAccessors.locale.set = function (locale) {
  1220. this._vm.$set(this._vm, 'locale', locale);
  1221. };
  1222. prototypeAccessors.fallbackLocale.get = function () { return this._vm.fallbackLocale };
  1223. prototypeAccessors.fallbackLocale.set = function (locale) {
  1224. this._localeChainCache = {};
  1225. this._vm.$set(this._vm, 'fallbackLocale', locale);
  1226. };
  1227. prototypeAccessors.formatFallbackMessages.get = function () { return this._formatFallbackMessages };
  1228. prototypeAccessors.formatFallbackMessages.set = function (fallback) { this._formatFallbackMessages = fallback; };
  1229. prototypeAccessors.missing.get = function () { return this._missing };
  1230. prototypeAccessors.missing.set = function (handler) { this._missing = handler; };
  1231. prototypeAccessors.formatter.get = function () { return this._formatter };
  1232. prototypeAccessors.formatter.set = function (formatter) { this._formatter = formatter; };
  1233. prototypeAccessors.silentTranslationWarn.get = function () { return this._silentTranslationWarn };
  1234. prototypeAccessors.silentTranslationWarn.set = function (silent) { this._silentTranslationWarn = silent; };
  1235. prototypeAccessors.silentFallbackWarn.get = function () { return this._silentFallbackWarn };
  1236. prototypeAccessors.silentFallbackWarn.set = function (silent) { this._silentFallbackWarn = silent; };
  1237. prototypeAccessors.preserveDirectiveContent.get = function () { return this._preserveDirectiveContent };
  1238. prototypeAccessors.preserveDirectiveContent.set = function (preserve) { this._preserveDirectiveContent = preserve; };
  1239. prototypeAccessors.warnHtmlInMessage.get = function () { return this._warnHtmlInMessage };
  1240. prototypeAccessors.warnHtmlInMessage.set = function (level) {
  1241. var this$1 = this;
  1242. var orgLevel = this._warnHtmlInMessage;
  1243. this._warnHtmlInMessage = level;
  1244. if (orgLevel !== level && (level === 'warn' || level === 'error')) {
  1245. var messages = this._getMessages();
  1246. Object.keys(messages).forEach(function (locale) {
  1247. this$1._checkLocaleMessage(locale, this$1._warnHtmlInMessage, messages[locale]);
  1248. });
  1249. }
  1250. };
  1251. prototypeAccessors.postTranslation.get = function () { return this._postTranslation };
  1252. prototypeAccessors.postTranslation.set = function (handler) { this._postTranslation = handler; };
  1253. prototypeAccessors.sync.get = function () { return this._sync };
  1254. prototypeAccessors.sync.set = function (val) { this._sync = val; };
  1255. VueI18n.prototype._getMessages = function _getMessages () { return this._vm.messages };
  1256. VueI18n.prototype._getDateTimeFormats = function _getDateTimeFormats () { return this._vm.dateTimeFormats };
  1257. VueI18n.prototype._getNumberFormats = function _getNumberFormats () { return this._vm.numberFormats };
  1258. VueI18n.prototype._warnDefault = function _warnDefault (locale, key, result, vm, values, interpolateMode) {
  1259. if (!isNull(result)) { return result }
  1260. if (this._missing) {
  1261. var missingRet = this._missing.apply(null, [locale, key, vm, values]);
  1262. if (isString(missingRet)) {
  1263. return missingRet
  1264. }
  1265. } else {
  1266. if (!this._isSilentTranslationWarn(key)) {
  1267. warn(
  1268. "Cannot translate the value of keypath '" + key + "'. " +
  1269. 'Use the value of keypath as default.'
  1270. );
  1271. }
  1272. }
  1273. if (this._formatFallbackMessages) {
  1274. var parsedArgs = parseArgs.apply(void 0, values);
  1275. return this._render(key, interpolateMode, parsedArgs.params, key)
  1276. } else {
  1277. return key
  1278. }
  1279. };
  1280. VueI18n.prototype._isFallbackRoot = function _isFallbackRoot (val) {
  1281. return (this._fallbackRootWithEmptyString? !val : isNull(val)) && !isNull(this._root) && this._fallbackRoot
  1282. };
  1283. VueI18n.prototype._isSilentFallbackWarn = function _isSilentFallbackWarn (key) {
  1284. return this._silentFallbackWarn instanceof RegExp
  1285. ? this._silentFallbackWarn.test(key)
  1286. : this._silentFallbackWarn
  1287. };
  1288. VueI18n.prototype._isSilentFallback = function _isSilentFallback (locale, key) {
  1289. return this._isSilentFallbackWarn(key) && (this._isFallbackRoot() || locale !== this.fallbackLocale)
  1290. };
  1291. VueI18n.prototype._isSilentTranslationWarn = function _isSilentTranslationWarn (key) {
  1292. return this._silentTranslationWarn instanceof RegExp
  1293. ? this._silentTranslationWarn.test(key)
  1294. : this._silentTranslationWarn
  1295. };
  1296. VueI18n.prototype._interpolate = function _interpolate (
  1297. locale,
  1298. message,
  1299. key,
  1300. host,
  1301. interpolateMode,
  1302. values,
  1303. visitedLinkStack
  1304. ) {
  1305. if (!message) { return null }
  1306. var pathRet = this._path.getPathValue(message, key);
  1307. if (isArray(pathRet) || isPlainObject(pathRet)) { return pathRet }
  1308. var ret;
  1309. if (isNull(pathRet)) {
  1310. /* istanbul ignore else */
  1311. if (isPlainObject(message)) {
  1312. ret = message[key];
  1313. if (!(isString(ret) || isFunction(ret))) {
  1314. if (!this._isSilentTranslationWarn(key) && !this._isSilentFallback(locale, key)) {
  1315. warn(("Value of key '" + key + "' is not a string or function !"));
  1316. }
  1317. return null
  1318. }
  1319. } else {
  1320. return null
  1321. }
  1322. } else {
  1323. /* istanbul ignore else */
  1324. if (isString(pathRet) || isFunction(pathRet)) {
  1325. ret = pathRet;
  1326. } else {
  1327. if (!this._isSilentTranslationWarn(key) && !this._isSilentFallback(locale, key)) {
  1328. warn(("Value of key '" + key + "' is not a string or function!"));
  1329. }
  1330. return null
  1331. }
  1332. }
  1333. // Check for the existence of links within the translated string
  1334. if (isString(ret) && (ret.indexOf('@:') >= 0 || ret.indexOf('@.') >= 0)) {
  1335. ret = this._link(locale, message, ret, host, 'raw', values, visitedLinkStack);
  1336. }
  1337. return this._render(ret, interpolateMode, values, key)
  1338. };
  1339. VueI18n.prototype._link = function _link (
  1340. locale,
  1341. message,
  1342. str,
  1343. host,
  1344. interpolateMode,
  1345. values,
  1346. visitedLinkStack
  1347. ) {
  1348. var ret = str;
  1349. // Match all the links within the local
  1350. // We are going to replace each of
  1351. // them with its translation
  1352. var matches = ret.match(linkKeyMatcher);
  1353. // eslint-disable-next-line no-autofix/prefer-const
  1354. for (var idx in matches) {
  1355. // ie compatible: filter custom array
  1356. // prototype method
  1357. if (!matches.hasOwnProperty(idx)) {
  1358. continue
  1359. }
  1360. var link = matches[idx];
  1361. var linkKeyPrefixMatches = link.match(linkKeyPrefixMatcher);
  1362. var linkPrefix = linkKeyPrefixMatches[0];
  1363. var formatterName = linkKeyPrefixMatches[1];
  1364. // Remove the leading @:, @.case: and the brackets
  1365. var linkPlaceholder = link.replace(linkPrefix, '').replace(bracketsMatcher, '');
  1366. if (includes(visitedLinkStack, linkPlaceholder)) {
  1367. {
  1368. warn(("Circular reference found. \"" + link + "\" is already visited in the chain of " + (visitedLinkStack.reverse().join(' <- '))));
  1369. }
  1370. return ret
  1371. }
  1372. visitedLinkStack.push(linkPlaceholder);
  1373. // Translate the link
  1374. var translated = this._interpolate(
  1375. locale, message, linkPlaceholder, host,
  1376. interpolateMode === 'raw' ? 'string' : interpolateMode,
  1377. interpolateMode === 'raw' ? undefined : values,
  1378. visitedLinkStack
  1379. );
  1380. if (this._isFallbackRoot(translated)) {
  1381. if (!this._isSilentTranslationWarn(linkPlaceholder)) {
  1382. warn(("Fall back to translate the link placeholder '" + linkPlaceholder + "' with root locale."));
  1383. }
  1384. /* istanbul ignore if */
  1385. if (!this._root) { throw Error('unexpected error') }
  1386. var root = this._root.$i18n;
  1387. translated = root._translate(
  1388. root._getMessages(), root.locale, root.fallbackLocale,
  1389. linkPlaceholder, host, interpolateMode, values
  1390. );
  1391. }
  1392. translated = this._warnDefault(
  1393. locale, linkPlaceholder, translated, host,
  1394. isArray(values) ? values : [values],
  1395. interpolateMode
  1396. );
  1397. if (this._modifiers.hasOwnProperty(formatterName)) {
  1398. translated = this._modifiers[formatterName](translated);
  1399. } else if (defaultModifiers.hasOwnProperty(formatterName)) {
  1400. translated = defaultModifiers[formatterName](translated);
  1401. }
  1402. visitedLinkStack.pop();
  1403. // Replace the link with the translated
  1404. ret = !translated ? ret : ret.replace(link, translated);
  1405. }
  1406. return ret
  1407. };
  1408. VueI18n.prototype._createMessageContext = function _createMessageContext (values, formatter, path, interpolateMode) {
  1409. var this$1 = this;
  1410. var _list = isArray(values) ? values : [];
  1411. var _named = isObject(values) ? values : {};
  1412. var list = function (index) { return _list[index]; };
  1413. var named = function (key) { return _named[key]; };
  1414. var messages = this._getMessages();
  1415. var locale = this.locale;
  1416. return {
  1417. list: list,
  1418. named: named,
  1419. values: values,
  1420. formatter: formatter,
  1421. path: path,
  1422. messages: messages,
  1423. locale: locale,
  1424. linked: function (linkedKey) { return this$1._interpolate(locale, messages[locale] || {}, linkedKey, null, interpolateMode, undefined, [linkedKey]); }
  1425. }
  1426. };
  1427. VueI18n.prototype._render = function _render (message, interpolateMode, values, path) {
  1428. if (isFunction(message)) {
  1429. return message(
  1430. this._createMessageContext(values, this._formatter || defaultFormatter, path, interpolateMode)
  1431. )
  1432. }
  1433. var ret = this._formatter.interpolate(message, values, path);
  1434. // If the custom formatter refuses to work - apply the default one
  1435. if (!ret) {
  1436. ret = defaultFormatter.interpolate(message, values, path);
  1437. }
  1438. // if interpolateMode is **not** 'string' ('row'),
  1439. // return the compiled data (e.g. ['foo', VNode, 'bar']) with formatter
  1440. return interpolateMode === 'string' && !isString(ret) ? ret.join('') : ret
  1441. };
  1442. VueI18n.prototype._appendItemToChain = function _appendItemToChain (chain, item, blocks) {
  1443. var follow = false;
  1444. if (!includes(chain, item)) {
  1445. follow = true;
  1446. if (item) {
  1447. follow = item[item.length - 1] !== '!';
  1448. item = item.replace(/!/g, '');
  1449. chain.push(item);
  1450. if (blocks && blocks[item]) {
  1451. follow = blocks[item];
  1452. }
  1453. }
  1454. }
  1455. return follow
  1456. };
  1457. VueI18n.prototype._appendLocaleToChain = function _appendLocaleToChain (chain, locale, blocks) {
  1458. var follow;
  1459. var tokens = locale.split('-');
  1460. do {
  1461. var item = tokens.join('-');
  1462. follow = this._appendItemToChain(chain, item, blocks);
  1463. tokens.splice(-1, 1);
  1464. } while (tokens.length && (follow === true))
  1465. return follow
  1466. };
  1467. VueI18n.prototype._appendBlockToChain = function _appendBlockToChain (chain, block, blocks) {
  1468. var follow = true;
  1469. for (var i = 0; (i < block.length) && (isBoolean(follow)); i++) {
  1470. var locale = block[i];
  1471. if (isString(locale)) {
  1472. follow = this._appendLocaleToChain(chain, locale, blocks);
  1473. }
  1474. }
  1475. return follow
  1476. };
  1477. VueI18n.prototype._getLocaleChain = function _getLocaleChain (start, fallbackLocale) {
  1478. if (start === '') { return [] }
  1479. if (!this._localeChainCache) {
  1480. this._localeChainCache = {};
  1481. }
  1482. var chain = this._localeChainCache[start];
  1483. if (!chain) {
  1484. if (!fallbackLocale) {
  1485. fallbackLocale = this.fallbackLocale;
  1486. }
  1487. chain = [];
  1488. // first block defined by start
  1489. var block = [start];
  1490. // while any intervening block found
  1491. while (isArray(block)) {
  1492. block = this._appendBlockToChain(
  1493. chain,
  1494. block,
  1495. fallbackLocale
  1496. );
  1497. }
  1498. // last block defined by default
  1499. var defaults;
  1500. if (isArray(fallbackLocale)) {
  1501. defaults = fallbackLocale;
  1502. } else if (isObject(fallbackLocale)) {
  1503. /* $FlowFixMe */
  1504. if (fallbackLocale['default']) {
  1505. defaults = fallbackLocale['default'];
  1506. } else {
  1507. defaults = null;
  1508. }
  1509. } else {
  1510. defaults = fallbackLocale;
  1511. }
  1512. // convert defaults to array
  1513. if (isString(defaults)) {
  1514. block = [defaults];
  1515. } else {
  1516. block = defaults;
  1517. }
  1518. if (block) {
  1519. this._appendBlockToChain(
  1520. chain,
  1521. block,
  1522. null
  1523. );
  1524. }
  1525. this._localeChainCache[start] = chain;
  1526. }
  1527. return chain
  1528. };
  1529. VueI18n.prototype._translate = function _translate (
  1530. messages,
  1531. locale,
  1532. fallback,
  1533. key,
  1534. host,
  1535. interpolateMode,
  1536. args
  1537. ) {
  1538. var chain = this._getLocaleChain(locale, fallback);
  1539. var res;
  1540. for (var i = 0; i < chain.length; i++) {
  1541. var step = chain[i];
  1542. res =
  1543. this._interpolate(step, messages[step], key, host, interpolateMode, args, [key]);
  1544. if (!isNull(res)) {
  1545. if (step !== locale && "development" !== 'production' && !this._isSilentTranslationWarn(key) && !this._isSilentFallbackWarn(key)) {
  1546. warn(("Fall back to translate the keypath '" + key + "' with '" + step + "' locale."));
  1547. }
  1548. return res
  1549. }
  1550. }
  1551. return null
  1552. };
  1553. VueI18n.prototype._t = function _t (key, _locale, messages, host) {
  1554. var ref;
  1555. var values = [], len = arguments.length - 4;
  1556. while ( len-- > 0 ) values[ len ] = arguments[ len + 4 ];
  1557. if (!key) { return '' }
  1558. var parsedArgs = parseArgs.apply(void 0, values);
  1559. if(this._escapeParameterHtml) {
  1560. parsedArgs.params = escapeParams(parsedArgs.params);
  1561. }
  1562. var locale = parsedArgs.locale || _locale;
  1563. var ret = this._translate(
  1564. messages, locale, this.fallbackLocale, key,
  1565. host, 'string', parsedArgs.params
  1566. );
  1567. if (this._isFallbackRoot(ret)) {
  1568. if (!this._isSilentTranslationWarn(key) && !this._isSilentFallbackWarn(key)) {
  1569. warn(("Fall back to translate the keypath '" + key + "' with root locale."));
  1570. }
  1571. /* istanbul ignore if */
  1572. if (!this._root) { throw Error('unexpected error') }
  1573. return (ref = this._root).$t.apply(ref, [ key ].concat( values ))
  1574. } else {
  1575. ret = this._warnDefault(locale, key, ret, host, values, 'string');
  1576. if (this._postTranslation && ret !== null && ret !== undefined) {
  1577. ret = this._postTranslation(ret, key);
  1578. }
  1579. return ret
  1580. }
  1581. };
  1582. VueI18n.prototype.t = function t (key) {
  1583. var ref;
  1584. var values = [], len = arguments.length - 1;
  1585. while ( len-- > 0 ) values[ len ] = arguments[ len + 1 ];
  1586. return (ref = this)._t.apply(ref, [ key, this.locale, this._getMessages(), null ].concat( values ))
  1587. };
  1588. VueI18n.prototype._i = function _i (key, locale, messages, host, values) {
  1589. var ret =
  1590. this._translate(messages, locale, this.fallbackLocale, key, host, 'raw', values);
  1591. if (this._isFallbackRoot(ret)) {
  1592. if (!this._isSilentTranslationWarn(key)) {
  1593. warn(("Fall back to interpolate the keypath '" + key + "' with root locale."));
  1594. }
  1595. if (!this._root) { throw Error('unexpected error') }
  1596. return this._root.$i18n.i(key, locale, values)
  1597. } else {
  1598. return this._warnDefault(locale, key, ret, host, [values], 'raw')
  1599. }
  1600. };
  1601. VueI18n.prototype.i = function i (key, locale, values) {
  1602. /* istanbul ignore if */
  1603. if (!key) { return '' }
  1604. if (!isString(locale)) {
  1605. locale = this.locale;
  1606. }
  1607. return this._i(key, locale, this._getMessages(), null, values)
  1608. };
  1609. VueI18n.prototype._tc = function _tc (
  1610. key,
  1611. _locale,
  1612. messages,
  1613. host,
  1614. choice
  1615. ) {
  1616. var ref;
  1617. var values = [], len = arguments.length - 5;
  1618. while ( len-- > 0 ) values[ len ] = arguments[ len + 5 ];
  1619. if (!key) { return '' }
  1620. if (choice === undefined) {
  1621. choice = 1;
  1622. }
  1623. var predefined = { 'count': choice, 'n': choice };
  1624. var parsedArgs = parseArgs.apply(void 0, values);
  1625. parsedArgs.params = Object.assign(predefined, parsedArgs.params);
  1626. values = parsedArgs.locale === null ? [parsedArgs.params] : [parsedArgs.locale, parsedArgs.params];
  1627. return this.fetchChoice((ref = this)._t.apply(ref, [ key, _locale, messages, host ].concat( values )), choice)
  1628. };
  1629. VueI18n.prototype.fetchChoice = function fetchChoice (message, choice) {
  1630. /* istanbul ignore if */
  1631. if (!message || !isString(message)) { return null }
  1632. var choices = message.split('|');
  1633. choice = this.getChoiceIndex(choice, choices.length);
  1634. if (!choices[choice]) { return message }
  1635. return choices[choice].trim()
  1636. };
  1637. VueI18n.prototype.tc = function tc (key, choice) {
  1638. var ref;
  1639. var values = [], len = arguments.length - 2;
  1640. while ( len-- > 0 ) values[ len ] = arguments[ len + 2 ];
  1641. return (ref = this)._tc.apply(ref, [ key, this.locale, this._getMessages(), null, choice ].concat( values ))
  1642. };
  1643. VueI18n.prototype._te = function _te (key, locale, messages) {
  1644. var args = [], len = arguments.length - 3;
  1645. while ( len-- > 0 ) args[ len ] = arguments[ len + 3 ];
  1646. var _locale = parseArgs.apply(void 0, args).locale || locale;
  1647. return this._exist(messages[_locale], key)
  1648. };
  1649. VueI18n.prototype.te = function te (key, locale) {
  1650. return this._te(key, this.locale, this._getMessages(), locale)
  1651. };
  1652. VueI18n.prototype.getLocaleMessage = function getLocaleMessage (locale) {
  1653. return looseClone(this._vm.messages[locale] || {})
  1654. };
  1655. VueI18n.prototype.setLocaleMessage = function setLocaleMessage (locale, message) {
  1656. if (this._warnHtmlInMessage === 'warn' || this._warnHtmlInMessage === 'error') {
  1657. this._checkLocaleMessage(locale, this._warnHtmlInMessage, message);
  1658. }
  1659. this._vm.$set(this._vm.messages, locale, message);
  1660. };
  1661. VueI18n.prototype.mergeLocaleMessage = function mergeLocaleMessage (locale, message) {
  1662. if (this._warnHtmlInMessage === 'warn' || this._warnHtmlInMessage === 'error') {
  1663. this._checkLocaleMessage(locale, this._warnHtmlInMessage, message);
  1664. }
  1665. this._vm.$set(this._vm.messages, locale, merge(
  1666. typeof this._vm.messages[locale] !== 'undefined' && Object.keys(this._vm.messages[locale]).length
  1667. ? Object.assign({}, this._vm.messages[locale])
  1668. : {},
  1669. message
  1670. ));
  1671. };
  1672. VueI18n.prototype.getDateTimeFormat = function getDateTimeFormat (locale) {
  1673. return looseClone(this._vm.dateTimeFormats[locale] || {})
  1674. };
  1675. VueI18n.prototype.setDateTimeFormat = function setDateTimeFormat (locale, format) {
  1676. this._vm.$set(this._vm.dateTimeFormats, locale, format);
  1677. this._clearDateTimeFormat(locale, format);
  1678. };
  1679. VueI18n.prototype.mergeDateTimeFormat = function mergeDateTimeFormat (locale, format) {
  1680. this._vm.$set(this._vm.dateTimeFormats, locale, merge(this._vm.dateTimeFormats[locale] || {}, format));
  1681. this._clearDateTimeFormat(locale, format);
  1682. };
  1683. VueI18n.prototype._clearDateTimeFormat = function _clearDateTimeFormat (locale, format) {
  1684. // eslint-disable-next-line no-autofix/prefer-const
  1685. for (var key in format) {
  1686. var id = locale + "__" + key;
  1687. if (!this._dateTimeFormatters.hasOwnProperty(id)) {
  1688. continue
  1689. }
  1690. delete this._dateTimeFormatters[id];
  1691. }
  1692. };
  1693. VueI18n.prototype._localizeDateTime = function _localizeDateTime (
  1694. value,
  1695. locale,
  1696. fallback,
  1697. dateTimeFormats,
  1698. key
  1699. ) {
  1700. var _locale = locale;
  1701. var formats = dateTimeFormats[_locale];
  1702. var chain = this._getLocaleChain(locale, fallback);
  1703. for (var i = 0; i < chain.length; i++) {
  1704. var current = _locale;
  1705. var step = chain[i];
  1706. formats = dateTimeFormats[step];
  1707. _locale = step;
  1708. // fallback locale
  1709. if (isNull(formats) || isNull(formats[key])) {
  1710. if (step !== locale && "development" !== 'production' && !this._isSilentTranslationWarn(key) && !this._isSilentFallbackWarn(key)) {
  1711. warn(("Fall back to '" + step + "' datetime formats from '" + current + "' datetime formats."));
  1712. }
  1713. } else {
  1714. break
  1715. }
  1716. }
  1717. if (isNull(formats) || isNull(formats[key])) {
  1718. return null
  1719. } else {
  1720. var format = formats[key];
  1721. var id = _locale + "__" + key;
  1722. var formatter = this._dateTimeFormatters[id];
  1723. if (!formatter) {
  1724. formatter = this._dateTimeFormatters[id] = new Intl.DateTimeFormat(_locale, format);
  1725. }
  1726. return formatter.format(value)
  1727. }
  1728. };
  1729. VueI18n.prototype._d = function _d (value, locale, key) {
  1730. /* istanbul ignore if */
  1731. if (!VueI18n.availabilities.dateTimeFormat) {
  1732. warn('Cannot format a Date value due to not supported Intl.DateTimeFormat.');
  1733. return ''
  1734. }
  1735. if (!key) {
  1736. return new Intl.DateTimeFormat(locale).format(value)
  1737. }
  1738. var ret =
  1739. this._localizeDateTime(value, locale, this.fallbackLocale, this._getDateTimeFormats(), key);
  1740. if (this._isFallbackRoot(ret)) {
  1741. if (!this._isSilentTranslationWarn(key) && !this._isSilentFallbackWarn(key)) {
  1742. warn(("Fall back to datetime localization of root: key '" + key + "'."));
  1743. }
  1744. /* istanbul ignore if */
  1745. if (!this._root) { throw Error('unexpected error') }
  1746. return this._root.$i18n.d(value, key, locale)
  1747. } else {
  1748. return ret || ''
  1749. }
  1750. };
  1751. VueI18n.prototype.d = function d (value) {
  1752. var args = [], len = arguments.length - 1;
  1753. while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
  1754. var locale = this.locale;
  1755. var key = null;
  1756. if (args.length === 1) {
  1757. if (isString(args[0])) {
  1758. key = args[0];
  1759. } else if (isObject(args[0])) {
  1760. if (args[0].locale) {
  1761. locale = args[0].locale;
  1762. }
  1763. if (args[0].key) {
  1764. key = args[0].key;
  1765. }
  1766. }
  1767. } else if (args.length === 2) {
  1768. if (isString(args[0])) {
  1769. key = args[0];
  1770. }
  1771. if (isString(args[1])) {
  1772. locale = args[1];
  1773. }
  1774. }
  1775. return this._d(value, locale, key)
  1776. };
  1777. VueI18n.prototype.getNumberFormat = function getNumberFormat (locale) {
  1778. return looseClone(this._vm.numberFormats[locale] || {})
  1779. };
  1780. VueI18n.prototype.setNumberFormat = function setNumberFormat (locale, format) {
  1781. this._vm.$set(this._vm.numberFormats, locale, format);
  1782. this._clearNumberFormat(locale, format);
  1783. };
  1784. VueI18n.prototype.mergeNumberFormat = function mergeNumberFormat (locale, format) {
  1785. this._vm.$set(this._vm.numberFormats, locale, merge(this._vm.numberFormats[locale] || {}, format));
  1786. this._clearNumberFormat(locale, format);
  1787. };
  1788. VueI18n.prototype._clearNumberFormat = function _clearNumberFormat (locale, format) {
  1789. // eslint-disable-next-line no-autofix/prefer-const
  1790. for (var key in format) {
  1791. var id = locale + "__" + key;
  1792. if (!this._numberFormatters.hasOwnProperty(id)) {
  1793. continue
  1794. }
  1795. delete this._numberFormatters[id];
  1796. }
  1797. };
  1798. VueI18n.prototype._getNumberFormatter = function _getNumberFormatter (
  1799. value,
  1800. locale,
  1801. fallback,
  1802. numberFormats,
  1803. key,
  1804. options
  1805. ) {
  1806. var _locale = locale;
  1807. var formats = numberFormats[_locale];
  1808. var chain = this._getLocaleChain(locale, fallback);
  1809. for (var i = 0; i < chain.length; i++) {
  1810. var current = _locale;
  1811. var step = chain[i];
  1812. formats = numberFormats[step];
  1813. _locale = step;
  1814. // fallback locale
  1815. if (isNull(formats) || isNull(formats[key])) {
  1816. if (step !== locale && "development" !== 'production' && !this._isSilentTranslationWarn(key) && !this._isSilentFallbackWarn(key)) {
  1817. warn(("Fall back to '" + step + "' number formats from '" + current + "' number formats."));
  1818. }
  1819. } else {
  1820. break
  1821. }
  1822. }
  1823. if (isNull(formats) || isNull(formats[key])) {
  1824. return null
  1825. } else {
  1826. var format = formats[key];
  1827. var formatter;
  1828. if (options) {
  1829. // If options specified - create one time number formatter
  1830. formatter = new Intl.NumberFormat(_locale, Object.assign({}, format, options));
  1831. } else {
  1832. var id = _locale + "__" + key;
  1833. formatter = this._numberFormatters[id];
  1834. if (!formatter) {
  1835. formatter = this._numberFormatters[id] = new Intl.NumberFormat(_locale, format);
  1836. }
  1837. }
  1838. return formatter
  1839. }
  1840. };
  1841. VueI18n.prototype._n = function _n (value, locale, key, options) {
  1842. /* istanbul ignore if */
  1843. if (!VueI18n.availabilities.numberFormat) {
  1844. {
  1845. warn('Cannot format a Number value due to not supported Intl.NumberFormat.');
  1846. }
  1847. return ''
  1848. }
  1849. if (!key) {
  1850. var nf = !options ? new Intl.NumberFormat(locale) : new Intl.NumberFormat(locale, options);
  1851. return nf.format(value)
  1852. }
  1853. var formatter = this._getNumberFormatter(value, locale, this.fallbackLocale, this._getNumberFormats(), key, options);
  1854. var ret = formatter && formatter.format(value);
  1855. if (this._isFallbackRoot(ret)) {
  1856. if (!this._isSilentTranslationWarn(key) && !this._isSilentFallbackWarn(key)) {
  1857. warn(("Fall back to number localization of root: key '" + key + "'."));
  1858. }
  1859. /* istanbul ignore if */
  1860. if (!this._root) { throw Error('unexpected error') }
  1861. return this._root.$i18n.n(value, Object.assign({}, { key: key, locale: locale }, options))
  1862. } else {
  1863. return ret || ''
  1864. }
  1865. };
  1866. VueI18n.prototype.n = function n (value) {
  1867. var args = [], len = arguments.length - 1;
  1868. while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ];
  1869. var locale = this.locale;
  1870. var key = null;
  1871. var options = null;
  1872. if (args.length === 1) {
  1873. if (isString(args[0])) {
  1874. key = args[0];
  1875. } else if (isObject(args[0])) {
  1876. if (args[0].locale) {
  1877. locale = args[0].locale;
  1878. }
  1879. if (args[0].key) {
  1880. key = args[0].key;
  1881. }
  1882. // Filter out number format options only
  1883. options = Object.keys(args[0]).reduce(function (acc, key) {
  1884. var obj;
  1885. if (includes(numberFormatKeys, key)) {
  1886. return Object.assign({}, acc, ( obj = {}, obj[key] = args[0][key], obj ))
  1887. }
  1888. return acc
  1889. }, null);
  1890. }
  1891. } else if (args.length === 2) {
  1892. if (isString(args[0])) {
  1893. key = args[0];
  1894. }
  1895. if (isString(args[1])) {
  1896. locale = args[1];
  1897. }
  1898. }
  1899. return this._n(value, locale, key, options)
  1900. };
  1901. VueI18n.prototype._ntp = function _ntp (value, locale, key, options) {
  1902. /* istanbul ignore if */
  1903. if (!VueI18n.availabilities.numberFormat) {
  1904. {
  1905. warn('Cannot format to parts a Number value due to not supported Intl.NumberFormat.');
  1906. }
  1907. return []
  1908. }
  1909. if (!key) {
  1910. var nf = !options ? new Intl.NumberFormat(locale) : new Intl.NumberFormat(locale, options);
  1911. return nf.formatToParts(value)
  1912. }
  1913. var formatter = this._getNumberFormatter(value, locale, this.fallbackLocale, this._getNumberFormats(), key, options);
  1914. var ret = formatter && formatter.formatToParts(value);
  1915. if (this._isFallbackRoot(ret)) {
  1916. if (!this._isSilentTranslationWarn(key)) {
  1917. warn(("Fall back to format number to parts of root: key '" + key + "' ."));
  1918. }
  1919. /* istanbul ignore if */
  1920. if (!this._root) { throw Error('unexpected error') }
  1921. return this._root.$i18n._ntp(value, locale, key, options)
  1922. } else {
  1923. return ret || []
  1924. }
  1925. };
  1926. Object.defineProperties( VueI18n.prototype, prototypeAccessors );
  1927. var availabilities;
  1928. // $FlowFixMe
  1929. Object.defineProperty(VueI18n, 'availabilities', {
  1930. get: function get () {
  1931. if (!availabilities) {
  1932. var intlDefined = typeof Intl !== 'undefined';
  1933. availabilities = {
  1934. dateTimeFormat: intlDefined && typeof Intl.DateTimeFormat !== 'undefined',
  1935. numberFormat: intlDefined && typeof Intl.NumberFormat !== 'undefined'
  1936. };
  1937. }
  1938. return availabilities
  1939. }
  1940. });
  1941. VueI18n.install = install;
  1942. VueI18n.version = '8.27.2';
  1943. return VueI18n;
  1944. })));