vue-i18n.js 67 KB

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