vue-i18n.common.js 58 KB

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