i18nJs.js 63 KB

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