vue-i18n.esm.js 60 KB

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