vue-i18n.esm.js 64 KB

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