vue-i18n.esm.js 59 KB

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