vue-i18n.prod.cjs 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320
  1. /*!
  2. * vue-i18n v9.5.0
  3. * (c) 2023 kazuya kawaguchi
  4. * Released under the MIT License.
  5. */
  6. 'use strict';
  7. var coreBase = require('@intlify/core-base');
  8. var vue = require('vue');
  9. var shared = require('@intlify/shared');
  10. /**
  11. * Vue I18n Version
  12. *
  13. * @remarks
  14. * Semver format. Same format as the package.json `version` field.
  15. *
  16. * @VueI18nGeneral
  17. */
  18. const VERSION = '9.5.0';
  19. const code = coreBase.CoreErrorCodes.__EXTEND_POINT__;
  20. const inc = shared.incrementer(code);
  21. const I18nErrorCodes = {
  22. // composer module errors
  23. UNEXPECTED_RETURN_TYPE: code,
  24. // legacy module errors
  25. INVALID_ARGUMENT: inc(),
  26. // i18n module errors
  27. MUST_BE_CALL_SETUP_TOP: inc(),
  28. NOT_INSTALLED: inc(),
  29. NOT_AVAILABLE_IN_LEGACY_MODE: inc(),
  30. // directive module errors
  31. REQUIRED_VALUE: inc(),
  32. INVALID_VALUE: inc(),
  33. // vue-devtools errors
  34. CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN: inc(),
  35. NOT_INSTALLED_WITH_PROVIDE: inc(),
  36. // unexpected error
  37. UNEXPECTED_ERROR: inc(),
  38. // not compatible legacy vue-i18n constructor
  39. NOT_COMPATIBLE_LEGACY_VUE_I18N: inc(),
  40. // bridge support vue 2.x only
  41. BRIDGE_SUPPORT_VUE_2_ONLY: inc(),
  42. // need to define `i18n` option in `allowComposition: true` and `useScope: 'local' at `useI18n``
  43. MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION: inc(),
  44. // Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly
  45. NOT_AVAILABLE_COMPOSITION_IN_LEGACY: inc(),
  46. // for enhancement
  47. __EXTEND_POINT__: inc() // 37
  48. };
  49. function createI18nError(code, ...args) {
  50. return coreBase.createCompileError(code, null, undefined);
  51. }
  52. const TranslateVNodeSymbol =
  53. /* #__PURE__*/ shared.makeSymbol('__translateVNode');
  54. const DatetimePartsSymbol = /* #__PURE__*/ shared.makeSymbol('__datetimeParts');
  55. const NumberPartsSymbol = /* #__PURE__*/ shared.makeSymbol('__numberParts');
  56. const SetPluralRulesSymbol = shared.makeSymbol('__setPluralRules');
  57. shared.makeSymbol('__intlifyMeta');
  58. const InejctWithOptionSymbol =
  59. /* #__PURE__*/ shared.makeSymbol('__injectWithOption');
  60. const DisposeSymbol = /* #__PURE__*/ shared.makeSymbol('__dispose');
  61. const __VUE_I18N_BRIDGE__ = '__VUE_I18N_BRIDGE__';
  62. /* eslint-disable @typescript-eslint/no-explicit-any */
  63. /**
  64. * Transform flat json in obj to normal json in obj
  65. */
  66. function handleFlatJson(obj) {
  67. // check obj
  68. if (!shared.isObject(obj)) {
  69. return obj;
  70. }
  71. for (const key in obj) {
  72. // check key
  73. if (!shared.hasOwn(obj, key)) {
  74. continue;
  75. }
  76. // handle for normal json
  77. if (!key.includes('.')) {
  78. // recursive process value if value is also a object
  79. if (shared.isObject(obj[key])) {
  80. handleFlatJson(obj[key]);
  81. }
  82. }
  83. // handle for flat json, transform to normal json
  84. else {
  85. // go to the last object
  86. const subKeys = key.split('.');
  87. const lastIndex = subKeys.length - 1;
  88. let currentObj = obj;
  89. let hasStringValue = false;
  90. for (let i = 0; i < lastIndex; i++) {
  91. if (!(subKeys[i] in currentObj)) {
  92. currentObj[subKeys[i]] = {};
  93. }
  94. if (!shared.isObject(currentObj[subKeys[i]])) {
  95. hasStringValue = true;
  96. break;
  97. }
  98. currentObj = currentObj[subKeys[i]];
  99. }
  100. // update last object value, delete old property
  101. if (!hasStringValue) {
  102. currentObj[subKeys[lastIndex]] = obj[key];
  103. delete obj[key];
  104. }
  105. // recursive process value if value is also a object
  106. if (shared.isObject(currentObj[subKeys[lastIndex]])) {
  107. handleFlatJson(currentObj[subKeys[lastIndex]]);
  108. }
  109. }
  110. }
  111. return obj;
  112. }
  113. function getLocaleMessages(locale, options) {
  114. const { messages, __i18n, messageResolver, flatJson } = options;
  115. // prettier-ignore
  116. const ret = (shared.isPlainObject(messages)
  117. ? messages
  118. : shared.isArray(__i18n)
  119. ? {}
  120. : { [locale]: {} });
  121. // merge locale messages of i18n custom block
  122. if (shared.isArray(__i18n)) {
  123. __i18n.forEach(custom => {
  124. if ('locale' in custom && 'resource' in custom) {
  125. const { locale, resource } = custom;
  126. if (locale) {
  127. ret[locale] = ret[locale] || {};
  128. deepCopy(resource, ret[locale]);
  129. }
  130. else {
  131. deepCopy(resource, ret);
  132. }
  133. }
  134. else {
  135. shared.isString(custom) && deepCopy(JSON.parse(custom), ret);
  136. }
  137. });
  138. }
  139. // handle messages for flat json
  140. if (messageResolver == null && flatJson) {
  141. for (const key in ret) {
  142. if (shared.hasOwn(ret, key)) {
  143. handleFlatJson(ret[key]);
  144. }
  145. }
  146. }
  147. return ret;
  148. }
  149. const isNotObjectOrIsArray = (val) => !shared.isObject(val) || shared.isArray(val);
  150. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
  151. function deepCopy(src, des) {
  152. // src and des should both be objects, and non of then can be a array
  153. if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) {
  154. throw createI18nError(I18nErrorCodes.INVALID_VALUE);
  155. }
  156. for (const key in src) {
  157. if (shared.hasOwn(src, key)) {
  158. if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) {
  159. // replace with src[key] when:
  160. // src[key] or des[key] is not a object, or
  161. // src[key] or des[key] is a array
  162. des[key] = src[key];
  163. }
  164. else {
  165. // src[key] and des[key] are both object, merge them
  166. deepCopy(src[key], des[key]);
  167. }
  168. }
  169. }
  170. }
  171. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  172. function getComponentOptions(instance) {
  173. return instance.type ;
  174. }
  175. function adjustI18nResources(gl, options, componentOptions // eslint-disable-line @typescript-eslint/no-explicit-any
  176. ) {
  177. let messages = shared.isObject(options.messages) ? options.messages : {};
  178. if ('__i18nGlobal' in componentOptions) {
  179. messages = getLocaleMessages(gl.locale.value, {
  180. messages,
  181. __i18n: componentOptions.__i18nGlobal
  182. });
  183. }
  184. // merge locale messages
  185. const locales = Object.keys(messages);
  186. if (locales.length) {
  187. locales.forEach(locale => {
  188. gl.mergeLocaleMessage(locale, messages[locale]);
  189. });
  190. }
  191. {
  192. // merge datetime formats
  193. if (shared.isObject(options.datetimeFormats)) {
  194. const locales = Object.keys(options.datetimeFormats);
  195. if (locales.length) {
  196. locales.forEach(locale => {
  197. gl.mergeDateTimeFormat(locale, options.datetimeFormats[locale]);
  198. });
  199. }
  200. }
  201. // merge number formats
  202. if (shared.isObject(options.numberFormats)) {
  203. const locales = Object.keys(options.numberFormats);
  204. if (locales.length) {
  205. locales.forEach(locale => {
  206. gl.mergeNumberFormat(locale, options.numberFormats[locale]);
  207. });
  208. }
  209. }
  210. }
  211. }
  212. function createTextNode(key) {
  213. return vue.createVNode(vue.Text, null, key, 0)
  214. ;
  215. }
  216. /* eslint-enable @typescript-eslint/no-explicit-any */
  217. /* eslint-disable @typescript-eslint/no-explicit-any */
  218. // extend VNode interface
  219. const DEVTOOLS_META = '__INTLIFY_META__';
  220. let composerID = 0;
  221. function defineCoreMissingHandler(missing) {
  222. return ((ctx, locale, key, type) => {
  223. return missing(locale, key, vue.getCurrentInstance() || undefined, type);
  224. });
  225. }
  226. // for Intlify DevTools
  227. const getMetaInfo = /* #__PURE__*/ () => {
  228. const instance = vue.getCurrentInstance();
  229. let meta = null; // eslint-disable-line @typescript-eslint/no-explicit-any
  230. return instance && (meta = getComponentOptions(instance)[DEVTOOLS_META])
  231. ? { [DEVTOOLS_META]: meta } // eslint-disable-line @typescript-eslint/no-explicit-any
  232. : null;
  233. };
  234. /**
  235. * Create composer interface factory
  236. *
  237. * @internal
  238. */
  239. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  240. function createComposer(options = {}, VueI18nLegacy) {
  241. const { __root, __injectWithOption } = options;
  242. const _isGlobal = __root === undefined;
  243. let _inheritLocale = shared.isBoolean(options.inheritLocale)
  244. ? options.inheritLocale
  245. : true;
  246. const _locale = vue.ref(
  247. // prettier-ignore
  248. __root && _inheritLocale
  249. ? __root.locale.value
  250. : shared.isString(options.locale)
  251. ? options.locale
  252. : coreBase.DEFAULT_LOCALE);
  253. const _fallbackLocale = vue.ref(
  254. // prettier-ignore
  255. __root && _inheritLocale
  256. ? __root.fallbackLocale.value
  257. : shared.isString(options.fallbackLocale) ||
  258. shared.isArray(options.fallbackLocale) ||
  259. shared.isPlainObject(options.fallbackLocale) ||
  260. options.fallbackLocale === false
  261. ? options.fallbackLocale
  262. : _locale.value);
  263. const _messages = vue.ref(getLocaleMessages(_locale.value, options));
  264. // prettier-ignore
  265. const _datetimeFormats = vue.ref(shared.isPlainObject(options.datetimeFormats)
  266. ? options.datetimeFormats
  267. : { [_locale.value]: {} })
  268. ;
  269. // prettier-ignore
  270. const _numberFormats = vue.ref(shared.isPlainObject(options.numberFormats)
  271. ? options.numberFormats
  272. : { [_locale.value]: {} })
  273. ;
  274. // warning suppress options
  275. // prettier-ignore
  276. let _missingWarn = __root
  277. ? __root.missingWarn
  278. : shared.isBoolean(options.missingWarn) || shared.isRegExp(options.missingWarn)
  279. ? options.missingWarn
  280. : true;
  281. // prettier-ignore
  282. let _fallbackWarn = __root
  283. ? __root.fallbackWarn
  284. : shared.isBoolean(options.fallbackWarn) || shared.isRegExp(options.fallbackWarn)
  285. ? options.fallbackWarn
  286. : true;
  287. // prettier-ignore
  288. let _fallbackRoot = __root
  289. ? __root.fallbackRoot
  290. : shared.isBoolean(options.fallbackRoot)
  291. ? options.fallbackRoot
  292. : true;
  293. // configure fall back to root
  294. let _fallbackFormat = !!options.fallbackFormat;
  295. // runtime missing
  296. let _missing = shared.isFunction(options.missing) ? options.missing : null;
  297. let _runtimeMissing = shared.isFunction(options.missing)
  298. ? defineCoreMissingHandler(options.missing)
  299. : null;
  300. // postTranslation handler
  301. let _postTranslation = shared.isFunction(options.postTranslation)
  302. ? options.postTranslation
  303. : null;
  304. // prettier-ignore
  305. let _warnHtmlMessage = __root
  306. ? __root.warnHtmlMessage
  307. : shared.isBoolean(options.warnHtmlMessage)
  308. ? options.warnHtmlMessage
  309. : true;
  310. let _escapeParameter = !!options.escapeParameter;
  311. // custom linked modifiers
  312. // prettier-ignore
  313. const _modifiers = __root
  314. ? __root.modifiers
  315. : shared.isPlainObject(options.modifiers)
  316. ? options.modifiers
  317. : {};
  318. // pluralRules
  319. let _pluralRules = options.pluralRules || (__root && __root.pluralRules);
  320. // runtime context
  321. // eslint-disable-next-line prefer-const
  322. let _context;
  323. const getCoreContext = () => {
  324. _isGlobal && coreBase.setFallbackContext(null);
  325. const ctxOptions = {
  326. version: VERSION,
  327. locale: _locale.value,
  328. fallbackLocale: _fallbackLocale.value,
  329. messages: _messages.value,
  330. modifiers: _modifiers,
  331. pluralRules: _pluralRules,
  332. missing: _runtimeMissing === null ? undefined : _runtimeMissing,
  333. missingWarn: _missingWarn,
  334. fallbackWarn: _fallbackWarn,
  335. fallbackFormat: _fallbackFormat,
  336. unresolving: true,
  337. postTranslation: _postTranslation === null ? undefined : _postTranslation,
  338. warnHtmlMessage: _warnHtmlMessage,
  339. escapeParameter: _escapeParameter,
  340. messageResolver: options.messageResolver,
  341. messageCompiler: options.messageCompiler,
  342. __meta: { framework: 'vue' }
  343. };
  344. {
  345. ctxOptions.datetimeFormats = _datetimeFormats.value;
  346. ctxOptions.numberFormats = _numberFormats.value;
  347. ctxOptions.__datetimeFormatters = shared.isPlainObject(_context)
  348. ? _context.__datetimeFormatters
  349. : undefined;
  350. ctxOptions.__numberFormatters = shared.isPlainObject(_context)
  351. ? _context.__numberFormatters
  352. : undefined;
  353. }
  354. const ctx = coreBase.createCoreContext(ctxOptions);
  355. _isGlobal && coreBase.setFallbackContext(ctx);
  356. return ctx;
  357. };
  358. _context = getCoreContext();
  359. coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  360. // track reactivity
  361. function trackReactivityValues() {
  362. return [
  363. _locale.value,
  364. _fallbackLocale.value,
  365. _messages.value,
  366. _datetimeFormats.value,
  367. _numberFormats.value
  368. ]
  369. ;
  370. }
  371. // locale
  372. const locale = vue.computed({
  373. get: () => _locale.value,
  374. set: val => {
  375. _locale.value = val;
  376. _context.locale = _locale.value;
  377. }
  378. });
  379. // fallbackLocale
  380. const fallbackLocale = vue.computed({
  381. get: () => _fallbackLocale.value,
  382. set: val => {
  383. _fallbackLocale.value = val;
  384. _context.fallbackLocale = _fallbackLocale.value;
  385. coreBase.updateFallbackLocale(_context, _locale.value, val);
  386. }
  387. });
  388. // messages
  389. const messages = vue.computed(() => _messages.value);
  390. // datetimeFormats
  391. const datetimeFormats = /* #__PURE__*/ vue.computed(() => _datetimeFormats.value);
  392. // numberFormats
  393. const numberFormats = /* #__PURE__*/ vue.computed(() => _numberFormats.value);
  394. // getPostTranslationHandler
  395. function getPostTranslationHandler() {
  396. return shared.isFunction(_postTranslation) ? _postTranslation : null;
  397. }
  398. // setPostTranslationHandler
  399. function setPostTranslationHandler(handler) {
  400. _postTranslation = handler;
  401. _context.postTranslation = handler;
  402. }
  403. // getMissingHandler
  404. function getMissingHandler() {
  405. return _missing;
  406. }
  407. // setMissingHandler
  408. function setMissingHandler(handler) {
  409. if (handler !== null) {
  410. _runtimeMissing = defineCoreMissingHandler(handler);
  411. }
  412. _missing = handler;
  413. _context.missing = _runtimeMissing;
  414. }
  415. const wrapWithDeps = (fn, argumentParser, warnType, fallbackSuccess, fallbackFail, successCondition) => {
  416. trackReactivityValues(); // track reactive dependency
  417. // NOTE: experimental !!
  418. let ret;
  419. try {
  420. if (false || false) ;
  421. if (!_isGlobal) {
  422. _context.fallbackContext = __root
  423. ? coreBase.getFallbackContext()
  424. : undefined;
  425. }
  426. ret = fn(_context);
  427. }
  428. finally {
  429. if (!_isGlobal) {
  430. _context.fallbackContext = undefined;
  431. }
  432. }
  433. if (shared.isNumber(ret) && ret === coreBase.NOT_REOSLVED) {
  434. const [key, arg2] = argumentParser();
  435. return __root && _fallbackRoot
  436. ? fallbackSuccess(__root)
  437. : fallbackFail(key);
  438. }
  439. else if (successCondition(ret)) {
  440. return ret;
  441. }
  442. else {
  443. /* istanbul ignore next */
  444. throw createI18nError(I18nErrorCodes.UNEXPECTED_RETURN_TYPE);
  445. }
  446. };
  447. // t
  448. function t(...args) {
  449. return wrapWithDeps(context => Reflect.apply(coreBase.translate, null, [context, ...args]), () => coreBase.parseTranslateArgs(...args), 'translate', root => Reflect.apply(root.t, root, [...args]), key => key, val => shared.isString(val));
  450. }
  451. // rt
  452. function rt(...args) {
  453. const [arg1, arg2, arg3] = args;
  454. if (arg3 && !shared.isObject(arg3)) {
  455. throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
  456. }
  457. return t(...[arg1, arg2, shared.assign({ resolvedMessage: true }, arg3 || {})]);
  458. }
  459. // d
  460. function d(...args) {
  461. return wrapWithDeps(context => Reflect.apply(coreBase.datetime, null, [context, ...args]), () => coreBase.parseDateTimeArgs(...args), 'datetime format', root => Reflect.apply(root.d, root, [...args]), () => coreBase.MISSING_RESOLVE_VALUE, val => shared.isString(val));
  462. }
  463. // n
  464. function n(...args) {
  465. return wrapWithDeps(context => Reflect.apply(coreBase.number, null, [context, ...args]), () => coreBase.parseNumberArgs(...args), 'number format', root => Reflect.apply(root.n, root, [...args]), () => coreBase.MISSING_RESOLVE_VALUE, val => shared.isString(val));
  466. }
  467. // for custom processor
  468. function normalize(values) {
  469. return values.map(val => shared.isString(val) || shared.isNumber(val) || shared.isBoolean(val)
  470. ? createTextNode(String(val))
  471. : val);
  472. }
  473. const interpolate = (val) => val;
  474. const processor = {
  475. normalize,
  476. interpolate,
  477. type: 'vnode'
  478. };
  479. // translateVNode, using for `i18n-t` component
  480. function translateVNode(...args) {
  481. return wrapWithDeps(context => {
  482. let ret;
  483. const _context = context;
  484. try {
  485. _context.processor = processor;
  486. ret = Reflect.apply(coreBase.translate, null, [_context, ...args]);
  487. }
  488. finally {
  489. _context.processor = null;
  490. }
  491. return ret;
  492. }, () => coreBase.parseTranslateArgs(...args), 'translate',
  493. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  494. root => root[TranslateVNodeSymbol](...args), key => [createTextNode(key)], val => shared.isArray(val));
  495. }
  496. // numberParts, using for `i18n-n` component
  497. function numberParts(...args) {
  498. return wrapWithDeps(context => Reflect.apply(coreBase.number, null, [context, ...args]), () => coreBase.parseNumberArgs(...args), 'number format',
  499. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  500. root => root[NumberPartsSymbol](...args), () => [], val => shared.isString(val) || shared.isArray(val));
  501. }
  502. // datetimeParts, using for `i18n-d` component
  503. function datetimeParts(...args) {
  504. return wrapWithDeps(context => Reflect.apply(coreBase.datetime, null, [context, ...args]), () => coreBase.parseDateTimeArgs(...args), 'datetime format',
  505. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  506. root => root[DatetimePartsSymbol](...args), () => [], val => shared.isString(val) || shared.isArray(val));
  507. }
  508. function setPluralRules(rules) {
  509. _pluralRules = rules;
  510. _context.pluralRules = _pluralRules;
  511. }
  512. // te
  513. function te(key, locale) {
  514. if (!key)
  515. return false;
  516. const targetLocale = shared.isString(locale) ? locale : _locale.value;
  517. const message = getLocaleMessage(targetLocale);
  518. return _context.messageResolver(message, key) !== null;
  519. }
  520. function resolveMessages(key) {
  521. let messages = null;
  522. const locales = coreBase.fallbackWithLocaleChain(_context, _fallbackLocale.value, _locale.value);
  523. for (let i = 0; i < locales.length; i++) {
  524. const targetLocaleMessages = _messages.value[locales[i]] || {};
  525. const messageValue = _context.messageResolver(targetLocaleMessages, key);
  526. if (messageValue != null) {
  527. messages = messageValue;
  528. break;
  529. }
  530. }
  531. return messages;
  532. }
  533. // tm
  534. function tm(key) {
  535. const messages = resolveMessages(key);
  536. // prettier-ignore
  537. return messages != null
  538. ? messages
  539. : __root
  540. ? __root.tm(key) || {}
  541. : {};
  542. }
  543. // getLocaleMessage
  544. function getLocaleMessage(locale) {
  545. return (_messages.value[locale] || {});
  546. }
  547. // setLocaleMessage
  548. function setLocaleMessage(locale, message) {
  549. _messages.value[locale] = message;
  550. _context.messages = _messages.value;
  551. }
  552. // mergeLocaleMessage
  553. function mergeLocaleMessage(locale, message) {
  554. _messages.value[locale] = _messages.value[locale] || {};
  555. deepCopy(message, _messages.value[locale]);
  556. _context.messages = _messages.value;
  557. }
  558. // getDateTimeFormat
  559. function getDateTimeFormat(locale) {
  560. return _datetimeFormats.value[locale] || {};
  561. }
  562. // setDateTimeFormat
  563. function setDateTimeFormat(locale, format) {
  564. _datetimeFormats.value[locale] = format;
  565. _context.datetimeFormats = _datetimeFormats.value;
  566. coreBase.clearDateTimeFormat(_context, locale, format);
  567. }
  568. // mergeDateTimeFormat
  569. function mergeDateTimeFormat(locale, format) {
  570. _datetimeFormats.value[locale] = shared.assign(_datetimeFormats.value[locale] || {}, format);
  571. _context.datetimeFormats = _datetimeFormats.value;
  572. coreBase.clearDateTimeFormat(_context, locale, format);
  573. }
  574. // getNumberFormat
  575. function getNumberFormat(locale) {
  576. return _numberFormats.value[locale] || {};
  577. }
  578. // setNumberFormat
  579. function setNumberFormat(locale, format) {
  580. _numberFormats.value[locale] = format;
  581. _context.numberFormats = _numberFormats.value;
  582. coreBase.clearNumberFormat(_context, locale, format);
  583. }
  584. // mergeNumberFormat
  585. function mergeNumberFormat(locale, format) {
  586. _numberFormats.value[locale] = shared.assign(_numberFormats.value[locale] || {}, format);
  587. _context.numberFormats = _numberFormats.value;
  588. coreBase.clearNumberFormat(_context, locale, format);
  589. }
  590. // for debug
  591. composerID++;
  592. // watch root locale & fallbackLocale
  593. if (__root && shared.inBrowser) {
  594. vue.watch(__root.locale, (val) => {
  595. if (_inheritLocale) {
  596. _locale.value = val;
  597. _context.locale = val;
  598. coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  599. }
  600. });
  601. vue.watch(__root.fallbackLocale, (val) => {
  602. if (_inheritLocale) {
  603. _fallbackLocale.value = val;
  604. _context.fallbackLocale = val;
  605. coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  606. }
  607. });
  608. }
  609. // define basic composition API!
  610. const composer = {
  611. id: composerID,
  612. locale,
  613. fallbackLocale,
  614. get inheritLocale() {
  615. return _inheritLocale;
  616. },
  617. set inheritLocale(val) {
  618. _inheritLocale = val;
  619. if (val && __root) {
  620. _locale.value = __root.locale.value;
  621. _fallbackLocale.value = __root.fallbackLocale.value;
  622. coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  623. }
  624. },
  625. get availableLocales() {
  626. return Object.keys(_messages.value).sort();
  627. },
  628. messages,
  629. get modifiers() {
  630. return _modifiers;
  631. },
  632. get pluralRules() {
  633. return _pluralRules || {};
  634. },
  635. get isGlobal() {
  636. return _isGlobal;
  637. },
  638. get missingWarn() {
  639. return _missingWarn;
  640. },
  641. set missingWarn(val) {
  642. _missingWarn = val;
  643. _context.missingWarn = _missingWarn;
  644. },
  645. get fallbackWarn() {
  646. return _fallbackWarn;
  647. },
  648. set fallbackWarn(val) {
  649. _fallbackWarn = val;
  650. _context.fallbackWarn = _fallbackWarn;
  651. },
  652. get fallbackRoot() {
  653. return _fallbackRoot;
  654. },
  655. set fallbackRoot(val) {
  656. _fallbackRoot = val;
  657. },
  658. get fallbackFormat() {
  659. return _fallbackFormat;
  660. },
  661. set fallbackFormat(val) {
  662. _fallbackFormat = val;
  663. _context.fallbackFormat = _fallbackFormat;
  664. },
  665. get warnHtmlMessage() {
  666. return _warnHtmlMessage;
  667. },
  668. set warnHtmlMessage(val) {
  669. _warnHtmlMessage = val;
  670. _context.warnHtmlMessage = val;
  671. },
  672. get escapeParameter() {
  673. return _escapeParameter;
  674. },
  675. set escapeParameter(val) {
  676. _escapeParameter = val;
  677. _context.escapeParameter = val;
  678. },
  679. t,
  680. getLocaleMessage,
  681. setLocaleMessage,
  682. mergeLocaleMessage,
  683. getPostTranslationHandler,
  684. setPostTranslationHandler,
  685. getMissingHandler,
  686. setMissingHandler,
  687. [SetPluralRulesSymbol]: setPluralRules
  688. };
  689. {
  690. composer.datetimeFormats = datetimeFormats;
  691. composer.numberFormats = numberFormats;
  692. composer.rt = rt;
  693. composer.te = te;
  694. composer.tm = tm;
  695. composer.d = d;
  696. composer.n = n;
  697. composer.getDateTimeFormat = getDateTimeFormat;
  698. composer.setDateTimeFormat = setDateTimeFormat;
  699. composer.mergeDateTimeFormat = mergeDateTimeFormat;
  700. composer.getNumberFormat = getNumberFormat;
  701. composer.setNumberFormat = setNumberFormat;
  702. composer.mergeNumberFormat = mergeNumberFormat;
  703. composer[InejctWithOptionSymbol] = __injectWithOption;
  704. composer[TranslateVNodeSymbol] = translateVNode;
  705. composer[DatetimePartsSymbol] = datetimeParts;
  706. composer[NumberPartsSymbol] = numberParts;
  707. }
  708. return composer;
  709. }
  710. /* eslint-enable @typescript-eslint/no-explicit-any */
  711. /* eslint-disable @typescript-eslint/no-explicit-any */
  712. /**
  713. * Convert to I18n Composer Options from VueI18n Options
  714. *
  715. * @internal
  716. */
  717. function convertComposerOptions(options) {
  718. const locale = shared.isString(options.locale) ? options.locale : coreBase.DEFAULT_LOCALE;
  719. const fallbackLocale = shared.isString(options.fallbackLocale) ||
  720. shared.isArray(options.fallbackLocale) ||
  721. shared.isPlainObject(options.fallbackLocale) ||
  722. options.fallbackLocale === false
  723. ? options.fallbackLocale
  724. : locale;
  725. const missing = shared.isFunction(options.missing) ? options.missing : undefined;
  726. const missingWarn = shared.isBoolean(options.silentTranslationWarn) ||
  727. shared.isRegExp(options.silentTranslationWarn)
  728. ? !options.silentTranslationWarn
  729. : true;
  730. const fallbackWarn = shared.isBoolean(options.silentFallbackWarn) ||
  731. shared.isRegExp(options.silentFallbackWarn)
  732. ? !options.silentFallbackWarn
  733. : true;
  734. const fallbackRoot = shared.isBoolean(options.fallbackRoot)
  735. ? options.fallbackRoot
  736. : true;
  737. const fallbackFormat = !!options.formatFallbackMessages;
  738. const modifiers = shared.isPlainObject(options.modifiers) ? options.modifiers : {};
  739. const pluralizationRules = options.pluralizationRules;
  740. const postTranslation = shared.isFunction(options.postTranslation)
  741. ? options.postTranslation
  742. : undefined;
  743. const warnHtmlMessage = shared.isString(options.warnHtmlInMessage)
  744. ? options.warnHtmlInMessage !== 'off'
  745. : true;
  746. const escapeParameter = !!options.escapeParameterHtml;
  747. const inheritLocale = shared.isBoolean(options.sync) ? options.sync : true;
  748. let messages = options.messages;
  749. if (shared.isPlainObject(options.sharedMessages)) {
  750. const sharedMessages = options.sharedMessages;
  751. const locales = Object.keys(sharedMessages);
  752. messages = locales.reduce((messages, locale) => {
  753. const message = messages[locale] || (messages[locale] = {});
  754. shared.assign(message, sharedMessages[locale]);
  755. return messages;
  756. }, (messages || {}));
  757. }
  758. const { __i18n, __root, __injectWithOption } = options;
  759. const datetimeFormats = options.datetimeFormats;
  760. const numberFormats = options.numberFormats;
  761. const flatJson = options.flatJson;
  762. return {
  763. locale,
  764. fallbackLocale,
  765. messages,
  766. flatJson,
  767. datetimeFormats,
  768. numberFormats,
  769. missing,
  770. missingWarn,
  771. fallbackWarn,
  772. fallbackRoot,
  773. fallbackFormat,
  774. modifiers,
  775. pluralRules: pluralizationRules,
  776. postTranslation,
  777. warnHtmlMessage,
  778. escapeParameter,
  779. messageResolver: options.messageResolver,
  780. inheritLocale,
  781. __i18n,
  782. __root,
  783. __injectWithOption
  784. };
  785. }
  786. /**
  787. * create VueI18n interface factory
  788. *
  789. * @internal
  790. */
  791. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  792. function createVueI18n(options = {}, VueI18nLegacy) {
  793. {
  794. const composer = createComposer(convertComposerOptions(options));
  795. const { __extender } = options;
  796. // defines VueI18n
  797. const vueI18n = {
  798. // id
  799. id: composer.id,
  800. // locale
  801. get locale() {
  802. return composer.locale.value;
  803. },
  804. set locale(val) {
  805. composer.locale.value = val;
  806. },
  807. // fallbackLocale
  808. get fallbackLocale() {
  809. return composer.fallbackLocale.value;
  810. },
  811. set fallbackLocale(val) {
  812. composer.fallbackLocale.value = val;
  813. },
  814. // messages
  815. get messages() {
  816. return composer.messages.value;
  817. },
  818. // datetimeFormats
  819. get datetimeFormats() {
  820. return composer.datetimeFormats.value;
  821. },
  822. // numberFormats
  823. get numberFormats() {
  824. return composer.numberFormats.value;
  825. },
  826. // availableLocales
  827. get availableLocales() {
  828. return composer.availableLocales;
  829. },
  830. // formatter
  831. get formatter() {
  832. // dummy
  833. return {
  834. interpolate() {
  835. return [];
  836. }
  837. };
  838. },
  839. set formatter(val) {
  840. },
  841. // missing
  842. get missing() {
  843. return composer.getMissingHandler();
  844. },
  845. set missing(handler) {
  846. composer.setMissingHandler(handler);
  847. },
  848. // silentTranslationWarn
  849. get silentTranslationWarn() {
  850. return shared.isBoolean(composer.missingWarn)
  851. ? !composer.missingWarn
  852. : composer.missingWarn;
  853. },
  854. set silentTranslationWarn(val) {
  855. composer.missingWarn = shared.isBoolean(val) ? !val : val;
  856. },
  857. // silentFallbackWarn
  858. get silentFallbackWarn() {
  859. return shared.isBoolean(composer.fallbackWarn)
  860. ? !composer.fallbackWarn
  861. : composer.fallbackWarn;
  862. },
  863. set silentFallbackWarn(val) {
  864. composer.fallbackWarn = shared.isBoolean(val) ? !val : val;
  865. },
  866. // modifiers
  867. get modifiers() {
  868. return composer.modifiers;
  869. },
  870. // formatFallbackMessages
  871. get formatFallbackMessages() {
  872. return composer.fallbackFormat;
  873. },
  874. set formatFallbackMessages(val) {
  875. composer.fallbackFormat = val;
  876. },
  877. // postTranslation
  878. get postTranslation() {
  879. return composer.getPostTranslationHandler();
  880. },
  881. set postTranslation(handler) {
  882. composer.setPostTranslationHandler(handler);
  883. },
  884. // sync
  885. get sync() {
  886. return composer.inheritLocale;
  887. },
  888. set sync(val) {
  889. composer.inheritLocale = val;
  890. },
  891. // warnInHtmlMessage
  892. get warnHtmlInMessage() {
  893. return composer.warnHtmlMessage ? 'warn' : 'off';
  894. },
  895. set warnHtmlInMessage(val) {
  896. composer.warnHtmlMessage = val !== 'off';
  897. },
  898. // escapeParameterHtml
  899. get escapeParameterHtml() {
  900. return composer.escapeParameter;
  901. },
  902. set escapeParameterHtml(val) {
  903. composer.escapeParameter = val;
  904. },
  905. // preserveDirectiveContent
  906. get preserveDirectiveContent() {
  907. return true;
  908. },
  909. set preserveDirectiveContent(val) {
  910. },
  911. // pluralizationRules
  912. get pluralizationRules() {
  913. return composer.pluralRules || {};
  914. },
  915. // for internal
  916. __composer: composer,
  917. // t
  918. t(...args) {
  919. const [arg1, arg2, arg3] = args;
  920. const options = {};
  921. let list = null;
  922. let named = null;
  923. if (!shared.isString(arg1)) {
  924. throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
  925. }
  926. const key = arg1;
  927. if (shared.isString(arg2)) {
  928. options.locale = arg2;
  929. }
  930. else if (shared.isArray(arg2)) {
  931. list = arg2;
  932. }
  933. else if (shared.isPlainObject(arg2)) {
  934. named = arg2;
  935. }
  936. if (shared.isArray(arg3)) {
  937. list = arg3;
  938. }
  939. else if (shared.isPlainObject(arg3)) {
  940. named = arg3;
  941. }
  942. // return composer.t(key, (list || named || {}) as any, options)
  943. return Reflect.apply(composer.t, composer, [
  944. key,
  945. (list || named || {}),
  946. options
  947. ]);
  948. },
  949. rt(...args) {
  950. return Reflect.apply(composer.rt, composer, [...args]);
  951. },
  952. // tc
  953. tc(...args) {
  954. const [arg1, arg2, arg3] = args;
  955. const options = { plural: 1 };
  956. let list = null;
  957. let named = null;
  958. if (!shared.isString(arg1)) {
  959. throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
  960. }
  961. const key = arg1;
  962. if (shared.isString(arg2)) {
  963. options.locale = arg2;
  964. }
  965. else if (shared.isNumber(arg2)) {
  966. options.plural = arg2;
  967. }
  968. else if (shared.isArray(arg2)) {
  969. list = arg2;
  970. }
  971. else if (shared.isPlainObject(arg2)) {
  972. named = arg2;
  973. }
  974. if (shared.isString(arg3)) {
  975. options.locale = arg3;
  976. }
  977. else if (shared.isArray(arg3)) {
  978. list = arg3;
  979. }
  980. else if (shared.isPlainObject(arg3)) {
  981. named = arg3;
  982. }
  983. // return composer.t(key, (list || named || {}) as any, options)
  984. return Reflect.apply(composer.t, composer, [
  985. key,
  986. (list || named || {}),
  987. options
  988. ]);
  989. },
  990. // te
  991. te(key, locale) {
  992. return composer.te(key, locale);
  993. },
  994. // tm
  995. tm(key) {
  996. return composer.tm(key);
  997. },
  998. // getLocaleMessage
  999. getLocaleMessage(locale) {
  1000. return composer.getLocaleMessage(locale);
  1001. },
  1002. // setLocaleMessage
  1003. setLocaleMessage(locale, message) {
  1004. composer.setLocaleMessage(locale, message);
  1005. },
  1006. // mergeLocaleMessage
  1007. mergeLocaleMessage(locale, message) {
  1008. composer.mergeLocaleMessage(locale, message);
  1009. },
  1010. // d
  1011. d(...args) {
  1012. return Reflect.apply(composer.d, composer, [...args]);
  1013. },
  1014. // getDateTimeFormat
  1015. getDateTimeFormat(locale) {
  1016. return composer.getDateTimeFormat(locale);
  1017. },
  1018. // setDateTimeFormat
  1019. setDateTimeFormat(locale, format) {
  1020. composer.setDateTimeFormat(locale, format);
  1021. },
  1022. // mergeDateTimeFormat
  1023. mergeDateTimeFormat(locale, format) {
  1024. composer.mergeDateTimeFormat(locale, format);
  1025. },
  1026. // n
  1027. n(...args) {
  1028. return Reflect.apply(composer.n, composer, [...args]);
  1029. },
  1030. // getNumberFormat
  1031. getNumberFormat(locale) {
  1032. return composer.getNumberFormat(locale);
  1033. },
  1034. // setNumberFormat
  1035. setNumberFormat(locale, format) {
  1036. composer.setNumberFormat(locale, format);
  1037. },
  1038. // mergeNumberFormat
  1039. mergeNumberFormat(locale, format) {
  1040. composer.mergeNumberFormat(locale, format);
  1041. },
  1042. // getChoiceIndex
  1043. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  1044. getChoiceIndex(choice, choicesLength) {
  1045. return -1;
  1046. }
  1047. };
  1048. vueI18n.__extender = __extender;
  1049. return vueI18n;
  1050. }
  1051. }
  1052. /* eslint-enable @typescript-eslint/no-explicit-any */
  1053. const baseFormatProps = {
  1054. tag: {
  1055. type: [String, Object]
  1056. },
  1057. locale: {
  1058. type: String
  1059. },
  1060. scope: {
  1061. type: String,
  1062. // NOTE: avoid https://github.com/microsoft/rushstack/issues/1050
  1063. validator: (val /* ComponentI18nScope */) => val === 'parent' || val === 'global',
  1064. default: 'parent' /* ComponentI18nScope */
  1065. },
  1066. i18n: {
  1067. type: Object
  1068. }
  1069. };
  1070. function getInterpolateArg(
  1071. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1072. { slots }, // SetupContext,
  1073. keys) {
  1074. if (keys.length === 1 && keys[0] === 'default') {
  1075. // default slot with list
  1076. const ret = slots.default ? slots.default() : [];
  1077. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1078. return ret.reduce((slot, current) => {
  1079. return [
  1080. ...slot,
  1081. // prettier-ignore
  1082. ...(current.type === vue.Fragment ? current.children : [current]
  1083. )
  1084. ];
  1085. }, []);
  1086. }
  1087. else {
  1088. // named slots
  1089. return keys.reduce((arg, key) => {
  1090. const slot = slots[key];
  1091. if (slot) {
  1092. arg[key] = slot();
  1093. }
  1094. return arg;
  1095. }, {});
  1096. }
  1097. }
  1098. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1099. function getFragmentableTag(tag) {
  1100. return vue.Fragment ;
  1101. }
  1102. const TranslationImpl = /*#__PURE__*/ vue.defineComponent({
  1103. /* eslint-disable */
  1104. name: 'i18n-t',
  1105. props: shared.assign({
  1106. keypath: {
  1107. type: String,
  1108. required: true
  1109. },
  1110. plural: {
  1111. type: [Number, String],
  1112. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1113. validator: (val) => shared.isNumber(val) || !isNaN(val)
  1114. }
  1115. }, baseFormatProps),
  1116. /* eslint-enable */
  1117. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1118. setup(props, context) {
  1119. const { slots, attrs } = context;
  1120. // NOTE: avoid https://github.com/microsoft/rushstack/issues/1050
  1121. const i18n = props.i18n ||
  1122. useI18n({
  1123. useScope: props.scope,
  1124. __useComponent: true
  1125. });
  1126. return () => {
  1127. const keys = Object.keys(slots).filter(key => key !== '_');
  1128. const options = {};
  1129. if (props.locale) {
  1130. options.locale = props.locale;
  1131. }
  1132. if (props.plural !== undefined) {
  1133. options.plural = shared.isString(props.plural) ? +props.plural : props.plural;
  1134. }
  1135. const arg = getInterpolateArg(context, keys);
  1136. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1137. const children = i18n[TranslateVNodeSymbol](props.keypath, arg, options);
  1138. const assignedAttrs = shared.assign({}, attrs);
  1139. const tag = shared.isString(props.tag) || shared.isObject(props.tag)
  1140. ? props.tag
  1141. : getFragmentableTag();
  1142. return vue.h(tag, assignedAttrs, children);
  1143. };
  1144. }
  1145. });
  1146. /**
  1147. * export the public type for h/tsx inference
  1148. * also to avoid inline import() in generated d.ts files
  1149. */
  1150. /**
  1151. * Translation Component
  1152. *
  1153. * @remarks
  1154. * See the following items for property about details
  1155. *
  1156. * @VueI18nSee [TranslationProps](component#translationprops)
  1157. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  1158. * @VueI18nSee [Component Interpolation](../guide/advanced/component)
  1159. *
  1160. * @example
  1161. * ```html
  1162. * <div id="app">
  1163. * <!-- ... -->
  1164. * <i18n keypath="term" tag="label" for="tos">
  1165. * <a :href="url" target="_blank">{{ $t('tos') }}</a>
  1166. * </i18n>
  1167. * <!-- ... -->
  1168. * </div>
  1169. * ```
  1170. * ```js
  1171. * import { createApp } from 'vue'
  1172. * import { createI18n } from 'vue-i18n'
  1173. *
  1174. * const messages = {
  1175. * en: {
  1176. * tos: 'Term of Service',
  1177. * term: 'I accept xxx {0}.'
  1178. * },
  1179. * ja: {
  1180. * tos: '利用規約',
  1181. * term: '私は xxx の{0}に同意します。'
  1182. * }
  1183. * }
  1184. *
  1185. * const i18n = createI18n({
  1186. * locale: 'en',
  1187. * messages
  1188. * })
  1189. *
  1190. * const app = createApp({
  1191. * data: {
  1192. * url: '/term'
  1193. * }
  1194. * }).use(i18n).mount('#app')
  1195. * ```
  1196. *
  1197. * @VueI18nComponent
  1198. */
  1199. const Translation = TranslationImpl;
  1200. const I18nT = Translation;
  1201. function isVNode(target) {
  1202. return shared.isArray(target) && !shared.isString(target[0]);
  1203. }
  1204. function renderFormatter(props, context, slotKeys, partFormatter) {
  1205. const { slots, attrs } = context;
  1206. return () => {
  1207. const options = { part: true };
  1208. let overrides = {};
  1209. if (props.locale) {
  1210. options.locale = props.locale;
  1211. }
  1212. if (shared.isString(props.format)) {
  1213. options.key = props.format;
  1214. }
  1215. else if (shared.isObject(props.format)) {
  1216. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1217. if (shared.isString(props.format.key)) {
  1218. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1219. options.key = props.format.key;
  1220. }
  1221. // Filter out number format options only
  1222. overrides = Object.keys(props.format).reduce((options, prop) => {
  1223. return slotKeys.includes(prop)
  1224. ? shared.assign({}, options, { [prop]: props.format[prop] }) // eslint-disable-line @typescript-eslint/no-explicit-any
  1225. : options;
  1226. }, {});
  1227. }
  1228. const parts = partFormatter(...[props.value, options, overrides]);
  1229. let children = [options.key];
  1230. if (shared.isArray(parts)) {
  1231. children = parts.map((part, index) => {
  1232. const slot = slots[part.type];
  1233. const node = slot
  1234. ? slot({ [part.type]: part.value, index, parts })
  1235. : [part.value];
  1236. if (isVNode(node)) {
  1237. node[0].key = `${part.type}-${index}`;
  1238. }
  1239. return node;
  1240. });
  1241. }
  1242. else if (shared.isString(parts)) {
  1243. children = [parts];
  1244. }
  1245. const assignedAttrs = shared.assign({}, attrs);
  1246. const tag = shared.isString(props.tag) || shared.isObject(props.tag)
  1247. ? props.tag
  1248. : getFragmentableTag();
  1249. return vue.h(tag, assignedAttrs, children);
  1250. };
  1251. }
  1252. const NumberFormatImpl = /*#__PURE__*/ vue.defineComponent({
  1253. /* eslint-disable */
  1254. name: 'i18n-n',
  1255. props: shared.assign({
  1256. value: {
  1257. type: Number,
  1258. required: true
  1259. },
  1260. format: {
  1261. type: [String, Object]
  1262. }
  1263. }, baseFormatProps),
  1264. /* eslint-enable */
  1265. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1266. setup(props, context) {
  1267. const i18n = props.i18n ||
  1268. useI18n({
  1269. useScope: 'parent',
  1270. __useComponent: true
  1271. });
  1272. return renderFormatter(props, context, coreBase.NUMBER_FORMAT_OPTIONS_KEYS, (...args) =>
  1273. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1274. i18n[NumberPartsSymbol](...args));
  1275. }
  1276. });
  1277. /**
  1278. * export the public type for h/tsx inference
  1279. * also to avoid inline import() in generated d.ts files
  1280. */
  1281. /**
  1282. * Number Format Component
  1283. *
  1284. * @remarks
  1285. * See the following items for property about details
  1286. *
  1287. * @VueI18nSee [FormattableProps](component#formattableprops)
  1288. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  1289. * @VueI18nSee [Custom Formatting](../guide/essentials/number#custom-formatting)
  1290. *
  1291. * @VueI18nDanger
  1292. * 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)
  1293. *
  1294. * If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-numberformat)
  1295. *
  1296. * @VueI18nComponent
  1297. */
  1298. const NumberFormat = NumberFormatImpl;
  1299. const I18nN = NumberFormat;
  1300. const DatetimeFormatImpl = /* #__PURE__*/ vue.defineComponent({
  1301. /* eslint-disable */
  1302. name: 'i18n-d',
  1303. props: shared.assign({
  1304. value: {
  1305. type: [Number, Date],
  1306. required: true
  1307. },
  1308. format: {
  1309. type: [String, Object]
  1310. }
  1311. }, baseFormatProps),
  1312. /* eslint-enable */
  1313. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1314. setup(props, context) {
  1315. const i18n = props.i18n ||
  1316. useI18n({
  1317. useScope: 'parent',
  1318. __useComponent: true
  1319. });
  1320. return renderFormatter(props, context, coreBase.DATETIME_FORMAT_OPTIONS_KEYS, (...args) =>
  1321. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1322. i18n[DatetimePartsSymbol](...args));
  1323. }
  1324. });
  1325. /**
  1326. * Datetime Format Component
  1327. *
  1328. * @remarks
  1329. * See the following items for property about details
  1330. *
  1331. * @VueI18nSee [FormattableProps](component#formattableprops)
  1332. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  1333. * @VueI18nSee [Custom Formatting](../guide/essentials/datetime#custom-formatting)
  1334. *
  1335. * @VueI18nDanger
  1336. * 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)
  1337. *
  1338. * If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-datetimeformat)
  1339. *
  1340. * @VueI18nComponent
  1341. */
  1342. const DatetimeFormat = DatetimeFormatImpl;
  1343. const I18nD = DatetimeFormat;
  1344. function getComposer$1(i18n, instance) {
  1345. const i18nInternal = i18n;
  1346. if (i18n.mode === 'composition') {
  1347. return (i18nInternal.__getInstance(instance) || i18n.global);
  1348. }
  1349. else {
  1350. const vueI18n = i18nInternal.__getInstance(instance);
  1351. return vueI18n != null
  1352. ? vueI18n.__composer
  1353. : i18n.global.__composer;
  1354. }
  1355. }
  1356. function vTDirective(i18n) {
  1357. const _process = (binding) => {
  1358. const { instance, modifiers, value } = binding;
  1359. /* istanbul ignore if */
  1360. if (!instance || !instance.$) {
  1361. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  1362. }
  1363. const composer = getComposer$1(i18n, instance.$);
  1364. const parsedValue = parseValue(value);
  1365. return [
  1366. Reflect.apply(composer.t, composer, [...makeParams(parsedValue)]),
  1367. composer
  1368. ];
  1369. };
  1370. const register = (el, binding) => {
  1371. const [textContent, composer] = _process(binding);
  1372. if (shared.inBrowser && i18n.global === composer) {
  1373. // global scope only
  1374. el.__i18nWatcher = vue.watch(composer.locale, () => {
  1375. binding.instance && binding.instance.$forceUpdate();
  1376. });
  1377. }
  1378. el.__composer = composer;
  1379. el.textContent = textContent;
  1380. };
  1381. const unregister = (el) => {
  1382. if (shared.inBrowser && el.__i18nWatcher) {
  1383. el.__i18nWatcher();
  1384. el.__i18nWatcher = undefined;
  1385. delete el.__i18nWatcher;
  1386. }
  1387. if (el.__composer) {
  1388. el.__composer = undefined;
  1389. delete el.__composer;
  1390. }
  1391. };
  1392. const update = (el, { value }) => {
  1393. if (el.__composer) {
  1394. const composer = el.__composer;
  1395. const parsedValue = parseValue(value);
  1396. el.textContent = Reflect.apply(composer.t, composer, [
  1397. ...makeParams(parsedValue)
  1398. ]);
  1399. }
  1400. };
  1401. const getSSRProps = (binding) => {
  1402. const [textContent] = _process(binding);
  1403. return { textContent };
  1404. };
  1405. return {
  1406. created: register,
  1407. unmounted: unregister,
  1408. beforeUpdate: update,
  1409. getSSRProps
  1410. };
  1411. }
  1412. function parseValue(value) {
  1413. if (shared.isString(value)) {
  1414. return { path: value };
  1415. }
  1416. else if (shared.isPlainObject(value)) {
  1417. if (!('path' in value)) {
  1418. throw createI18nError(I18nErrorCodes.REQUIRED_VALUE, 'path');
  1419. }
  1420. return value;
  1421. }
  1422. else {
  1423. throw createI18nError(I18nErrorCodes.INVALID_VALUE);
  1424. }
  1425. }
  1426. function makeParams(value) {
  1427. const { path, locale, args, choice, plural } = value;
  1428. const options = {};
  1429. const named = args || {};
  1430. if (shared.isString(locale)) {
  1431. options.locale = locale;
  1432. }
  1433. if (shared.isNumber(choice)) {
  1434. options.plural = choice;
  1435. }
  1436. if (shared.isNumber(plural)) {
  1437. options.plural = plural;
  1438. }
  1439. return [path, named, options];
  1440. }
  1441. function apply(app, i18n, ...options) {
  1442. const pluginOptions = shared.isPlainObject(options[0])
  1443. ? options[0]
  1444. : {};
  1445. const useI18nComponentName = !!pluginOptions.useI18nComponentName;
  1446. const globalInstall = shared.isBoolean(pluginOptions.globalInstall)
  1447. ? pluginOptions.globalInstall
  1448. : true;
  1449. if (globalInstall) {
  1450. [!useI18nComponentName ? Translation.name : 'i18n', 'I18nT'].forEach(name => app.component(name, Translation));
  1451. [NumberFormat.name, 'I18nN'].forEach(name => app.component(name, NumberFormat));
  1452. [DatetimeFormat.name, 'I18nD'].forEach(name => app.component(name, DatetimeFormat));
  1453. }
  1454. // install directive
  1455. {
  1456. app.directive('t', vTDirective(i18n));
  1457. }
  1458. }
  1459. /**
  1460. * Supports compatibility for legacy vue-i18n APIs
  1461. * This mixin is used when we use vue-i18n@v9.x or later
  1462. */
  1463. function defineMixin(vuei18n, composer, i18n) {
  1464. return {
  1465. beforeCreate() {
  1466. const instance = vue.getCurrentInstance();
  1467. /* istanbul ignore if */
  1468. if (!instance) {
  1469. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  1470. }
  1471. const options = this.$options;
  1472. if (options.i18n) {
  1473. const optionsI18n = options.i18n;
  1474. if (options.__i18n) {
  1475. optionsI18n.__i18n = options.__i18n;
  1476. }
  1477. optionsI18n.__root = composer;
  1478. if (this === this.$root) {
  1479. // merge option and gttach global
  1480. this.$i18n = mergeToGlobal(vuei18n, optionsI18n);
  1481. }
  1482. else {
  1483. optionsI18n.__injectWithOption = true;
  1484. optionsI18n.__extender = i18n.__vueI18nExtend;
  1485. // atttach local VueI18n instance
  1486. this.$i18n = createVueI18n(optionsI18n);
  1487. // extend VueI18n instance
  1488. const _vueI18n = this.$i18n;
  1489. if (_vueI18n.__extender) {
  1490. _vueI18n.__disposer = _vueI18n.__extender(this.$i18n);
  1491. }
  1492. }
  1493. }
  1494. else if (options.__i18n) {
  1495. if (this === this.$root) {
  1496. // merge option and gttach global
  1497. this.$i18n = mergeToGlobal(vuei18n, options);
  1498. }
  1499. else {
  1500. // atttach local VueI18n instance
  1501. this.$i18n = createVueI18n({
  1502. __i18n: options.__i18n,
  1503. __injectWithOption: true,
  1504. __extender: i18n.__vueI18nExtend,
  1505. __root: composer
  1506. });
  1507. // extend VueI18n instance
  1508. const _vueI18n = this.$i18n;
  1509. if (_vueI18n.__extender) {
  1510. _vueI18n.__disposer = _vueI18n.__extender(this.$i18n);
  1511. }
  1512. }
  1513. }
  1514. else {
  1515. // attach global VueI18n instance
  1516. this.$i18n = vuei18n;
  1517. }
  1518. if (options.__i18nGlobal) {
  1519. adjustI18nResources(composer, options, options);
  1520. }
  1521. // defines vue-i18n legacy APIs
  1522. this.$t = (...args) => this.$i18n.t(...args);
  1523. this.$rt = (...args) => this.$i18n.rt(...args);
  1524. this.$tc = (...args) => this.$i18n.tc(...args);
  1525. this.$te = (key, locale) => this.$i18n.te(key, locale);
  1526. this.$d = (...args) => this.$i18n.d(...args);
  1527. this.$n = (...args) => this.$i18n.n(...args);
  1528. this.$tm = (key) => this.$i18n.tm(key);
  1529. i18n.__setInstance(instance, this.$i18n);
  1530. },
  1531. mounted() {
  1532. },
  1533. unmounted() {
  1534. const instance = vue.getCurrentInstance();
  1535. /* istanbul ignore if */
  1536. if (!instance) {
  1537. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  1538. }
  1539. const _vueI18n = this.$i18n;
  1540. delete this.$t;
  1541. delete this.$rt;
  1542. delete this.$tc;
  1543. delete this.$te;
  1544. delete this.$d;
  1545. delete this.$n;
  1546. delete this.$tm;
  1547. if (_vueI18n.__disposer) {
  1548. _vueI18n.__disposer();
  1549. delete _vueI18n.__disposer;
  1550. delete _vueI18n.__extender;
  1551. }
  1552. i18n.__deleteInstance(instance);
  1553. delete this.$i18n;
  1554. }
  1555. };
  1556. }
  1557. function mergeToGlobal(g, options) {
  1558. g.locale = options.locale || g.locale;
  1559. g.fallbackLocale = options.fallbackLocale || g.fallbackLocale;
  1560. g.missing = options.missing || g.missing;
  1561. g.silentTranslationWarn =
  1562. options.silentTranslationWarn || g.silentFallbackWarn;
  1563. g.silentFallbackWarn = options.silentFallbackWarn || g.silentFallbackWarn;
  1564. g.formatFallbackMessages =
  1565. options.formatFallbackMessages || g.formatFallbackMessages;
  1566. g.postTranslation = options.postTranslation || g.postTranslation;
  1567. g.warnHtmlInMessage = options.warnHtmlInMessage || g.warnHtmlInMessage;
  1568. g.escapeParameterHtml = options.escapeParameterHtml || g.escapeParameterHtml;
  1569. g.sync = options.sync || g.sync;
  1570. g.__composer[SetPluralRulesSymbol](options.pluralizationRules || g.pluralizationRules);
  1571. const messages = getLocaleMessages(g.locale, {
  1572. messages: options.messages,
  1573. __i18n: options.__i18n
  1574. });
  1575. Object.keys(messages).forEach(locale => g.mergeLocaleMessage(locale, messages[locale]));
  1576. if (options.datetimeFormats) {
  1577. Object.keys(options.datetimeFormats).forEach(locale => g.mergeDateTimeFormat(locale, options.datetimeFormats[locale]));
  1578. }
  1579. if (options.numberFormats) {
  1580. Object.keys(options.numberFormats).forEach(locale => g.mergeNumberFormat(locale, options.numberFormats[locale]));
  1581. }
  1582. return g;
  1583. }
  1584. /**
  1585. * Injection key for {@link useI18n}
  1586. *
  1587. * @remarks
  1588. * The global injection key for I18n instances with `useI18n`. this injection key is used in Web Components.
  1589. * Specify the i18n instance created by {@link createI18n} together with `provide` function.
  1590. *
  1591. * @VueI18nGeneral
  1592. */
  1593. const I18nInjectionKey =
  1594. /* #__PURE__*/ shared.makeSymbol('global-vue-i18n');
  1595. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
  1596. function createI18n(options = {}, VueI18nLegacy) {
  1597. // prettier-ignore
  1598. const __legacyMode = shared.isBoolean(options.legacy)
  1599. ? options.legacy
  1600. : true;
  1601. // prettier-ignore
  1602. const __globalInjection = shared.isBoolean(options.globalInjection)
  1603. ? options.globalInjection
  1604. : true;
  1605. // prettier-ignore
  1606. const __allowComposition = __legacyMode
  1607. ? !!options.allowComposition
  1608. : true;
  1609. const __instances = new Map();
  1610. const [globalScope, __global] = createGlobal(options, __legacyMode);
  1611. const symbol = /* #__PURE__*/ shared.makeSymbol('');
  1612. function __getInstance(component) {
  1613. return __instances.get(component) || null;
  1614. }
  1615. function __setInstance(component, instance) {
  1616. __instances.set(component, instance);
  1617. }
  1618. function __deleteInstance(component) {
  1619. __instances.delete(component);
  1620. }
  1621. {
  1622. const i18n = {
  1623. // mode
  1624. get mode() {
  1625. return __legacyMode
  1626. ? 'legacy'
  1627. : 'composition';
  1628. },
  1629. // allowComposition
  1630. get allowComposition() {
  1631. return __allowComposition;
  1632. },
  1633. // install plugin
  1634. async install(app, ...options) {
  1635. // setup global provider
  1636. app.__VUE_I18N_SYMBOL__ = symbol;
  1637. app.provide(app.__VUE_I18N_SYMBOL__, i18n);
  1638. // set composer & vuei18n extend hook options from plugin options
  1639. if (shared.isPlainObject(options[0])) {
  1640. const opts = options[0];
  1641. i18n.__composerExtend =
  1642. opts.__composerExtend;
  1643. i18n.__vueI18nExtend =
  1644. opts.__vueI18nExtend;
  1645. }
  1646. // global method and properties injection for Composition API
  1647. let globalReleaseHandler = null;
  1648. if (!__legacyMode && __globalInjection) {
  1649. globalReleaseHandler = injectGlobalFields(app, i18n.global);
  1650. }
  1651. // install built-in components and directive
  1652. {
  1653. apply(app, i18n, ...options);
  1654. }
  1655. // setup mixin for Legacy API
  1656. if (__legacyMode) {
  1657. app.mixin(defineMixin(__global, __global.__composer, i18n));
  1658. }
  1659. // release global scope
  1660. const unmountApp = app.unmount;
  1661. app.unmount = () => {
  1662. globalReleaseHandler && globalReleaseHandler();
  1663. i18n.dispose();
  1664. unmountApp();
  1665. };
  1666. },
  1667. // global accessor
  1668. get global() {
  1669. return __global;
  1670. },
  1671. dispose() {
  1672. globalScope.stop();
  1673. },
  1674. // @internal
  1675. __instances,
  1676. // @internal
  1677. __getInstance,
  1678. // @internal
  1679. __setInstance,
  1680. // @internal
  1681. __deleteInstance
  1682. };
  1683. return i18n;
  1684. }
  1685. }
  1686. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  1687. function useI18n(options = {}) {
  1688. const instance = vue.getCurrentInstance();
  1689. if (instance == null) {
  1690. throw createI18nError(I18nErrorCodes.MUST_BE_CALL_SETUP_TOP);
  1691. }
  1692. if (!instance.isCE &&
  1693. instance.appContext.app != null &&
  1694. !instance.appContext.app.__VUE_I18N_SYMBOL__) {
  1695. throw createI18nError(I18nErrorCodes.NOT_INSTALLED);
  1696. }
  1697. const i18n = getI18nInstance(instance);
  1698. const gl = getGlobalComposer(i18n);
  1699. const componentOptions = getComponentOptions(instance);
  1700. const scope = getScope(options, componentOptions);
  1701. {
  1702. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1703. if (i18n.mode === 'legacy' && !options.__useComponent) {
  1704. if (!i18n.allowComposition) {
  1705. throw createI18nError(I18nErrorCodes.NOT_AVAILABLE_IN_LEGACY_MODE);
  1706. }
  1707. return useI18nForLegacy(instance, scope, gl, options);
  1708. }
  1709. }
  1710. if (scope === 'global') {
  1711. adjustI18nResources(gl, options, componentOptions);
  1712. return gl;
  1713. }
  1714. if (scope === 'parent') {
  1715. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1716. let composer = getComposer(i18n, instance, options.__useComponent);
  1717. if (composer == null) {
  1718. composer = gl;
  1719. }
  1720. return composer;
  1721. }
  1722. const i18nInternal = i18n;
  1723. let composer = i18nInternal.__getInstance(instance);
  1724. if (composer == null) {
  1725. const composerOptions = shared.assign({}, options);
  1726. if ('__i18n' in componentOptions) {
  1727. composerOptions.__i18n = componentOptions.__i18n;
  1728. }
  1729. if (gl) {
  1730. composerOptions.__root = gl;
  1731. }
  1732. composer = createComposer(composerOptions);
  1733. if (i18nInternal.__composerExtend) {
  1734. composer[DisposeSymbol] =
  1735. i18nInternal.__composerExtend(composer);
  1736. }
  1737. setupLifeCycle(i18nInternal, instance, composer);
  1738. i18nInternal.__setInstance(instance, composer);
  1739. }
  1740. return composer;
  1741. }
  1742. /**
  1743. * Cast to VueI18n legacy compatible type
  1744. *
  1745. * @remarks
  1746. * This API is provided only with [vue-i18n-bridge](https://vue-i18n.intlify.dev/guide/migration/ways.html#what-is-vue-i18n-bridge).
  1747. *
  1748. * The purpose of this function is to convert an {@link I18n} instance created with {@link createI18n | createI18n(legacy: true)} into a `vue-i18n@v8.x` compatible instance of `new VueI18n` in a TypeScript environment.
  1749. *
  1750. * @param i18n - An instance of {@link I18n}
  1751. * @returns A i18n instance which is casted to {@link VueI18n} type
  1752. *
  1753. * @VueI18nTip
  1754. * :new: provided by **vue-i18n-bridge only**
  1755. *
  1756. * @VueI18nGeneral
  1757. */
  1758. const castToVueI18n = /* #__PURE__*/ (i18n
  1759. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1760. ) => {
  1761. if (!(__VUE_I18N_BRIDGE__ in i18n)) {
  1762. throw createI18nError(I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N);
  1763. }
  1764. return i18n;
  1765. };
  1766. function createGlobal(options, legacyMode, VueI18nLegacy // eslint-disable-line @typescript-eslint/no-explicit-any
  1767. ) {
  1768. const scope = vue.effectScope();
  1769. {
  1770. const obj = legacyMode
  1771. ? scope.run(() => createVueI18n(options))
  1772. : scope.run(() => createComposer(options));
  1773. if (obj == null) {
  1774. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  1775. }
  1776. return [scope, obj];
  1777. }
  1778. }
  1779. function getI18nInstance(instance) {
  1780. {
  1781. const i18n = vue.inject(!instance.isCE
  1782. ? instance.appContext.app.__VUE_I18N_SYMBOL__
  1783. : I18nInjectionKey);
  1784. /* istanbul ignore if */
  1785. if (!i18n) {
  1786. throw createI18nError(!instance.isCE
  1787. ? I18nErrorCodes.UNEXPECTED_ERROR
  1788. : I18nErrorCodes.NOT_INSTALLED_WITH_PROVIDE);
  1789. }
  1790. return i18n;
  1791. }
  1792. }
  1793. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1794. function getScope(options, componentOptions) {
  1795. // prettier-ignore
  1796. return shared.isEmptyObject(options)
  1797. ? ('__i18n' in componentOptions)
  1798. ? 'local'
  1799. : 'global'
  1800. : !options.useScope
  1801. ? 'local'
  1802. : options.useScope;
  1803. }
  1804. function getGlobalComposer(i18n) {
  1805. // prettier-ignore
  1806. return i18n.mode === 'composition'
  1807. ? i18n.global
  1808. : i18n.global.__composer
  1809. ;
  1810. }
  1811. function getComposer(i18n, target, useComponent = false) {
  1812. let composer = null;
  1813. const root = target.root;
  1814. let current = getParentComponentInstance(target, useComponent);
  1815. while (current != null) {
  1816. const i18nInternal = i18n;
  1817. if (i18n.mode === 'composition') {
  1818. composer = i18nInternal.__getInstance(current);
  1819. }
  1820. else {
  1821. {
  1822. const vueI18n = i18nInternal.__getInstance(current);
  1823. if (vueI18n != null) {
  1824. composer = vueI18n
  1825. .__composer;
  1826. if (useComponent &&
  1827. composer &&
  1828. !composer[InejctWithOptionSymbol] // eslint-disable-line @typescript-eslint/no-explicit-any
  1829. ) {
  1830. composer = null;
  1831. }
  1832. }
  1833. }
  1834. }
  1835. if (composer != null) {
  1836. break;
  1837. }
  1838. if (root === current) {
  1839. break;
  1840. }
  1841. current = current.parent;
  1842. }
  1843. return composer;
  1844. }
  1845. function getParentComponentInstance(target, useComponent = false) {
  1846. if (target == null) {
  1847. return null;
  1848. }
  1849. {
  1850. // if `useComponent: true` will be specified, we get lexical scope owner instance for use-case slots
  1851. return !useComponent
  1852. ? target.parent
  1853. : target.vnode.ctx || target.parent; // eslint-disable-line @typescript-eslint/no-explicit-any
  1854. }
  1855. }
  1856. function setupLifeCycle(i18n, target, composer) {
  1857. {
  1858. vue.onMounted(() => {
  1859. }, target);
  1860. vue.onUnmounted(() => {
  1861. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1862. const _composer = composer;
  1863. i18n.__deleteInstance(target);
  1864. // dispose extended resources
  1865. const dispose = _composer[DisposeSymbol];
  1866. if (dispose) {
  1867. dispose();
  1868. delete _composer[DisposeSymbol];
  1869. }
  1870. }, target);
  1871. }
  1872. }
  1873. function useI18nForLegacy(instance, scope, root, options = {} // eslint-disable-line @typescript-eslint/no-explicit-any
  1874. ) {
  1875. const isLocalScope = scope === 'local';
  1876. const _composer = vue.shallowRef(null);
  1877. if (isLocalScope &&
  1878. instance.proxy &&
  1879. !(instance.proxy.$options.i18n || instance.proxy.$options.__i18n)) {
  1880. throw createI18nError(I18nErrorCodes.MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION);
  1881. }
  1882. const _inheritLocale = shared.isBoolean(options.inheritLocale)
  1883. ? options.inheritLocale
  1884. : !shared.isString(options.locale);
  1885. const _locale = vue.ref(
  1886. // prettier-ignore
  1887. !isLocalScope || _inheritLocale
  1888. ? root.locale.value
  1889. : shared.isString(options.locale)
  1890. ? options.locale
  1891. : coreBase.DEFAULT_LOCALE);
  1892. const _fallbackLocale = vue.ref(
  1893. // prettier-ignore
  1894. !isLocalScope || _inheritLocale
  1895. ? root.fallbackLocale.value
  1896. : shared.isString(options.fallbackLocale) ||
  1897. shared.isArray(options.fallbackLocale) ||
  1898. shared.isPlainObject(options.fallbackLocale) ||
  1899. options.fallbackLocale === false
  1900. ? options.fallbackLocale
  1901. : _locale.value);
  1902. const _messages = vue.ref(getLocaleMessages(_locale.value, options));
  1903. // prettier-ignore
  1904. const _datetimeFormats = vue.ref(shared.isPlainObject(options.datetimeFormats)
  1905. ? options.datetimeFormats
  1906. : { [_locale.value]: {} });
  1907. // prettier-ignore
  1908. const _numberFormats = vue.ref(shared.isPlainObject(options.numberFormats)
  1909. ? options.numberFormats
  1910. : { [_locale.value]: {} });
  1911. // prettier-ignore
  1912. const _missingWarn = isLocalScope
  1913. ? root.missingWarn
  1914. : shared.isBoolean(options.missingWarn) || shared.isRegExp(options.missingWarn)
  1915. ? options.missingWarn
  1916. : true;
  1917. // prettier-ignore
  1918. const _fallbackWarn = isLocalScope
  1919. ? root.fallbackWarn
  1920. : shared.isBoolean(options.fallbackWarn) || shared.isRegExp(options.fallbackWarn)
  1921. ? options.fallbackWarn
  1922. : true;
  1923. // prettier-ignore
  1924. const _fallbackRoot = isLocalScope
  1925. ? root.fallbackRoot
  1926. : shared.isBoolean(options.fallbackRoot)
  1927. ? options.fallbackRoot
  1928. : true;
  1929. // configure fall back to root
  1930. const _fallbackFormat = !!options.fallbackFormat;
  1931. // runtime missing
  1932. const _missing = shared.isFunction(options.missing) ? options.missing : null;
  1933. // postTranslation handler
  1934. const _postTranslation = shared.isFunction(options.postTranslation)
  1935. ? options.postTranslation
  1936. : null;
  1937. // prettier-ignore
  1938. const _warnHtmlMessage = isLocalScope
  1939. ? root.warnHtmlMessage
  1940. : shared.isBoolean(options.warnHtmlMessage)
  1941. ? options.warnHtmlMessage
  1942. : true;
  1943. const _escapeParameter = !!options.escapeParameter;
  1944. // prettier-ignore
  1945. const _modifiers = isLocalScope
  1946. ? root.modifiers
  1947. : shared.isPlainObject(options.modifiers)
  1948. ? options.modifiers
  1949. : {};
  1950. // pluralRules
  1951. const _pluralRules = options.pluralRules || (isLocalScope && root.pluralRules);
  1952. // track reactivity
  1953. function trackReactivityValues() {
  1954. return [
  1955. _locale.value,
  1956. _fallbackLocale.value,
  1957. _messages.value,
  1958. _datetimeFormats.value,
  1959. _numberFormats.value
  1960. ];
  1961. }
  1962. // locale
  1963. const locale = vue.computed({
  1964. get: () => {
  1965. return _composer.value ? _composer.value.locale.value : _locale.value;
  1966. },
  1967. set: val => {
  1968. if (_composer.value) {
  1969. _composer.value.locale.value = val;
  1970. }
  1971. _locale.value = val;
  1972. }
  1973. });
  1974. // fallbackLocale
  1975. const fallbackLocale = vue.computed({
  1976. get: () => {
  1977. return _composer.value
  1978. ? _composer.value.fallbackLocale.value
  1979. : _fallbackLocale.value;
  1980. },
  1981. set: val => {
  1982. if (_composer.value) {
  1983. _composer.value.fallbackLocale.value = val;
  1984. }
  1985. _fallbackLocale.value = val;
  1986. }
  1987. });
  1988. // messages
  1989. const messages = vue.computed(() => {
  1990. if (_composer.value) {
  1991. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1992. return _composer.value.messages.value;
  1993. }
  1994. else {
  1995. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1996. return _messages.value;
  1997. }
  1998. });
  1999. const datetimeFormats = vue.computed(() => _datetimeFormats.value);
  2000. const numberFormats = vue.computed(() => _numberFormats.value);
  2001. function getPostTranslationHandler() {
  2002. return _composer.value
  2003. ? _composer.value.getPostTranslationHandler()
  2004. : _postTranslation;
  2005. }
  2006. function setPostTranslationHandler(handler) {
  2007. if (_composer.value) {
  2008. _composer.value.setPostTranslationHandler(handler);
  2009. }
  2010. }
  2011. function getMissingHandler() {
  2012. return _composer.value ? _composer.value.getMissingHandler() : _missing;
  2013. }
  2014. function setMissingHandler(handler) {
  2015. if (_composer.value) {
  2016. _composer.value.setMissingHandler(handler);
  2017. }
  2018. }
  2019. function warpWithDeps(fn) {
  2020. trackReactivityValues();
  2021. return fn();
  2022. }
  2023. function t(...args) {
  2024. return _composer.value
  2025. ? warpWithDeps(() => Reflect.apply(_composer.value.t, null, [...args]))
  2026. : warpWithDeps(() => '');
  2027. }
  2028. function rt(...args) {
  2029. return _composer.value
  2030. ? Reflect.apply(_composer.value.rt, null, [...args])
  2031. : '';
  2032. }
  2033. function d(...args) {
  2034. return _composer.value
  2035. ? warpWithDeps(() => Reflect.apply(_composer.value.d, null, [...args]))
  2036. : warpWithDeps(() => '');
  2037. }
  2038. function n(...args) {
  2039. return _composer.value
  2040. ? warpWithDeps(() => Reflect.apply(_composer.value.n, null, [...args]))
  2041. : warpWithDeps(() => '');
  2042. }
  2043. function tm(key) {
  2044. return _composer.value ? _composer.value.tm(key) : {};
  2045. }
  2046. function te(key, locale) {
  2047. return _composer.value ? _composer.value.te(key, locale) : false;
  2048. }
  2049. function getLocaleMessage(locale) {
  2050. return _composer.value ? _composer.value.getLocaleMessage(locale) : {};
  2051. }
  2052. function setLocaleMessage(locale, message) {
  2053. if (_composer.value) {
  2054. _composer.value.setLocaleMessage(locale, message);
  2055. _messages.value[locale] = message;
  2056. }
  2057. }
  2058. function mergeLocaleMessage(locale, message) {
  2059. if (_composer.value) {
  2060. _composer.value.mergeLocaleMessage(locale, message);
  2061. }
  2062. }
  2063. function getDateTimeFormat(locale) {
  2064. return _composer.value ? _composer.value.getDateTimeFormat(locale) : {};
  2065. }
  2066. function setDateTimeFormat(locale, format) {
  2067. if (_composer.value) {
  2068. _composer.value.setDateTimeFormat(locale, format);
  2069. _datetimeFormats.value[locale] = format;
  2070. }
  2071. }
  2072. function mergeDateTimeFormat(locale, format) {
  2073. if (_composer.value) {
  2074. _composer.value.mergeDateTimeFormat(locale, format);
  2075. }
  2076. }
  2077. function getNumberFormat(locale) {
  2078. return _composer.value ? _composer.value.getNumberFormat(locale) : {};
  2079. }
  2080. function setNumberFormat(locale, format) {
  2081. if (_composer.value) {
  2082. _composer.value.setNumberFormat(locale, format);
  2083. _numberFormats.value[locale] = format;
  2084. }
  2085. }
  2086. function mergeNumberFormat(locale, format) {
  2087. if (_composer.value) {
  2088. _composer.value.mergeNumberFormat(locale, format);
  2089. }
  2090. }
  2091. const wrapper = {
  2092. get id() {
  2093. return _composer.value ? _composer.value.id : -1;
  2094. },
  2095. locale,
  2096. fallbackLocale,
  2097. messages,
  2098. datetimeFormats,
  2099. numberFormats,
  2100. get inheritLocale() {
  2101. return _composer.value ? _composer.value.inheritLocale : _inheritLocale;
  2102. },
  2103. set inheritLocale(val) {
  2104. if (_composer.value) {
  2105. _composer.value.inheritLocale = val;
  2106. }
  2107. },
  2108. get availableLocales() {
  2109. return _composer.value
  2110. ? _composer.value.availableLocales
  2111. : Object.keys(_messages.value);
  2112. },
  2113. get modifiers() {
  2114. return (_composer.value ? _composer.value.modifiers : _modifiers);
  2115. },
  2116. get pluralRules() {
  2117. return (_composer.value ? _composer.value.pluralRules : _pluralRules);
  2118. },
  2119. get isGlobal() {
  2120. return _composer.value ? _composer.value.isGlobal : false;
  2121. },
  2122. get missingWarn() {
  2123. return _composer.value ? _composer.value.missingWarn : _missingWarn;
  2124. },
  2125. set missingWarn(val) {
  2126. if (_composer.value) {
  2127. _composer.value.missingWarn = val;
  2128. }
  2129. },
  2130. get fallbackWarn() {
  2131. return _composer.value ? _composer.value.fallbackWarn : _fallbackWarn;
  2132. },
  2133. set fallbackWarn(val) {
  2134. if (_composer.value) {
  2135. _composer.value.missingWarn = val;
  2136. }
  2137. },
  2138. get fallbackRoot() {
  2139. return _composer.value ? _composer.value.fallbackRoot : _fallbackRoot;
  2140. },
  2141. set fallbackRoot(val) {
  2142. if (_composer.value) {
  2143. _composer.value.fallbackRoot = val;
  2144. }
  2145. },
  2146. get fallbackFormat() {
  2147. return _composer.value ? _composer.value.fallbackFormat : _fallbackFormat;
  2148. },
  2149. set fallbackFormat(val) {
  2150. if (_composer.value) {
  2151. _composer.value.fallbackFormat = val;
  2152. }
  2153. },
  2154. get warnHtmlMessage() {
  2155. return _composer.value
  2156. ? _composer.value.warnHtmlMessage
  2157. : _warnHtmlMessage;
  2158. },
  2159. set warnHtmlMessage(val) {
  2160. if (_composer.value) {
  2161. _composer.value.warnHtmlMessage = val;
  2162. }
  2163. },
  2164. get escapeParameter() {
  2165. return _composer.value
  2166. ? _composer.value.escapeParameter
  2167. : _escapeParameter;
  2168. },
  2169. set escapeParameter(val) {
  2170. if (_composer.value) {
  2171. _composer.value.escapeParameter = val;
  2172. }
  2173. },
  2174. t,
  2175. getPostTranslationHandler,
  2176. setPostTranslationHandler,
  2177. getMissingHandler,
  2178. setMissingHandler,
  2179. rt,
  2180. d,
  2181. n,
  2182. tm,
  2183. te,
  2184. getLocaleMessage,
  2185. setLocaleMessage,
  2186. mergeLocaleMessage,
  2187. getDateTimeFormat,
  2188. setDateTimeFormat,
  2189. mergeDateTimeFormat,
  2190. getNumberFormat,
  2191. setNumberFormat,
  2192. mergeNumberFormat
  2193. };
  2194. function sync(composer) {
  2195. composer.locale.value = _locale.value;
  2196. composer.fallbackLocale.value = _fallbackLocale.value;
  2197. Object.keys(_messages.value).forEach(locale => {
  2198. composer.mergeLocaleMessage(locale, _messages.value[locale]);
  2199. });
  2200. Object.keys(_datetimeFormats.value).forEach(locale => {
  2201. composer.mergeDateTimeFormat(locale, _datetimeFormats.value[locale]);
  2202. });
  2203. Object.keys(_numberFormats.value).forEach(locale => {
  2204. composer.mergeNumberFormat(locale, _numberFormats.value[locale]);
  2205. });
  2206. composer.escapeParameter = _escapeParameter;
  2207. composer.fallbackFormat = _fallbackFormat;
  2208. composer.fallbackRoot = _fallbackRoot;
  2209. composer.fallbackWarn = _fallbackWarn;
  2210. composer.missingWarn = _missingWarn;
  2211. composer.warnHtmlMessage = _warnHtmlMessage;
  2212. }
  2213. vue.onBeforeMount(() => {
  2214. if (instance.proxy == null || instance.proxy.$i18n == null) {
  2215. throw createI18nError(I18nErrorCodes.NOT_AVAILABLE_COMPOSITION_IN_LEGACY);
  2216. }
  2217. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2218. const composer = (_composer.value = instance.proxy.$i18n
  2219. .__composer);
  2220. if (scope === 'global') {
  2221. _locale.value = composer.locale.value;
  2222. _fallbackLocale.value = composer.fallbackLocale.value;
  2223. _messages.value = composer.messages.value;
  2224. _datetimeFormats.value = composer.datetimeFormats.value;
  2225. _numberFormats.value = composer.numberFormats.value;
  2226. }
  2227. else if (isLocalScope) {
  2228. sync(composer);
  2229. }
  2230. });
  2231. return wrapper;
  2232. }
  2233. const globalExportProps = [
  2234. 'locale',
  2235. 'fallbackLocale',
  2236. 'availableLocales'
  2237. ];
  2238. const globalExportMethods = ['t', 'rt', 'd', 'n', 'tm', 'te']
  2239. ;
  2240. function injectGlobalFields(app, composer) {
  2241. const i18n = Object.create(null);
  2242. globalExportProps.forEach(prop => {
  2243. const desc = Object.getOwnPropertyDescriptor(composer, prop);
  2244. if (!desc) {
  2245. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  2246. }
  2247. const wrap = vue.isRef(desc.value) // check computed props
  2248. ? {
  2249. get() {
  2250. return desc.value.value;
  2251. },
  2252. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2253. set(val) {
  2254. desc.value.value = val;
  2255. }
  2256. }
  2257. : {
  2258. get() {
  2259. return desc.get && desc.get();
  2260. }
  2261. };
  2262. Object.defineProperty(i18n, prop, wrap);
  2263. });
  2264. app.config.globalProperties.$i18n = i18n;
  2265. globalExportMethods.forEach(method => {
  2266. const desc = Object.getOwnPropertyDescriptor(composer, method);
  2267. if (!desc || !desc.value) {
  2268. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  2269. }
  2270. Object.defineProperty(app.config.globalProperties, `$${method}`, desc);
  2271. });
  2272. const dispose = () => {
  2273. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2274. delete app.config.globalProperties.$i18n;
  2275. globalExportMethods.forEach(method => {
  2276. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2277. delete app.config.globalProperties[`$${method}`];
  2278. });
  2279. };
  2280. return dispose;
  2281. }
  2282. // register message compiler at vue-i18n
  2283. {
  2284. coreBase.registerMessageCompiler(coreBase.compile);
  2285. }
  2286. // register message resolver at vue-i18n
  2287. coreBase.registerMessageResolver(coreBase.resolveValue);
  2288. // register fallback locale at vue-i18n
  2289. coreBase.registerLocaleFallbacker(coreBase.fallbackWithLocaleChain);
  2290. exports.DatetimeFormat = DatetimeFormat;
  2291. exports.I18nD = I18nD;
  2292. exports.I18nInjectionKey = I18nInjectionKey;
  2293. exports.I18nN = I18nN;
  2294. exports.I18nT = I18nT;
  2295. exports.NumberFormat = NumberFormat;
  2296. exports.Translation = Translation;
  2297. exports.VERSION = VERSION;
  2298. exports.castToVueI18n = castToVueI18n;
  2299. exports.createI18n = createI18n;
  2300. exports.useI18n = useI18n;
  2301. exports.vTDirective = vTDirective;