vue-i18n.js 61 KB

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