vue-i18n.js 63 KB

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