vue-i18n.esm.browser.js 54 KB

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