vue-i18n.common.js 63 KB

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