vue-i18n.cjs.prod.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  1. /*!
  2. * vue-i18n v9.1.9
  3. * (c) 2021 kazuya kawaguchi
  4. * Released under the MIT License.
  5. */
  6. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. var coreBase = require('@intlify/core-base');
  9. var vue = require('vue');
  10. var shared = require('@intlify/shared');
  11. /**
  12. * Vue I18n Version
  13. *
  14. * @remarks
  15. * Semver format. Same format as the package.json `version` field.
  16. *
  17. * @VueI18nGeneral
  18. */
  19. const VERSION = '9.1.9';
  20. function createI18nError(code, ...args) {
  21. return coreBase.createCompileError(code, null, undefined);
  22. }
  23. const TransrateVNodeSymbol = shared.makeSymbol('__transrateVNode');
  24. const DatetimePartsSymbol = shared.makeSymbol('__datetimeParts');
  25. const NumberPartsSymbol = shared.makeSymbol('__numberParts');
  26. shared.makeSymbol('__enableEmitter');
  27. shared.makeSymbol('__disableEmitter');
  28. const SetPluralRulesSymbol = shared.makeSymbol('__setPluralRules');
  29. shared.makeSymbol('__intlifyMeta');
  30. const InejctWithOption = shared.makeSymbol('__injectWithOption');
  31. let composerID = 0;
  32. function defineCoreMissingHandler(missing) {
  33. return ((ctx, locale, key, type) => {
  34. return missing(locale, key, vue.getCurrentInstance() || undefined, type);
  35. });
  36. }
  37. function getLocaleMessages(locale, options) {
  38. const { messages, __i18n } = options;
  39. // prettier-ignore
  40. const ret = shared.isPlainObject(messages)
  41. ? messages
  42. : shared.isArray(__i18n)
  43. ? {}
  44. : { [locale]: {} };
  45. // merge locale messages of i18n custom block
  46. if (shared.isArray(__i18n)) {
  47. __i18n.forEach(({ locale, resource }) => {
  48. if (locale) {
  49. ret[locale] = ret[locale] || {};
  50. deepCopy(resource, ret[locale]);
  51. }
  52. else {
  53. deepCopy(resource, ret);
  54. }
  55. });
  56. }
  57. // handle messages for flat json
  58. if (options.flatJson) {
  59. for (const key in ret) {
  60. if (shared.hasOwn(ret, key)) {
  61. coreBase.handleFlatJson(ret[key]);
  62. }
  63. }
  64. }
  65. return ret;
  66. }
  67. const isNotObjectOrIsArray = (val) => !shared.isObject(val) || shared.isArray(val);
  68. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  69. function deepCopy(src, des) {
  70. // src and des should both be objects, and non of then can be a array
  71. if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) {
  72. throw createI18nError(20 /* INVALID_VALUE */);
  73. }
  74. for (const key in src) {
  75. if (shared.hasOwn(src, key)) {
  76. if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) {
  77. // replace with src[key] when:
  78. // src[key] or des[key] is not a object, or
  79. // src[key] or des[key] is a array
  80. des[key] = src[key];
  81. }
  82. else {
  83. // src[key] and des[key] are both object, merge them
  84. deepCopy(src[key], des[key]);
  85. }
  86. }
  87. }
  88. }
  89. /**
  90. * Create composer interface factory
  91. *
  92. * @internal
  93. */
  94. function createComposer(options = {}) {
  95. const { __root } = options;
  96. const _isGlobal = __root === undefined;
  97. let _inheritLocale = shared.isBoolean(options.inheritLocale)
  98. ? options.inheritLocale
  99. : true;
  100. const _locale = vue.ref(
  101. // prettier-ignore
  102. __root && _inheritLocale
  103. ? __root.locale.value
  104. : shared.isString(options.locale)
  105. ? options.locale
  106. : 'en-US');
  107. const _fallbackLocale = vue.ref(
  108. // prettier-ignore
  109. __root && _inheritLocale
  110. ? __root.fallbackLocale.value
  111. : shared.isString(options.fallbackLocale) ||
  112. shared.isArray(options.fallbackLocale) ||
  113. shared.isPlainObject(options.fallbackLocale) ||
  114. options.fallbackLocale === false
  115. ? options.fallbackLocale
  116. : _locale.value);
  117. const _messages = vue.ref(getLocaleMessages(_locale.value, options));
  118. const _datetimeFormats = vue.ref(shared.isPlainObject(options.datetimeFormats)
  119. ? options.datetimeFormats
  120. : { [_locale.value]: {} });
  121. const _numberFormats = vue.ref(shared.isPlainObject(options.numberFormats)
  122. ? options.numberFormats
  123. : { [_locale.value]: {} });
  124. // warning suppress options
  125. // prettier-ignore
  126. let _missingWarn = __root
  127. ? __root.missingWarn
  128. : shared.isBoolean(options.missingWarn) || shared.isRegExp(options.missingWarn)
  129. ? options.missingWarn
  130. : true;
  131. // prettier-ignore
  132. let _fallbackWarn = __root
  133. ? __root.fallbackWarn
  134. : shared.isBoolean(options.fallbackWarn) || shared.isRegExp(options.fallbackWarn)
  135. ? options.fallbackWarn
  136. : true;
  137. // prettier-ignore
  138. let _fallbackRoot = __root
  139. ? __root.fallbackRoot
  140. : shared.isBoolean(options.fallbackRoot)
  141. ? options.fallbackRoot
  142. : true;
  143. // configure fall back to root
  144. let _fallbackFormat = !!options.fallbackFormat;
  145. // runtime missing
  146. let _missing = shared.isFunction(options.missing) ? options.missing : null;
  147. let _runtimeMissing = shared.isFunction(options.missing)
  148. ? defineCoreMissingHandler(options.missing)
  149. : null;
  150. // postTranslation handler
  151. let _postTranslation = shared.isFunction(options.postTranslation)
  152. ? options.postTranslation
  153. : null;
  154. let _warnHtmlMessage = shared.isBoolean(options.warnHtmlMessage)
  155. ? options.warnHtmlMessage
  156. : true;
  157. let _escapeParameter = !!options.escapeParameter;
  158. // custom linked modifiers
  159. // prettier-ignore
  160. const _modifiers = __root
  161. ? __root.modifiers
  162. : shared.isPlainObject(options.modifiers)
  163. ? options.modifiers
  164. : {};
  165. // pluralRules
  166. let _pluralRules = options.pluralRules || (__root && __root.pluralRules);
  167. // runtime context
  168. // eslint-disable-next-line prefer-const
  169. let _context;
  170. function getCoreContext() {
  171. return coreBase.createCoreContext({
  172. version: VERSION,
  173. locale: _locale.value,
  174. fallbackLocale: _fallbackLocale.value,
  175. messages: _messages.value,
  176. datetimeFormats: _datetimeFormats.value,
  177. numberFormats: _numberFormats.value,
  178. modifiers: _modifiers,
  179. pluralRules: _pluralRules,
  180. missing: _runtimeMissing === null ? undefined : _runtimeMissing,
  181. missingWarn: _missingWarn,
  182. fallbackWarn: _fallbackWarn,
  183. fallbackFormat: _fallbackFormat,
  184. unresolving: true,
  185. postTranslation: _postTranslation === null ? undefined : _postTranslation,
  186. warnHtmlMessage: _warnHtmlMessage,
  187. escapeParameter: _escapeParameter,
  188. __datetimeFormatters: shared.isPlainObject(_context)
  189. ? _context.__datetimeFormatters
  190. : undefined,
  191. __numberFormatters: shared.isPlainObject(_context)
  192. ? _context.__numberFormatters
  193. : undefined,
  194. __v_emitter: shared.isPlainObject(_context)
  195. ? _context.__v_emitter
  196. : undefined,
  197. __meta: { framework: 'vue' }
  198. });
  199. }
  200. _context = getCoreContext();
  201. coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  202. // track reactivity
  203. function trackReactivityValues() {
  204. return [
  205. _locale.value,
  206. _fallbackLocale.value,
  207. _messages.value,
  208. _datetimeFormats.value,
  209. _numberFormats.value
  210. ];
  211. }
  212. // locale
  213. const locale = vue.computed({
  214. get: () => _locale.value,
  215. set: val => {
  216. _locale.value = val;
  217. _context.locale = _locale.value;
  218. }
  219. });
  220. // fallbackLocale
  221. const fallbackLocale = vue.computed({
  222. get: () => _fallbackLocale.value,
  223. set: val => {
  224. _fallbackLocale.value = val;
  225. _context.fallbackLocale = _fallbackLocale.value;
  226. coreBase.updateFallbackLocale(_context, _locale.value, val);
  227. }
  228. });
  229. // messages
  230. const messages = vue.computed(() => _messages.value);
  231. // datetimeFormats
  232. const datetimeFormats = vue.computed(() => _datetimeFormats.value);
  233. // numberFormats
  234. const numberFormats = vue.computed(() => _numberFormats.value);
  235. // getPostTranslationHandler
  236. function getPostTranslationHandler() {
  237. return shared.isFunction(_postTranslation) ? _postTranslation : null;
  238. }
  239. // setPostTranslationHandler
  240. function setPostTranslationHandler(handler) {
  241. _postTranslation = handler;
  242. _context.postTranslation = handler;
  243. }
  244. // getMissingHandler
  245. function getMissingHandler() {
  246. return _missing;
  247. }
  248. // setMissingHandler
  249. function setMissingHandler(handler) {
  250. if (handler !== null) {
  251. _runtimeMissing = defineCoreMissingHandler(handler);
  252. }
  253. _missing = handler;
  254. _context.missing = _runtimeMissing;
  255. }
  256. function wrapWithDeps(fn, argumentParser, warnType, fallbackSuccess, fallbackFail, successCondition) {
  257. trackReactivityValues(); // track reactive dependency
  258. // NOTE: experimental !!
  259. let ret;
  260. {
  261. ret = fn(_context);
  262. }
  263. if (shared.isNumber(ret) && ret === coreBase.NOT_REOSLVED) {
  264. const [key, arg2] = argumentParser();
  265. return __root && _fallbackRoot
  266. ? fallbackSuccess(__root)
  267. : fallbackFail(key);
  268. }
  269. else if (successCondition(ret)) {
  270. return ret;
  271. }
  272. else {
  273. /* istanbul ignore next */
  274. throw createI18nError(14 /* UNEXPECTED_RETURN_TYPE */);
  275. }
  276. }
  277. // t
  278. function t(...args) {
  279. return wrapWithDeps(context => coreBase.translate(context, ...args), () => coreBase.parseTranslateArgs(...args), 'translate', root => root.t(...args), key => key, val => shared.isString(val));
  280. }
  281. // rt
  282. function rt(...args) {
  283. const [arg1, arg2, arg3] = args;
  284. if (arg3 && !shared.isObject(arg3)) {
  285. throw createI18nError(15 /* INVALID_ARGUMENT */);
  286. }
  287. return t(...[arg1, arg2, shared.assign({ resolvedMessage: true }, arg3 || {})]);
  288. }
  289. // d
  290. function d(...args) {
  291. return wrapWithDeps(context => coreBase.datetime(context, ...args), () => coreBase.parseDateTimeArgs(...args), 'datetime format', root => root.d(...args), () => coreBase.MISSING_RESOLVE_VALUE, val => shared.isString(val));
  292. }
  293. // n
  294. function n(...args) {
  295. return wrapWithDeps(context => coreBase.number(context, ...args), () => coreBase.parseNumberArgs(...args), 'number format', root => root.n(...args), () => coreBase.MISSING_RESOLVE_VALUE, val => shared.isString(val));
  296. }
  297. // for custom processor
  298. function normalize(values) {
  299. return values.map(val => shared.isString(val) ? vue.createVNode(vue.Text, null, val, 0) : val);
  300. }
  301. const interpolate = (val) => val;
  302. const processor = {
  303. normalize,
  304. interpolate,
  305. type: 'vnode'
  306. };
  307. // transrateVNode, using for `i18n-t` component
  308. function transrateVNode(...args) {
  309. return wrapWithDeps(context => {
  310. let ret;
  311. const _context = context;
  312. try {
  313. _context.processor = processor;
  314. ret = coreBase.translate(_context, ...args);
  315. }
  316. finally {
  317. _context.processor = null;
  318. }
  319. return ret;
  320. }, () => coreBase.parseTranslateArgs(...args), 'translate',
  321. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  322. root => root[TransrateVNodeSymbol](...args), key => [vue.createVNode(vue.Text, null, key, 0)], val => shared.isArray(val));
  323. }
  324. // numberParts, using for `i18n-n` component
  325. function numberParts(...args) {
  326. return wrapWithDeps(context => coreBase.number(context, ...args), () => coreBase.parseNumberArgs(...args), 'number format',
  327. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  328. root => root[NumberPartsSymbol](...args), () => [], val => shared.isString(val) || shared.isArray(val));
  329. }
  330. // datetimeParts, using for `i18n-d` component
  331. function datetimeParts(...args) {
  332. return wrapWithDeps(context => coreBase.datetime(context, ...args), () => coreBase.parseDateTimeArgs(...args), 'datetime format',
  333. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  334. root => root[DatetimePartsSymbol](...args), () => [], val => shared.isString(val) || shared.isArray(val));
  335. }
  336. function setPluralRules(rules) {
  337. _pluralRules = rules;
  338. _context.pluralRules = _pluralRules;
  339. }
  340. // te
  341. function te(key, locale) {
  342. const targetLocale = shared.isString(locale) ? locale : _locale.value;
  343. const message = getLocaleMessage(targetLocale);
  344. return coreBase.resolveValue(message, key) !== null;
  345. }
  346. function resolveMessages(key) {
  347. let messages = null;
  348. const locales = coreBase.getLocaleChain(_context, _fallbackLocale.value, _locale.value);
  349. for (let i = 0; i < locales.length; i++) {
  350. const targetLocaleMessages = _messages.value[locales[i]] || {};
  351. const messageValue = coreBase.resolveValue(targetLocaleMessages, key);
  352. if (messageValue != null) {
  353. messages = messageValue;
  354. break;
  355. }
  356. }
  357. return messages;
  358. }
  359. // tm
  360. function tm(key) {
  361. const messages = resolveMessages(key);
  362. // prettier-ignore
  363. return messages != null
  364. ? messages
  365. : __root
  366. ? __root.tm(key) || {}
  367. : {};
  368. }
  369. // getLocaleMessage
  370. function getLocaleMessage(locale) {
  371. return (_messages.value[locale] || {});
  372. }
  373. // setLocaleMessage
  374. function setLocaleMessage(locale, message) {
  375. _messages.value[locale] = message;
  376. _context.messages = _messages.value;
  377. }
  378. // mergeLocaleMessage
  379. function mergeLocaleMessage(locale, message) {
  380. _messages.value[locale] = _messages.value[locale] || {};
  381. deepCopy(message, _messages.value[locale]);
  382. _context.messages = _messages.value;
  383. }
  384. // getDateTimeFormat
  385. function getDateTimeFormat(locale) {
  386. return _datetimeFormats.value[locale] || {};
  387. }
  388. // setDateTimeFormat
  389. function setDateTimeFormat(locale, format) {
  390. _datetimeFormats.value[locale] = format;
  391. _context.datetimeFormats = _datetimeFormats.value;
  392. coreBase.clearDateTimeFormat(_context, locale, format);
  393. }
  394. // mergeDateTimeFormat
  395. function mergeDateTimeFormat(locale, format) {
  396. _datetimeFormats.value[locale] = shared.assign(_datetimeFormats.value[locale] || {}, format);
  397. _context.datetimeFormats = _datetimeFormats.value;
  398. coreBase.clearDateTimeFormat(_context, locale, format);
  399. }
  400. // getNumberFormat
  401. function getNumberFormat(locale) {
  402. return _numberFormats.value[locale] || {};
  403. }
  404. // setNumberFormat
  405. function setNumberFormat(locale, format) {
  406. _numberFormats.value[locale] = format;
  407. _context.numberFormats = _numberFormats.value;
  408. coreBase.clearNumberFormat(_context, locale, format);
  409. }
  410. // mergeNumberFormat
  411. function mergeNumberFormat(locale, format) {
  412. _numberFormats.value[locale] = shared.assign(_numberFormats.value[locale] || {}, format);
  413. _context.numberFormats = _numberFormats.value;
  414. coreBase.clearNumberFormat(_context, locale, format);
  415. }
  416. // for debug
  417. composerID++;
  418. // watch root locale & fallbackLocale
  419. if (__root) {
  420. vue.watch(__root.locale, (val) => {
  421. if (_inheritLocale) {
  422. _locale.value = val;
  423. _context.locale = val;
  424. coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  425. }
  426. });
  427. vue.watch(__root.fallbackLocale, (val) => {
  428. if (_inheritLocale) {
  429. _fallbackLocale.value = val;
  430. _context.fallbackLocale = val;
  431. coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  432. }
  433. });
  434. }
  435. // define composition API!
  436. const composer = {
  437. id: composerID,
  438. locale,
  439. fallbackLocale,
  440. get inheritLocale() {
  441. return _inheritLocale;
  442. },
  443. set inheritLocale(val) {
  444. _inheritLocale = val;
  445. if (val && __root) {
  446. _locale.value = __root.locale.value;
  447. _fallbackLocale.value = __root.fallbackLocale.value;
  448. coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  449. }
  450. },
  451. get availableLocales() {
  452. return Object.keys(_messages.value).sort();
  453. },
  454. messages,
  455. datetimeFormats,
  456. numberFormats,
  457. get modifiers() {
  458. return _modifiers;
  459. },
  460. get pluralRules() {
  461. return _pluralRules || {};
  462. },
  463. get isGlobal() {
  464. return _isGlobal;
  465. },
  466. get missingWarn() {
  467. return _missingWarn;
  468. },
  469. set missingWarn(val) {
  470. _missingWarn = val;
  471. _context.missingWarn = _missingWarn;
  472. },
  473. get fallbackWarn() {
  474. return _fallbackWarn;
  475. },
  476. set fallbackWarn(val) {
  477. _fallbackWarn = val;
  478. _context.fallbackWarn = _fallbackWarn;
  479. },
  480. get fallbackRoot() {
  481. return _fallbackRoot;
  482. },
  483. set fallbackRoot(val) {
  484. _fallbackRoot = val;
  485. },
  486. get fallbackFormat() {
  487. return _fallbackFormat;
  488. },
  489. set fallbackFormat(val) {
  490. _fallbackFormat = val;
  491. _context.fallbackFormat = _fallbackFormat;
  492. },
  493. get warnHtmlMessage() {
  494. return _warnHtmlMessage;
  495. },
  496. set warnHtmlMessage(val) {
  497. _warnHtmlMessage = val;
  498. _context.warnHtmlMessage = val;
  499. },
  500. get escapeParameter() {
  501. return _escapeParameter;
  502. },
  503. set escapeParameter(val) {
  504. _escapeParameter = val;
  505. _context.escapeParameter = val;
  506. },
  507. t,
  508. rt,
  509. d,
  510. n,
  511. te,
  512. tm,
  513. getLocaleMessage,
  514. setLocaleMessage,
  515. mergeLocaleMessage,
  516. getDateTimeFormat,
  517. setDateTimeFormat,
  518. mergeDateTimeFormat,
  519. getNumberFormat,
  520. setNumberFormat,
  521. mergeNumberFormat,
  522. getPostTranslationHandler,
  523. setPostTranslationHandler,
  524. getMissingHandler,
  525. setMissingHandler,
  526. [TransrateVNodeSymbol]: transrateVNode,
  527. [NumberPartsSymbol]: numberParts,
  528. [DatetimePartsSymbol]: datetimeParts,
  529. [SetPluralRulesSymbol]: setPluralRules,
  530. [InejctWithOption]: options.__injectWithOption // eslint-disable-line @typescript-eslint/no-explicit-any
  531. };
  532. return composer;
  533. }
  534. /**
  535. * Convert to I18n Composer Options from VueI18n Options
  536. *
  537. * @internal
  538. */
  539. function convertComposerOptions(options) {
  540. const locale = shared.isString(options.locale) ? options.locale : 'en-US';
  541. const fallbackLocale = shared.isString(options.fallbackLocale) ||
  542. shared.isArray(options.fallbackLocale) ||
  543. shared.isPlainObject(options.fallbackLocale) ||
  544. options.fallbackLocale === false
  545. ? options.fallbackLocale
  546. : locale;
  547. const missing = shared.isFunction(options.missing) ? options.missing : undefined;
  548. const missingWarn = shared.isBoolean(options.silentTranslationWarn) ||
  549. shared.isRegExp(options.silentTranslationWarn)
  550. ? !options.silentTranslationWarn
  551. : true;
  552. const fallbackWarn = shared.isBoolean(options.silentFallbackWarn) ||
  553. shared.isRegExp(options.silentFallbackWarn)
  554. ? !options.silentFallbackWarn
  555. : true;
  556. const fallbackRoot = shared.isBoolean(options.fallbackRoot)
  557. ? options.fallbackRoot
  558. : true;
  559. const fallbackFormat = !!options.formatFallbackMessages;
  560. const modifiers = shared.isPlainObject(options.modifiers) ? options.modifiers : {};
  561. const pluralizationRules = options.pluralizationRules;
  562. const postTranslation = shared.isFunction(options.postTranslation)
  563. ? options.postTranslation
  564. : undefined;
  565. const warnHtmlMessage = shared.isString(options.warnHtmlInMessage)
  566. ? options.warnHtmlInMessage !== 'off'
  567. : true;
  568. const escapeParameter = !!options.escapeParameterHtml;
  569. const inheritLocale = shared.isBoolean(options.sync) ? options.sync : true;
  570. let messages = options.messages;
  571. if (shared.isPlainObject(options.sharedMessages)) {
  572. const sharedMessages = options.sharedMessages;
  573. const locales = Object.keys(sharedMessages);
  574. messages = locales.reduce((messages, locale) => {
  575. const message = messages[locale] || (messages[locale] = {});
  576. shared.assign(message, sharedMessages[locale]);
  577. return messages;
  578. }, (messages || {}));
  579. }
  580. const { __i18n, __root, __injectWithOption } = options;
  581. const datetimeFormats = options.datetimeFormats;
  582. const numberFormats = options.numberFormats;
  583. const flatJson = options.flatJson;
  584. return {
  585. locale,
  586. fallbackLocale,
  587. messages,
  588. flatJson,
  589. datetimeFormats,
  590. numberFormats,
  591. missing,
  592. missingWarn,
  593. fallbackWarn,
  594. fallbackRoot,
  595. fallbackFormat,
  596. modifiers,
  597. pluralRules: pluralizationRules,
  598. postTranslation,
  599. warnHtmlMessage,
  600. escapeParameter,
  601. inheritLocale,
  602. __i18n,
  603. __root,
  604. __injectWithOption
  605. };
  606. }
  607. /**
  608. * create VueI18n interface factory
  609. *
  610. * @internal
  611. */
  612. function createVueI18n(options = {}) {
  613. const composer = createComposer(convertComposerOptions(options));
  614. // defines VueI18n
  615. const vueI18n = {
  616. // id
  617. id: composer.id,
  618. // locale
  619. get locale() {
  620. return composer.locale.value;
  621. },
  622. set locale(val) {
  623. composer.locale.value = val;
  624. },
  625. // fallbackLocale
  626. get fallbackLocale() {
  627. return composer.fallbackLocale.value;
  628. },
  629. set fallbackLocale(val) {
  630. composer.fallbackLocale.value = val;
  631. },
  632. // messages
  633. get messages() {
  634. return composer.messages.value;
  635. },
  636. // datetimeFormats
  637. get datetimeFormats() {
  638. return composer.datetimeFormats.value;
  639. },
  640. // numberFormats
  641. get numberFormats() {
  642. return composer.numberFormats.value;
  643. },
  644. // availableLocales
  645. get availableLocales() {
  646. return composer.availableLocales;
  647. },
  648. // formatter
  649. get formatter() {
  650. // dummy
  651. return {
  652. interpolate() {
  653. return [];
  654. }
  655. };
  656. },
  657. set formatter(val) {
  658. },
  659. // missing
  660. get missing() {
  661. return composer.getMissingHandler();
  662. },
  663. set missing(handler) {
  664. composer.setMissingHandler(handler);
  665. },
  666. // silentTranslationWarn
  667. get silentTranslationWarn() {
  668. return shared.isBoolean(composer.missingWarn)
  669. ? !composer.missingWarn
  670. : composer.missingWarn;
  671. },
  672. set silentTranslationWarn(val) {
  673. composer.missingWarn = shared.isBoolean(val) ? !val : val;
  674. },
  675. // silentFallbackWarn
  676. get silentFallbackWarn() {
  677. return shared.isBoolean(composer.fallbackWarn)
  678. ? !composer.fallbackWarn
  679. : composer.fallbackWarn;
  680. },
  681. set silentFallbackWarn(val) {
  682. composer.fallbackWarn = shared.isBoolean(val) ? !val : val;
  683. },
  684. // modifiers
  685. get modifiers() {
  686. return composer.modifiers;
  687. },
  688. // formatFallbackMessages
  689. get formatFallbackMessages() {
  690. return composer.fallbackFormat;
  691. },
  692. set formatFallbackMessages(val) {
  693. composer.fallbackFormat = val;
  694. },
  695. // postTranslation
  696. get postTranslation() {
  697. return composer.getPostTranslationHandler();
  698. },
  699. set postTranslation(handler) {
  700. composer.setPostTranslationHandler(handler);
  701. },
  702. // sync
  703. get sync() {
  704. return composer.inheritLocale;
  705. },
  706. set sync(val) {
  707. composer.inheritLocale = val;
  708. },
  709. // warnInHtmlMessage
  710. get warnHtmlInMessage() {
  711. return composer.warnHtmlMessage ? 'warn' : 'off';
  712. },
  713. set warnHtmlInMessage(val) {
  714. composer.warnHtmlMessage = val !== 'off';
  715. },
  716. // escapeParameterHtml
  717. get escapeParameterHtml() {
  718. return composer.escapeParameter;
  719. },
  720. set escapeParameterHtml(val) {
  721. composer.escapeParameter = val;
  722. },
  723. // preserveDirectiveContent
  724. get preserveDirectiveContent() {
  725. return true;
  726. },
  727. set preserveDirectiveContent(val) {
  728. },
  729. // pluralizationRules
  730. get pluralizationRules() {
  731. return composer.pluralRules || {};
  732. },
  733. // for internal
  734. __composer: composer,
  735. // t
  736. t(...args) {
  737. const [arg1, arg2, arg3] = args;
  738. const options = {};
  739. let list = null;
  740. let named = null;
  741. if (!shared.isString(arg1)) {
  742. throw createI18nError(15 /* INVALID_ARGUMENT */);
  743. }
  744. const key = arg1;
  745. if (shared.isString(arg2)) {
  746. options.locale = arg2;
  747. }
  748. else if (shared.isArray(arg2)) {
  749. list = arg2;
  750. }
  751. else if (shared.isPlainObject(arg2)) {
  752. named = arg2;
  753. }
  754. if (shared.isArray(arg3)) {
  755. list = arg3;
  756. }
  757. else if (shared.isPlainObject(arg3)) {
  758. named = arg3;
  759. }
  760. return composer.t(key, list || named || {}, options);
  761. },
  762. rt(...args) {
  763. return composer.rt(...args);
  764. },
  765. // tc
  766. tc(...args) {
  767. const [arg1, arg2, arg3] = args;
  768. const options = { plural: 1 };
  769. let list = null;
  770. let named = null;
  771. if (!shared.isString(arg1)) {
  772. throw createI18nError(15 /* INVALID_ARGUMENT */);
  773. }
  774. const key = arg1;
  775. if (shared.isString(arg2)) {
  776. options.locale = arg2;
  777. }
  778. else if (shared.isNumber(arg2)) {
  779. options.plural = arg2;
  780. }
  781. else if (shared.isArray(arg2)) {
  782. list = arg2;
  783. }
  784. else if (shared.isPlainObject(arg2)) {
  785. named = arg2;
  786. }
  787. if (shared.isString(arg3)) {
  788. options.locale = arg3;
  789. }
  790. else if (shared.isArray(arg3)) {
  791. list = arg3;
  792. }
  793. else if (shared.isPlainObject(arg3)) {
  794. named = arg3;
  795. }
  796. return composer.t(key, list || named || {}, options);
  797. },
  798. // te
  799. te(key, locale) {
  800. return composer.te(key, locale);
  801. },
  802. // tm
  803. tm(key) {
  804. return composer.tm(key);
  805. },
  806. // getLocaleMessage
  807. getLocaleMessage(locale) {
  808. return composer.getLocaleMessage(locale);
  809. },
  810. // setLocaleMessage
  811. setLocaleMessage(locale, message) {
  812. composer.setLocaleMessage(locale, message);
  813. },
  814. // mergeLocaleMessage
  815. mergeLocaleMessage(locale, message) {
  816. composer.mergeLocaleMessage(locale, message);
  817. },
  818. // d
  819. d(...args) {
  820. return composer.d(...args);
  821. },
  822. // getDateTimeFormat
  823. getDateTimeFormat(locale) {
  824. return composer.getDateTimeFormat(locale);
  825. },
  826. // setDateTimeFormat
  827. setDateTimeFormat(locale, format) {
  828. composer.setDateTimeFormat(locale, format);
  829. },
  830. // mergeDateTimeFormat
  831. mergeDateTimeFormat(locale, format) {
  832. composer.mergeDateTimeFormat(locale, format);
  833. },
  834. // n
  835. n(...args) {
  836. return composer.n(...args);
  837. },
  838. // getNumberFormat
  839. getNumberFormat(locale) {
  840. return composer.getNumberFormat(locale);
  841. },
  842. // setNumberFormat
  843. setNumberFormat(locale, format) {
  844. composer.setNumberFormat(locale, format);
  845. },
  846. // mergeNumberFormat
  847. mergeNumberFormat(locale, format) {
  848. composer.mergeNumberFormat(locale, format);
  849. },
  850. // getChoiceIndex
  851. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  852. getChoiceIndex(choice, choicesLength) {
  853. return -1;
  854. },
  855. // for internal
  856. __onComponentInstanceCreated(target) {
  857. const { componentInstanceCreatedListener } = options;
  858. if (componentInstanceCreatedListener) {
  859. componentInstanceCreatedListener(target, vueI18n);
  860. }
  861. }
  862. };
  863. return vueI18n;
  864. }
  865. const baseFormatProps = {
  866. tag: {
  867. type: [String, Object]
  868. },
  869. locale: {
  870. type: String
  871. },
  872. scope: {
  873. type: String,
  874. validator: (val) => val === 'parent' || val === 'global',
  875. default: 'parent'
  876. },
  877. i18n: {
  878. type: Object
  879. }
  880. };
  881. /**
  882. * Translation Component
  883. *
  884. * @remarks
  885. * See the following items for property about details
  886. *
  887. * @VueI18nSee [TranslationProps](component#translationprops)
  888. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  889. * @VueI18nSee [Component Interpolation](../guide/advanced/component)
  890. *
  891. * @example
  892. * ```html
  893. * <div id="app">
  894. * <!-- ... -->
  895. * <i18n path="term" tag="label" for="tos">
  896. * <a :href="url" target="_blank">{{ $t('tos') }}</a>
  897. * </i18n>
  898. * <!-- ... -->
  899. * </div>
  900. * ```
  901. * ```js
  902. * import { createApp } from 'vue'
  903. * import { createI18n } from 'vue-i18n'
  904. *
  905. * const messages = {
  906. * en: {
  907. * tos: 'Term of Service',
  908. * term: 'I accept xxx {0}.'
  909. * },
  910. * ja: {
  911. * tos: '利用規約',
  912. * term: '私は xxx の{0}に同意します。'
  913. * }
  914. * }
  915. *
  916. * const i18n = createI18n({
  917. * locale: 'en',
  918. * messages
  919. * })
  920. *
  921. * const app = createApp({
  922. * data: {
  923. * url: '/term'
  924. * }
  925. * }).use(i18n).mount('#app')
  926. * ```
  927. *
  928. * @VueI18nComponent
  929. */
  930. const Translation = {
  931. /* eslint-disable */
  932. name: 'i18n-t',
  933. props: shared.assign({
  934. keypath: {
  935. type: String,
  936. required: true
  937. },
  938. plural: {
  939. type: [Number, String],
  940. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  941. validator: (val) => shared.isNumber(val) || !isNaN(val)
  942. }
  943. }, baseFormatProps),
  944. /* eslint-enable */
  945. setup(props, context) {
  946. const { slots, attrs } = context;
  947. const i18n = props.i18n ||
  948. useI18n({
  949. useScope: props.scope,
  950. __useComponent: true
  951. });
  952. const keys = Object.keys(slots).filter(key => key !== '_');
  953. return () => {
  954. const options = {};
  955. if (props.locale) {
  956. options.locale = props.locale;
  957. }
  958. if (props.plural !== undefined) {
  959. options.plural = shared.isString(props.plural) ? +props.plural : props.plural;
  960. }
  961. const arg = getInterpolateArg(context, keys);
  962. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  963. const children = i18n[TransrateVNodeSymbol](props.keypath, arg, options);
  964. const assignedAttrs = shared.assign({}, attrs);
  965. // prettier-ignore
  966. return shared.isString(props.tag)
  967. ? vue.h(props.tag, assignedAttrs, children)
  968. : shared.isObject(props.tag)
  969. ? vue.h(props.tag, assignedAttrs, children)
  970. : vue.h(vue.Fragment, assignedAttrs, children);
  971. };
  972. }
  973. };
  974. function getInterpolateArg({ slots }, keys) {
  975. if (keys.length === 1 && keys[0] === 'default') {
  976. // default slot only
  977. return slots.default ? slots.default() : [];
  978. }
  979. else {
  980. // named slots
  981. return keys.reduce((arg, key) => {
  982. const slot = slots[key];
  983. if (slot) {
  984. arg[key] = slot();
  985. }
  986. return arg;
  987. }, {});
  988. }
  989. }
  990. function renderFormatter(props, context, slotKeys, partFormatter) {
  991. const { slots, attrs } = context;
  992. return () => {
  993. const options = { part: true };
  994. let overrides = {};
  995. if (props.locale) {
  996. options.locale = props.locale;
  997. }
  998. if (shared.isString(props.format)) {
  999. options.key = props.format;
  1000. }
  1001. else if (shared.isObject(props.format)) {
  1002. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1003. if (shared.isString(props.format.key)) {
  1004. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1005. options.key = props.format.key;
  1006. }
  1007. // Filter out number format options only
  1008. overrides = Object.keys(props.format).reduce((options, prop) => {
  1009. return slotKeys.includes(prop)
  1010. ? shared.assign({}, options, { [prop]: props.format[prop] }) // eslint-disable-line @typescript-eslint/no-explicit-any
  1011. : options;
  1012. }, {});
  1013. }
  1014. const parts = partFormatter(...[props.value, options, overrides]);
  1015. let children = [options.key];
  1016. if (shared.isArray(parts)) {
  1017. children = parts.map((part, index) => {
  1018. const slot = slots[part.type];
  1019. return slot
  1020. ? slot({ [part.type]: part.value, index, parts })
  1021. : [part.value];
  1022. });
  1023. }
  1024. else if (shared.isString(parts)) {
  1025. children = [parts];
  1026. }
  1027. const assignedAttrs = shared.assign({}, attrs);
  1028. // prettier-ignore
  1029. return shared.isString(props.tag)
  1030. ? vue.h(props.tag, assignedAttrs, children)
  1031. : shared.isObject(props.tag)
  1032. ? vue.h(props.tag, assignedAttrs, children)
  1033. : vue.h(vue.Fragment, assignedAttrs, children);
  1034. };
  1035. }
  1036. const NUMBER_FORMAT_KEYS = [
  1037. 'localeMatcher',
  1038. 'style',
  1039. 'unit',
  1040. 'unitDisplay',
  1041. 'currency',
  1042. 'currencyDisplay',
  1043. 'useGrouping',
  1044. 'numberingSystem',
  1045. 'minimumIntegerDigits',
  1046. 'minimumFractionDigits',
  1047. 'maximumFractionDigits',
  1048. 'minimumSignificantDigits',
  1049. 'maximumSignificantDigits',
  1050. 'notation',
  1051. 'formatMatcher'
  1052. ];
  1053. /**
  1054. * Number Format Component
  1055. *
  1056. * @remarks
  1057. * See the following items for property about details
  1058. *
  1059. * @VueI18nSee [FormattableProps](component#formattableprops)
  1060. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  1061. * @VueI18nSee [Custom Formatting](../guide/essentials/number#custom-formatting)
  1062. *
  1063. * @VueI18nDanger
  1064. * Not supported IE, due to no support `Intl.NumberFormat#formatToParts` in [IE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/formatToParts)
  1065. *
  1066. * If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-numberformat)
  1067. *
  1068. * @VueI18nComponent
  1069. */
  1070. const NumberFormat = {
  1071. /* eslint-disable */
  1072. name: 'i18n-n',
  1073. props: shared.assign({
  1074. value: {
  1075. type: Number,
  1076. required: true
  1077. },
  1078. format: {
  1079. type: [String, Object]
  1080. }
  1081. }, baseFormatProps),
  1082. /* eslint-enable */
  1083. setup(props, context) {
  1084. const i18n = props.i18n ||
  1085. useI18n({ useScope: 'parent', __useComponent: true });
  1086. return renderFormatter(props, context, NUMBER_FORMAT_KEYS, (...args) =>
  1087. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1088. i18n[NumberPartsSymbol](...args));
  1089. }
  1090. };
  1091. const DATETIME_FORMAT_KEYS = [
  1092. 'dateStyle',
  1093. 'timeStyle',
  1094. 'fractionalSecondDigits',
  1095. 'calendar',
  1096. 'dayPeriod',
  1097. 'numberingSystem',
  1098. 'localeMatcher',
  1099. 'timeZone',
  1100. 'hour12',
  1101. 'hourCycle',
  1102. 'formatMatcher',
  1103. 'weekday',
  1104. 'era',
  1105. 'year',
  1106. 'month',
  1107. 'day',
  1108. 'hour',
  1109. 'minute',
  1110. 'second',
  1111. 'timeZoneName'
  1112. ];
  1113. /**
  1114. * Datetime Format Component
  1115. *
  1116. * @remarks
  1117. * See the following items for property about details
  1118. *
  1119. * @VueI18nSee [FormattableProps](component#formattableprops)
  1120. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  1121. * @VueI18nSee [Custom Formatting](../guide/essentials/datetime#custom-formatting)
  1122. *
  1123. * @VueI18nDanger
  1124. * Not supported IE, due to no support `Intl.DateTimeFormat#formatToParts` in [IE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts)
  1125. *
  1126. * If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-datetimeformat)
  1127. *
  1128. * @VueI18nComponent
  1129. */
  1130. const DatetimeFormat = {
  1131. /* eslint-disable */
  1132. name: 'i18n-d',
  1133. props: shared.assign({
  1134. value: {
  1135. type: [Number, Date],
  1136. required: true
  1137. },
  1138. format: {
  1139. type: [String, Object]
  1140. }
  1141. }, baseFormatProps),
  1142. /* eslint-enable */
  1143. setup(props, context) {
  1144. const i18n = props.i18n ||
  1145. useI18n({ useScope: 'parent', __useComponent: true });
  1146. return renderFormatter(props, context, DATETIME_FORMAT_KEYS, (...args) =>
  1147. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1148. i18n[DatetimePartsSymbol](...args));
  1149. }
  1150. };
  1151. function getComposer$1(i18n, instance) {
  1152. const i18nInternal = i18n;
  1153. if (i18n.mode === 'composition') {
  1154. return (i18nInternal.__getInstance(instance) || i18n.global);
  1155. }
  1156. else {
  1157. const vueI18n = i18nInternal.__getInstance(instance);
  1158. return vueI18n != null
  1159. ? vueI18n.__composer
  1160. : i18n.global.__composer;
  1161. }
  1162. }
  1163. function vTDirective(i18n) {
  1164. const bind = (el, { instance, value, modifiers }) => {
  1165. /* istanbul ignore if */
  1166. if (!instance || !instance.$) {
  1167. throw createI18nError(22 /* UNEXPECTED_ERROR */);
  1168. }
  1169. const composer = getComposer$1(i18n, instance.$);
  1170. const parsedValue = parseValue(value);
  1171. el.textContent = composer.t(...makeParams(parsedValue));
  1172. };
  1173. return {
  1174. beforeMount: bind,
  1175. beforeUpdate: bind
  1176. };
  1177. }
  1178. function parseValue(value) {
  1179. if (shared.isString(value)) {
  1180. return { path: value };
  1181. }
  1182. else if (shared.isPlainObject(value)) {
  1183. if (!('path' in value)) {
  1184. throw createI18nError(19 /* REQUIRED_VALUE */, 'path');
  1185. }
  1186. return value;
  1187. }
  1188. else {
  1189. throw createI18nError(20 /* INVALID_VALUE */);
  1190. }
  1191. }
  1192. function makeParams(value) {
  1193. const { path, locale, args, choice, plural } = value;
  1194. const options = {};
  1195. const named = args || {};
  1196. if (shared.isString(locale)) {
  1197. options.locale = locale;
  1198. }
  1199. if (shared.isNumber(choice)) {
  1200. options.plural = choice;
  1201. }
  1202. if (shared.isNumber(plural)) {
  1203. options.plural = plural;
  1204. }
  1205. return [path, named, options];
  1206. }
  1207. function apply(app, i18n, ...options) {
  1208. const pluginOptions = shared.isPlainObject(options[0])
  1209. ? options[0]
  1210. : {};
  1211. const useI18nComponentName = !!pluginOptions.useI18nComponentName;
  1212. const globalInstall = shared.isBoolean(pluginOptions.globalInstall)
  1213. ? pluginOptions.globalInstall
  1214. : true;
  1215. if (globalInstall) {
  1216. // install components
  1217. app.component(!useI18nComponentName ? Translation.name : 'i18n', Translation);
  1218. app.component(NumberFormat.name, NumberFormat);
  1219. app.component(DatetimeFormat.name, DatetimeFormat);
  1220. }
  1221. // install directive
  1222. app.directive('t', vTDirective(i18n));
  1223. }
  1224. // supports compatibility for legacy vue-i18n APIs
  1225. function defineMixin(vuei18n, composer, i18n) {
  1226. return {
  1227. beforeCreate() {
  1228. const instance = vue.getCurrentInstance();
  1229. /* istanbul ignore if */
  1230. if (!instance) {
  1231. throw createI18nError(22 /* UNEXPECTED_ERROR */);
  1232. }
  1233. const options = this.$options;
  1234. if (options.i18n) {
  1235. const optionsI18n = options.i18n;
  1236. if (options.__i18n) {
  1237. optionsI18n.__i18n = options.__i18n;
  1238. }
  1239. optionsI18n.__root = composer;
  1240. if (this === this.$root) {
  1241. this.$i18n = mergeToRoot(vuei18n, optionsI18n);
  1242. }
  1243. else {
  1244. optionsI18n.__injectWithOption = true;
  1245. this.$i18n = createVueI18n(optionsI18n);
  1246. }
  1247. }
  1248. else if (options.__i18n) {
  1249. if (this === this.$root) {
  1250. this.$i18n = mergeToRoot(vuei18n, options);
  1251. }
  1252. else {
  1253. this.$i18n = createVueI18n({
  1254. __i18n: options.__i18n,
  1255. __injectWithOption: true,
  1256. __root: composer
  1257. });
  1258. }
  1259. }
  1260. else {
  1261. // set global
  1262. this.$i18n = vuei18n;
  1263. }
  1264. vuei18n.__onComponentInstanceCreated(this.$i18n);
  1265. i18n.__setInstance(instance, this.$i18n);
  1266. // defines vue-i18n legacy APIs
  1267. this.$t = (...args) => this.$i18n.t(...args);
  1268. this.$rt = (...args) => this.$i18n.rt(...args);
  1269. this.$tc = (...args) => this.$i18n.tc(...args);
  1270. this.$te = (key, locale) => this.$i18n.te(key, locale);
  1271. this.$d = (...args) => this.$i18n.d(...args);
  1272. this.$n = (...args) => this.$i18n.n(...args);
  1273. this.$tm = (key) => this.$i18n.tm(key);
  1274. },
  1275. mounted() {
  1276. },
  1277. beforeUnmount() {
  1278. const instance = vue.getCurrentInstance();
  1279. /* istanbul ignore if */
  1280. if (!instance) {
  1281. throw createI18nError(22 /* UNEXPECTED_ERROR */);
  1282. }
  1283. delete this.$t;
  1284. delete this.$rt;
  1285. delete this.$tc;
  1286. delete this.$te;
  1287. delete this.$d;
  1288. delete this.$n;
  1289. delete this.$tm;
  1290. i18n.__deleteInstance(instance);
  1291. delete this.$i18n;
  1292. }
  1293. };
  1294. }
  1295. function mergeToRoot(root, options) {
  1296. root.locale = options.locale || root.locale;
  1297. root.fallbackLocale = options.fallbackLocale || root.fallbackLocale;
  1298. root.missing = options.missing || root.missing;
  1299. root.silentTranslationWarn =
  1300. options.silentTranslationWarn || root.silentFallbackWarn;
  1301. root.silentFallbackWarn =
  1302. options.silentFallbackWarn || root.silentFallbackWarn;
  1303. root.formatFallbackMessages =
  1304. options.formatFallbackMessages || root.formatFallbackMessages;
  1305. root.postTranslation = options.postTranslation || root.postTranslation;
  1306. root.warnHtmlInMessage = options.warnHtmlInMessage || root.warnHtmlInMessage;
  1307. root.escapeParameterHtml =
  1308. options.escapeParameterHtml || root.escapeParameterHtml;
  1309. root.sync = options.sync || root.sync;
  1310. root.__composer[SetPluralRulesSymbol](options.pluralizationRules || root.pluralizationRules);
  1311. const messages = getLocaleMessages(root.locale, {
  1312. messages: options.messages,
  1313. __i18n: options.__i18n
  1314. });
  1315. Object.keys(messages).forEach(locale => root.mergeLocaleMessage(locale, messages[locale]));
  1316. if (options.datetimeFormats) {
  1317. Object.keys(options.datetimeFormats).forEach(locale => root.mergeDateTimeFormat(locale, options.datetimeFormats[locale]));
  1318. }
  1319. if (options.numberFormats) {
  1320. Object.keys(options.numberFormats).forEach(locale => root.mergeNumberFormat(locale, options.numberFormats[locale]));
  1321. }
  1322. return root;
  1323. }
  1324. /**
  1325. * Vue I18n factory
  1326. *
  1327. * @param options - An options, see the {@link I18nOptions}
  1328. *
  1329. * @returns {@link I18n} instance
  1330. *
  1331. * @remarks
  1332. * If you use Legacy API mode, you need toto specify {@link VueI18nOptions} and `legacy: true` option.
  1333. *
  1334. * If you use composition API mode, you need to specify {@link ComposerOptions}.
  1335. *
  1336. * @VueI18nSee [Getting Started](../guide/)
  1337. * @VueI18nSee [Composition API](../guide/advanced/composition)
  1338. *
  1339. * @example
  1340. * case: for Legacy API
  1341. * ```js
  1342. * import { createApp } from 'vue'
  1343. * import { createI18n } from 'vue-i18n'
  1344. *
  1345. * // call with I18n option
  1346. * const i18n = createI18n({
  1347. * locale: 'ja',
  1348. * messages: {
  1349. * en: { ... },
  1350. * ja: { ... }
  1351. * }
  1352. * })
  1353. *
  1354. * const App = {
  1355. * // ...
  1356. * }
  1357. *
  1358. * const app = createApp(App)
  1359. *
  1360. * // install!
  1361. * app.use(i18n)
  1362. * app.mount('#app')
  1363. * ```
  1364. *
  1365. * @example
  1366. * case: for composition API
  1367. * ```js
  1368. * import { createApp } from 'vue'
  1369. * import { createI18n, useI18n } from 'vue-i18n'
  1370. *
  1371. * // call with I18n option
  1372. * const i18n = createI18n({
  1373. * legacy: false, // you must specify 'legacy: false' option
  1374. * locale: 'ja',
  1375. * messages: {
  1376. * en: { ... },
  1377. * ja: { ... }
  1378. * }
  1379. * })
  1380. *
  1381. * const App = {
  1382. * setup() {
  1383. * // ...
  1384. * const { t } = useI18n({ ... })
  1385. * return { ... , t }
  1386. * }
  1387. * }
  1388. *
  1389. * const app = createApp(App)
  1390. *
  1391. * // install!
  1392. * app.use(i18n)
  1393. * app.mount('#app')
  1394. * ```
  1395. *
  1396. * @VueI18nGeneral
  1397. */
  1398. function createI18n(options = {}) {
  1399. // prettier-ignore
  1400. const __legacyMode = shared.isBoolean(options.legacy)
  1401. ? options.legacy
  1402. : true;
  1403. const __globalInjection = !!options.globalInjection;
  1404. const __instances = new Map();
  1405. // prettier-ignore
  1406. const __global = __legacyMode
  1407. ? createVueI18n(options)
  1408. : createComposer(options);
  1409. const symbol = shared.makeSymbol('');
  1410. const i18n = {
  1411. // mode
  1412. get mode() {
  1413. // prettier-ignore
  1414. return __legacyMode
  1415. ? 'legacy'
  1416. : 'composition'
  1417. ;
  1418. },
  1419. // install plugin
  1420. async install(app, ...options) {
  1421. // setup global provider
  1422. app.__VUE_I18N_SYMBOL__ = symbol;
  1423. app.provide(app.__VUE_I18N_SYMBOL__, i18n);
  1424. // global method and properties injection for Composition API
  1425. if (!__legacyMode && __globalInjection) {
  1426. injectGlobalFields(app, i18n.global);
  1427. }
  1428. // install built-in components and directive
  1429. {
  1430. apply(app, i18n, ...options);
  1431. }
  1432. // setup mixin for Legacy API
  1433. if (__legacyMode) {
  1434. app.mixin(defineMixin(__global, __global.__composer, i18n));
  1435. }
  1436. },
  1437. // global accessor
  1438. get global() {
  1439. return __global;
  1440. },
  1441. // @internal
  1442. __instances,
  1443. // @internal
  1444. __getInstance(component) {
  1445. return __instances.get(component) || null;
  1446. },
  1447. // @internal
  1448. __setInstance(component, instance) {
  1449. __instances.set(component, instance);
  1450. },
  1451. // @internal
  1452. __deleteInstance(component) {
  1453. __instances.delete(component);
  1454. }
  1455. };
  1456. return i18n;
  1457. }
  1458. /**
  1459. * Use Composition API for Vue I18n
  1460. *
  1461. * @param options - An options, see {@link UseI18nOptions}
  1462. *
  1463. * @returns {@link Composer} instance
  1464. *
  1465. * @remarks
  1466. * This function is mainly used by `setup`.
  1467. *
  1468. * If options are specified, Composer instance is created for each component and you can be localized on the component.
  1469. *
  1470. * If options are not specified, you can be localized using the global Composer.
  1471. *
  1472. * @example
  1473. * case: Component resource base localization
  1474. * ```html
  1475. * <template>
  1476. * <form>
  1477. * <label>{{ t('language') }}</label>
  1478. * <select v-model="locale">
  1479. * <option value="en">en</option>
  1480. * <option value="ja">ja</option>
  1481. * </select>
  1482. * </form>
  1483. * <p>message: {{ t('hello') }}</p>
  1484. * </template>
  1485. *
  1486. * <script>
  1487. * import { useI18n } from 'vue-i18n'
  1488. *
  1489. * export default {
  1490. * setup() {
  1491. * const { t, locale } = useI18n({
  1492. * locale: 'ja',
  1493. * messages: {
  1494. * en: { ... },
  1495. * ja: { ... }
  1496. * }
  1497. * })
  1498. * // Something to do ...
  1499. *
  1500. * return { ..., t, locale }
  1501. * }
  1502. * }
  1503. * </script>
  1504. * ```
  1505. *
  1506. * @VueI18nComposition
  1507. */
  1508. function useI18n(options = {}) {
  1509. const instance = vue.getCurrentInstance();
  1510. if (instance == null) {
  1511. throw createI18nError(16 /* MUST_BE_CALL_SETUP_TOP */);
  1512. }
  1513. if (!instance.appContext.app.__VUE_I18N_SYMBOL__) {
  1514. throw createI18nError(17 /* NOT_INSLALLED */);
  1515. }
  1516. const i18n = vue.inject(instance.appContext.app.__VUE_I18N_SYMBOL__);
  1517. /* istanbul ignore if */
  1518. if (!i18n) {
  1519. throw createI18nError(22 /* UNEXPECTED_ERROR */);
  1520. }
  1521. // prettier-ignore
  1522. const global = i18n.mode === 'composition'
  1523. ? i18n.global
  1524. : i18n.global.__composer;
  1525. // prettier-ignore
  1526. const scope = shared.isEmptyObject(options)
  1527. ? ('__i18n' in instance.type)
  1528. ? 'local'
  1529. : 'global'
  1530. : !options.useScope
  1531. ? 'local'
  1532. : options.useScope;
  1533. if (scope === 'global') {
  1534. let messages = shared.isObject(options.messages) ? options.messages : {};
  1535. if ('__i18nGlobal' in instance.type) {
  1536. messages = getLocaleMessages(global.locale.value, {
  1537. messages,
  1538. __i18n: instance.type.__i18nGlobal
  1539. });
  1540. }
  1541. // merge locale messages
  1542. const locales = Object.keys(messages);
  1543. if (locales.length) {
  1544. locales.forEach(locale => {
  1545. global.mergeLocaleMessage(locale, messages[locale]);
  1546. });
  1547. }
  1548. // merge datetime formats
  1549. if (shared.isObject(options.datetimeFormats)) {
  1550. const locales = Object.keys(options.datetimeFormats);
  1551. if (locales.length) {
  1552. locales.forEach(locale => {
  1553. global.mergeDateTimeFormat(locale, options.datetimeFormats[locale]);
  1554. });
  1555. }
  1556. }
  1557. // merge number formats
  1558. if (shared.isObject(options.numberFormats)) {
  1559. const locales = Object.keys(options.numberFormats);
  1560. if (locales.length) {
  1561. locales.forEach(locale => {
  1562. global.mergeNumberFormat(locale, options.numberFormats[locale]);
  1563. });
  1564. }
  1565. }
  1566. return global;
  1567. }
  1568. if (scope === 'parent') {
  1569. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1570. let composer = getComposer(i18n, instance, options.__useComponent);
  1571. if (composer == null) {
  1572. composer = global;
  1573. }
  1574. return composer;
  1575. }
  1576. // scope 'local' case
  1577. if (i18n.mode === 'legacy') {
  1578. throw createI18nError(18 /* NOT_AVAILABLE_IN_LEGACY_MODE */);
  1579. }
  1580. const i18nInternal = i18n;
  1581. let composer = i18nInternal.__getInstance(instance);
  1582. if (composer == null) {
  1583. const type = instance.type;
  1584. const composerOptions = shared.assign({}, options);
  1585. if (type.__i18n) {
  1586. composerOptions.__i18n = type.__i18n;
  1587. }
  1588. if (global) {
  1589. composerOptions.__root = global;
  1590. }
  1591. composer = createComposer(composerOptions);
  1592. setupLifeCycle(i18nInternal, instance);
  1593. i18nInternal.__setInstance(instance, composer);
  1594. }
  1595. return composer;
  1596. }
  1597. function getComposer(i18n, target, useComponent = false) {
  1598. let composer = null;
  1599. const root = target.root;
  1600. let current = target.parent;
  1601. while (current != null) {
  1602. const i18nInternal = i18n;
  1603. if (i18n.mode === 'composition') {
  1604. composer = i18nInternal.__getInstance(current);
  1605. }
  1606. else {
  1607. const vueI18n = i18nInternal.__getInstance(current);
  1608. if (vueI18n != null) {
  1609. composer = vueI18n
  1610. .__composer;
  1611. }
  1612. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1613. if (useComponent && composer && !composer[InejctWithOption]) {
  1614. composer = null;
  1615. }
  1616. }
  1617. if (composer != null) {
  1618. break;
  1619. }
  1620. if (root === current) {
  1621. break;
  1622. }
  1623. current = current.parent;
  1624. }
  1625. return composer;
  1626. }
  1627. function setupLifeCycle(i18n, target, composer) {
  1628. vue.onMounted(() => {
  1629. }, target);
  1630. vue.onUnmounted(() => {
  1631. i18n.__deleteInstance(target);
  1632. }, target);
  1633. }
  1634. const globalExportProps = [
  1635. 'locale',
  1636. 'fallbackLocale',
  1637. 'availableLocales'
  1638. ];
  1639. const globalExportMethods = ['t', 'rt', 'd', 'n', 'tm'];
  1640. function injectGlobalFields(app, composer) {
  1641. const i18n = Object.create(null);
  1642. globalExportProps.forEach(prop => {
  1643. const desc = Object.getOwnPropertyDescriptor(composer, prop);
  1644. if (!desc) {
  1645. throw createI18nError(22 /* UNEXPECTED_ERROR */);
  1646. }
  1647. const wrap = vue.isRef(desc.value) // check computed props
  1648. ? {
  1649. get() {
  1650. return desc.value.value;
  1651. },
  1652. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1653. set(val) {
  1654. desc.value.value = val;
  1655. }
  1656. }
  1657. : {
  1658. get() {
  1659. return desc.get && desc.get();
  1660. }
  1661. };
  1662. Object.defineProperty(i18n, prop, wrap);
  1663. });
  1664. app.config.globalProperties.$i18n = i18n;
  1665. globalExportMethods.forEach(method => {
  1666. const desc = Object.getOwnPropertyDescriptor(composer, method);
  1667. if (!desc || !desc.value) {
  1668. throw createI18nError(22 /* UNEXPECTED_ERROR */);
  1669. }
  1670. Object.defineProperty(app.config.globalProperties, `$${method}`, desc);
  1671. });
  1672. }
  1673. // register message compiler at vue-i18n
  1674. coreBase.registerMessageCompiler(coreBase.compileToFunction);
  1675. exports.DatetimeFormat = DatetimeFormat;
  1676. exports.NumberFormat = NumberFormat;
  1677. exports.Translation = Translation;
  1678. exports.VERSION = VERSION;
  1679. exports.createI18n = createI18n;
  1680. exports.useI18n = useI18n;
  1681. exports.vTDirective = vTDirective;