vue-i18n.js 62 KB

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