vue-i18n.esm.js 58 KB

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