vue-i18n.common.js 59 KB

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