vue-i18n.common.js 64 KB

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