vue-i18n.esm.browser.js 57 KB

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