vue-i18n.esm.browser.js 53 KB

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