vue-i18n.runtime.mjs 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941
  1. /*!
  2. * vue-i18n v9.8.0
  3. * (c) 2023 kazuya kawaguchi
  4. * Released under the MIT License.
  5. */
  6. import { getGlobalThis, incrementer, format, makeSymbol, isPlainObject, isArray, deepCopy, isString, hasOwn, isObject, warn, isBoolean, isRegExp, isFunction, inBrowser, assign, isNumber, createEmitter, isEmptyObject } from '@intlify/shared';
  7. import { CoreWarnCodes, CoreErrorCodes, createCompileError, DEFAULT_LOCALE, updateFallbackLocale, setFallbackContext, createCoreContext, clearDateTimeFormat, clearNumberFormat, setAdditionalMeta, getFallbackContext, NOT_REOSLVED, isTranslateFallbackWarn, isTranslateMissingWarn, parseTranslateArgs, translate, MISSING_RESOLVE_VALUE, parseDateTimeArgs, datetime, parseNumberArgs, number, isMessageAST, isMessageFunction, fallbackWithLocaleChain, NUMBER_FORMAT_OPTIONS_KEYS, DATETIME_FORMAT_OPTIONS_KEYS, registerMessageCompiler, compile, registerMessageResolver, resolveValue, registerLocaleFallbacker, setDevToolsHook } from '@intlify/core-base';
  8. import { createVNode, Text, ref, computed, watch, getCurrentInstance, Fragment, defineComponent, h, effectScope, inject, onMounted, onUnmounted, shallowRef, onBeforeMount, isRef } from 'vue';
  9. import { setupDevtoolsPlugin } from '@vue/devtools-api';
  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.8.0';
  19. /**
  20. * This is only called in esm-bundler builds.
  21. * istanbul-ignore-next
  22. */
  23. function initFeatureFlags() {
  24. if (typeof __VUE_I18N_FULL_INSTALL__ !== 'boolean') {
  25. getGlobalThis().__VUE_I18N_FULL_INSTALL__ = true;
  26. }
  27. if (typeof __VUE_I18N_LEGACY_API__ !== 'boolean') {
  28. getGlobalThis().__VUE_I18N_LEGACY_API__ = true;
  29. }
  30. if (typeof __INTLIFY_JIT_COMPILATION__ !== 'boolean') {
  31. getGlobalThis().__INTLIFY_JIT_COMPILATION__ = false;
  32. }
  33. if (typeof __INTLIFY_DROP_MESSAGE_COMPILER__ !== 'boolean') {
  34. getGlobalThis().__INTLIFY_DROP_MESSAGE_COMPILER__ = false;
  35. }
  36. if (typeof __INTLIFY_PROD_DEVTOOLS__ !== 'boolean') {
  37. getGlobalThis().__INTLIFY_PROD_DEVTOOLS__ = false;
  38. }
  39. }
  40. const code$1 = CoreWarnCodes.__EXTEND_POINT__;
  41. const inc$1 = incrementer(code$1);
  42. const I18nWarnCodes = {
  43. FALLBACK_TO_ROOT: code$1,
  44. NOT_SUPPORTED_PRESERVE: inc$1(),
  45. NOT_SUPPORTED_FORMATTER: inc$1(),
  46. NOT_SUPPORTED_PRESERVE_DIRECTIVE: inc$1(),
  47. NOT_SUPPORTED_GET_CHOICE_INDEX: inc$1(),
  48. COMPONENT_NAME_LEGACY_COMPATIBLE: inc$1(),
  49. NOT_FOUND_PARENT_SCOPE: inc$1(),
  50. IGNORE_OBJ_FLATTEN: inc$1(),
  51. NOTICE_DROP_ALLOW_COMPOSITION: inc$1() // 17
  52. };
  53. const warnMessages = {
  54. [I18nWarnCodes.FALLBACK_TO_ROOT]: `Fall back to {type} '{key}' with root locale.`,
  55. [I18nWarnCodes.NOT_SUPPORTED_PRESERVE]: `Not supported 'preserve'.`,
  56. [I18nWarnCodes.NOT_SUPPORTED_FORMATTER]: `Not supported 'formatter'.`,
  57. [I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE]: `Not supported 'preserveDirectiveContent'.`,
  58. [I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX]: `Not supported 'getChoiceIndex'.`,
  59. [I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE]: `Component name legacy compatible: '{name}' -> 'i18n'`,
  60. [I18nWarnCodes.NOT_FOUND_PARENT_SCOPE]: `Not found parent scope. use the global scope.`,
  61. [I18nWarnCodes.IGNORE_OBJ_FLATTEN]: `Ignore object flatten: '{key}' key has an string value`,
  62. [I18nWarnCodes.NOTICE_DROP_ALLOW_COMPOSITION]: `'allowComposition' option will be dropped in the next major version. For more information, please see 👉 https://tinyurl.com/2p97mcze`
  63. };
  64. function getWarnMessage(code, ...args) {
  65. return format(warnMessages[code], ...args);
  66. }
  67. const code = CoreErrorCodes.__EXTEND_POINT__;
  68. const inc = incrementer(code);
  69. const I18nErrorCodes = {
  70. // composer module errors
  71. UNEXPECTED_RETURN_TYPE: code,
  72. // legacy module errors
  73. INVALID_ARGUMENT: inc(),
  74. // i18n module errors
  75. MUST_BE_CALL_SETUP_TOP: inc(),
  76. NOT_INSTALLED: inc(),
  77. NOT_AVAILABLE_IN_LEGACY_MODE: inc(),
  78. // directive module errors
  79. REQUIRED_VALUE: inc(),
  80. INVALID_VALUE: inc(),
  81. // vue-devtools errors
  82. CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN: inc(),
  83. NOT_INSTALLED_WITH_PROVIDE: inc(),
  84. // unexpected error
  85. UNEXPECTED_ERROR: inc(),
  86. // not compatible legacy vue-i18n constructor
  87. NOT_COMPATIBLE_LEGACY_VUE_I18N: inc(),
  88. // bridge support vue 2.x only
  89. BRIDGE_SUPPORT_VUE_2_ONLY: inc(),
  90. // need to define `i18n` option in `allowComposition: true` and `useScope: 'local' at `useI18n``
  91. MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION: inc(),
  92. // Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly
  93. NOT_AVAILABLE_COMPOSITION_IN_LEGACY: inc(),
  94. // for enhancement
  95. __EXTEND_POINT__: inc() // 40
  96. };
  97. function createI18nError(code, ...args) {
  98. return createCompileError(code, null, (process.env.NODE_ENV !== 'production') ? { messages: errorMessages, args } : undefined);
  99. }
  100. const errorMessages = {
  101. [I18nErrorCodes.UNEXPECTED_RETURN_TYPE]: 'Unexpected return type in composer',
  102. [I18nErrorCodes.INVALID_ARGUMENT]: 'Invalid argument',
  103. [I18nErrorCodes.MUST_BE_CALL_SETUP_TOP]: 'Must be called at the top of a `setup` function',
  104. [I18nErrorCodes.NOT_INSTALLED]: 'Need to install with `app.use` function',
  105. [I18nErrorCodes.UNEXPECTED_ERROR]: 'Unexpected error',
  106. [I18nErrorCodes.NOT_AVAILABLE_IN_LEGACY_MODE]: 'Not available in legacy mode',
  107. [I18nErrorCodes.REQUIRED_VALUE]: `Required in value: {0}`,
  108. [I18nErrorCodes.INVALID_VALUE]: `Invalid value`,
  109. [I18nErrorCodes.CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN]: `Cannot setup vue-devtools plugin`,
  110. [I18nErrorCodes.NOT_INSTALLED_WITH_PROVIDE]: 'Need to install with `provide` function',
  111. [I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N]: 'Not compatible legacy VueI18n.',
  112. [I18nErrorCodes.BRIDGE_SUPPORT_VUE_2_ONLY]: 'vue-i18n-bridge support Vue 2.x only',
  113. [I18nErrorCodes.MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION]: 'Must define ‘i18n’ option or custom block in Composition API with using local scope in Legacy API mode',
  114. [I18nErrorCodes.NOT_AVAILABLE_COMPOSITION_IN_LEGACY]: 'Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly'
  115. };
  116. const TranslateVNodeSymbol =
  117. /* #__PURE__*/ makeSymbol('__translateVNode');
  118. const DatetimePartsSymbol = /* #__PURE__*/ makeSymbol('__datetimeParts');
  119. const NumberPartsSymbol = /* #__PURE__*/ makeSymbol('__numberParts');
  120. const EnableEmitter = /* #__PURE__*/ makeSymbol('__enableEmitter');
  121. const DisableEmitter = /* #__PURE__*/ makeSymbol('__disableEmitter');
  122. const SetPluralRulesSymbol = makeSymbol('__setPluralRules');
  123. makeSymbol('__intlifyMeta');
  124. const InejctWithOptionSymbol =
  125. /* #__PURE__*/ makeSymbol('__injectWithOption');
  126. const DisposeSymbol = /* #__PURE__*/ makeSymbol('__dispose');
  127. const __VUE_I18N_BRIDGE__ = '__VUE_I18N_BRIDGE__';
  128. /* eslint-disable @typescript-eslint/no-explicit-any */
  129. /**
  130. * Transform flat json in obj to normal json in obj
  131. */
  132. function handleFlatJson(obj) {
  133. // check obj
  134. if (!isObject(obj)) {
  135. return obj;
  136. }
  137. for (const key in obj) {
  138. // check key
  139. if (!hasOwn(obj, key)) {
  140. continue;
  141. }
  142. // handle for normal json
  143. if (!key.includes('.')) {
  144. // recursive process value if value is also a object
  145. if (isObject(obj[key])) {
  146. handleFlatJson(obj[key]);
  147. }
  148. }
  149. // handle for flat json, transform to normal json
  150. else {
  151. // go to the last object
  152. const subKeys = key.split('.');
  153. const lastIndex = subKeys.length - 1;
  154. let currentObj = obj;
  155. let hasStringValue = false;
  156. for (let i = 0; i < lastIndex; i++) {
  157. if (!(subKeys[i] in currentObj)) {
  158. currentObj[subKeys[i]] = {};
  159. }
  160. if (!isObject(currentObj[subKeys[i]])) {
  161. (process.env.NODE_ENV !== 'production') &&
  162. warn(getWarnMessage(I18nWarnCodes.IGNORE_OBJ_FLATTEN, {
  163. key: subKeys[i]
  164. }));
  165. hasStringValue = true;
  166. break;
  167. }
  168. currentObj = currentObj[subKeys[i]];
  169. }
  170. // update last object value, delete old property
  171. if (!hasStringValue) {
  172. currentObj[subKeys[lastIndex]] = obj[key];
  173. delete obj[key];
  174. }
  175. // recursive process value if value is also a object
  176. if (isObject(currentObj[subKeys[lastIndex]])) {
  177. handleFlatJson(currentObj[subKeys[lastIndex]]);
  178. }
  179. }
  180. }
  181. return obj;
  182. }
  183. function getLocaleMessages(locale, options) {
  184. const { messages, __i18n, messageResolver, flatJson } = options;
  185. // prettier-ignore
  186. const ret = (isPlainObject(messages)
  187. ? messages
  188. : isArray(__i18n)
  189. ? {}
  190. : { [locale]: {} });
  191. // merge locale messages of i18n custom block
  192. if (isArray(__i18n)) {
  193. __i18n.forEach(custom => {
  194. if ('locale' in custom && 'resource' in custom) {
  195. const { locale, resource } = custom;
  196. if (locale) {
  197. ret[locale] = ret[locale] || {};
  198. deepCopy(resource, ret[locale]);
  199. }
  200. else {
  201. deepCopy(resource, ret);
  202. }
  203. }
  204. else {
  205. isString(custom) && deepCopy(JSON.parse(custom), ret);
  206. }
  207. });
  208. }
  209. // handle messages for flat json
  210. if (messageResolver == null && flatJson) {
  211. for (const key in ret) {
  212. if (hasOwn(ret, key)) {
  213. handleFlatJson(ret[key]);
  214. }
  215. }
  216. }
  217. return ret;
  218. }
  219. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  220. function getComponentOptions(instance) {
  221. return instance.type ;
  222. }
  223. function adjustI18nResources(gl, options, componentOptions // eslint-disable-line @typescript-eslint/no-explicit-any
  224. ) {
  225. let messages = isObject(options.messages) ? options.messages : {};
  226. if ('__i18nGlobal' in componentOptions) {
  227. messages = getLocaleMessages(gl.locale.value, {
  228. messages,
  229. __i18n: componentOptions.__i18nGlobal
  230. });
  231. }
  232. // merge locale messages
  233. const locales = Object.keys(messages);
  234. if (locales.length) {
  235. locales.forEach(locale => {
  236. gl.mergeLocaleMessage(locale, messages[locale]);
  237. });
  238. }
  239. {
  240. // merge datetime formats
  241. if (isObject(options.datetimeFormats)) {
  242. const locales = Object.keys(options.datetimeFormats);
  243. if (locales.length) {
  244. locales.forEach(locale => {
  245. gl.mergeDateTimeFormat(locale, options.datetimeFormats[locale]);
  246. });
  247. }
  248. }
  249. // merge number formats
  250. if (isObject(options.numberFormats)) {
  251. const locales = Object.keys(options.numberFormats);
  252. if (locales.length) {
  253. locales.forEach(locale => {
  254. gl.mergeNumberFormat(locale, options.numberFormats[locale]);
  255. });
  256. }
  257. }
  258. }
  259. }
  260. function createTextNode(key) {
  261. return createVNode(Text, null, key, 0)
  262. ;
  263. }
  264. /* eslint-enable @typescript-eslint/no-explicit-any */
  265. /* eslint-disable @typescript-eslint/no-explicit-any */
  266. // extend VNode interface
  267. const DEVTOOLS_META = '__INTLIFY_META__';
  268. const NOOP_RETURN_ARRAY = () => [];
  269. const NOOP_RETURN_FALSE = () => false;
  270. let composerID = 0;
  271. function defineCoreMissingHandler(missing) {
  272. return ((ctx, locale, key, type) => {
  273. return missing(locale, key, getCurrentInstance() || undefined, type);
  274. });
  275. }
  276. // for Intlify DevTools
  277. /* #__NO_SIDE_EFFECTS__ */
  278. const getMetaInfo = () => {
  279. const instance = getCurrentInstance();
  280. let meta = null; // eslint-disable-line @typescript-eslint/no-explicit-any
  281. return instance && (meta = getComponentOptions(instance)[DEVTOOLS_META])
  282. ? { [DEVTOOLS_META]: meta } // eslint-disable-line @typescript-eslint/no-explicit-any
  283. : null;
  284. };
  285. /**
  286. * Create composer interface factory
  287. *
  288. * @internal
  289. */
  290. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  291. function createComposer(options = {}, VueI18nLegacy) {
  292. const { __root, __injectWithOption } = options;
  293. const _isGlobal = __root === undefined;
  294. const flatJson = options.flatJson;
  295. let _inheritLocale = isBoolean(options.inheritLocale)
  296. ? options.inheritLocale
  297. : true;
  298. const _locale = ref(
  299. // prettier-ignore
  300. __root && _inheritLocale
  301. ? __root.locale.value
  302. : isString(options.locale)
  303. ? options.locale
  304. : DEFAULT_LOCALE);
  305. const _fallbackLocale = ref(
  306. // prettier-ignore
  307. __root && _inheritLocale
  308. ? __root.fallbackLocale.value
  309. : isString(options.fallbackLocale) ||
  310. isArray(options.fallbackLocale) ||
  311. isPlainObject(options.fallbackLocale) ||
  312. options.fallbackLocale === false
  313. ? options.fallbackLocale
  314. : _locale.value);
  315. const _messages = ref(getLocaleMessages(_locale.value, options));
  316. // prettier-ignore
  317. const _datetimeFormats = ref(isPlainObject(options.datetimeFormats)
  318. ? options.datetimeFormats
  319. : { [_locale.value]: {} })
  320. ;
  321. // prettier-ignore
  322. const _numberFormats = ref(isPlainObject(options.numberFormats)
  323. ? options.numberFormats
  324. : { [_locale.value]: {} })
  325. ;
  326. // warning suppress options
  327. // prettier-ignore
  328. let _missingWarn = __root
  329. ? __root.missingWarn
  330. : isBoolean(options.missingWarn) || isRegExp(options.missingWarn)
  331. ? options.missingWarn
  332. : true;
  333. // prettier-ignore
  334. let _fallbackWarn = __root
  335. ? __root.fallbackWarn
  336. : isBoolean(options.fallbackWarn) || isRegExp(options.fallbackWarn)
  337. ? options.fallbackWarn
  338. : true;
  339. // prettier-ignore
  340. let _fallbackRoot = __root
  341. ? __root.fallbackRoot
  342. : isBoolean(options.fallbackRoot)
  343. ? options.fallbackRoot
  344. : true;
  345. // configure fall back to root
  346. let _fallbackFormat = !!options.fallbackFormat;
  347. // runtime missing
  348. let _missing = isFunction(options.missing) ? options.missing : null;
  349. let _runtimeMissing = isFunction(options.missing)
  350. ? defineCoreMissingHandler(options.missing)
  351. : null;
  352. // postTranslation handler
  353. let _postTranslation = isFunction(options.postTranslation)
  354. ? options.postTranslation
  355. : null;
  356. // prettier-ignore
  357. let _warnHtmlMessage = __root
  358. ? __root.warnHtmlMessage
  359. : isBoolean(options.warnHtmlMessage)
  360. ? options.warnHtmlMessage
  361. : true;
  362. let _escapeParameter = !!options.escapeParameter;
  363. // custom linked modifiers
  364. // prettier-ignore
  365. const _modifiers = __root
  366. ? __root.modifiers
  367. : isPlainObject(options.modifiers)
  368. ? options.modifiers
  369. : {};
  370. // pluralRules
  371. let _pluralRules = options.pluralRules || (__root && __root.pluralRules);
  372. // runtime context
  373. // eslint-disable-next-line prefer-const
  374. let _context;
  375. const getCoreContext = () => {
  376. _isGlobal && setFallbackContext(null);
  377. const ctxOptions = {
  378. version: VERSION,
  379. locale: _locale.value,
  380. fallbackLocale: _fallbackLocale.value,
  381. messages: _messages.value,
  382. modifiers: _modifiers,
  383. pluralRules: _pluralRules,
  384. missing: _runtimeMissing === null ? undefined : _runtimeMissing,
  385. missingWarn: _missingWarn,
  386. fallbackWarn: _fallbackWarn,
  387. fallbackFormat: _fallbackFormat,
  388. unresolving: true,
  389. postTranslation: _postTranslation === null ? undefined : _postTranslation,
  390. warnHtmlMessage: _warnHtmlMessage,
  391. escapeParameter: _escapeParameter,
  392. messageResolver: options.messageResolver,
  393. messageCompiler: options.messageCompiler,
  394. __meta: { framework: 'vue' }
  395. };
  396. {
  397. ctxOptions.datetimeFormats = _datetimeFormats.value;
  398. ctxOptions.numberFormats = _numberFormats.value;
  399. ctxOptions.__datetimeFormatters = isPlainObject(_context)
  400. ? _context.__datetimeFormatters
  401. : undefined;
  402. ctxOptions.__numberFormatters = isPlainObject(_context)
  403. ? _context.__numberFormatters
  404. : undefined;
  405. }
  406. if ((process.env.NODE_ENV !== 'production')) {
  407. ctxOptions.__v_emitter = isPlainObject(_context)
  408. ? _context.__v_emitter
  409. : undefined;
  410. }
  411. const ctx = createCoreContext(ctxOptions);
  412. _isGlobal && setFallbackContext(ctx);
  413. return ctx;
  414. };
  415. _context = getCoreContext();
  416. updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  417. // track reactivity
  418. function trackReactivityValues() {
  419. return [
  420. _locale.value,
  421. _fallbackLocale.value,
  422. _messages.value,
  423. _datetimeFormats.value,
  424. _numberFormats.value
  425. ]
  426. ;
  427. }
  428. // locale
  429. const locale = computed({
  430. get: () => _locale.value,
  431. set: val => {
  432. _locale.value = val;
  433. _context.locale = _locale.value;
  434. }
  435. });
  436. // fallbackLocale
  437. const fallbackLocale = computed({
  438. get: () => _fallbackLocale.value,
  439. set: val => {
  440. _fallbackLocale.value = val;
  441. _context.fallbackLocale = _fallbackLocale.value;
  442. updateFallbackLocale(_context, _locale.value, val);
  443. }
  444. });
  445. // messages
  446. const messages = computed(() => _messages.value);
  447. // datetimeFormats
  448. const datetimeFormats = /* #__PURE__*/ computed(() => _datetimeFormats.value);
  449. // numberFormats
  450. const numberFormats = /* #__PURE__*/ computed(() => _numberFormats.value);
  451. // getPostTranslationHandler
  452. function getPostTranslationHandler() {
  453. return isFunction(_postTranslation) ? _postTranslation : null;
  454. }
  455. // setPostTranslationHandler
  456. function setPostTranslationHandler(handler) {
  457. _postTranslation = handler;
  458. _context.postTranslation = handler;
  459. }
  460. // getMissingHandler
  461. function getMissingHandler() {
  462. return _missing;
  463. }
  464. // setMissingHandler
  465. function setMissingHandler(handler) {
  466. if (handler !== null) {
  467. _runtimeMissing = defineCoreMissingHandler(handler);
  468. }
  469. _missing = handler;
  470. _context.missing = _runtimeMissing;
  471. }
  472. function isResolvedTranslateMessage(type, arg // eslint-disable-line @typescript-eslint/no-explicit-any
  473. ) {
  474. return type !== 'translate' || !arg.resolvedMessage;
  475. }
  476. const wrapWithDeps = (fn, argumentParser, warnType, fallbackSuccess, fallbackFail, successCondition) => {
  477. trackReactivityValues(); // track reactive dependency
  478. // NOTE: experimental !!
  479. let ret;
  480. try {
  481. if ((process.env.NODE_ENV !== 'production') || __INTLIFY_PROD_DEVTOOLS__) {
  482. setAdditionalMeta(getMetaInfo());
  483. }
  484. if (!_isGlobal) {
  485. _context.fallbackContext = __root
  486. ? getFallbackContext()
  487. : undefined;
  488. }
  489. ret = fn(_context);
  490. }
  491. finally {
  492. if ((process.env.NODE_ENV !== 'production') || __INTLIFY_PROD_DEVTOOLS__) {
  493. setAdditionalMeta(null);
  494. }
  495. if (!_isGlobal) {
  496. _context.fallbackContext = undefined;
  497. }
  498. }
  499. if ((warnType !== 'translate exists' && // for not `te` (e.g `t`)
  500. isNumber(ret) &&
  501. ret === NOT_REOSLVED) ||
  502. (warnType === 'translate exists' && !ret) // for `te`
  503. ) {
  504. const [key, arg2] = argumentParser();
  505. if ((process.env.NODE_ENV !== 'production') &&
  506. __root &&
  507. isString(key) &&
  508. isResolvedTranslateMessage(warnType, arg2)) {
  509. if (_fallbackRoot &&
  510. (isTranslateFallbackWarn(_fallbackWarn, key) ||
  511. isTranslateMissingWarn(_missingWarn, key))) {
  512. warn(getWarnMessage(I18nWarnCodes.FALLBACK_TO_ROOT, {
  513. key,
  514. type: warnType
  515. }));
  516. }
  517. // for vue-devtools timeline event
  518. if ((process.env.NODE_ENV !== 'production')) {
  519. const { __v_emitter: emitter } = _context;
  520. if (emitter && _fallbackRoot) {
  521. emitter.emit("fallback" /* VueDevToolsTimelineEvents.FALBACK */, {
  522. type: warnType,
  523. key,
  524. to: 'global',
  525. groupId: `${warnType}:${key}`
  526. });
  527. }
  528. }
  529. }
  530. return __root && _fallbackRoot
  531. ? fallbackSuccess(__root)
  532. : fallbackFail(key);
  533. }
  534. else if (successCondition(ret)) {
  535. return ret;
  536. }
  537. else {
  538. /* istanbul ignore next */
  539. throw createI18nError(I18nErrorCodes.UNEXPECTED_RETURN_TYPE);
  540. }
  541. };
  542. // t
  543. function t(...args) {
  544. return wrapWithDeps(context => Reflect.apply(translate, null, [context, ...args]), () => parseTranslateArgs(...args), 'translate', root => Reflect.apply(root.t, root, [...args]), key => key, val => isString(val));
  545. }
  546. // rt
  547. function rt(...args) {
  548. const [arg1, arg2, arg3] = args;
  549. if (arg3 && !isObject(arg3)) {
  550. throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
  551. }
  552. return t(...[arg1, arg2, assign({ resolvedMessage: true }, arg3 || {})]);
  553. }
  554. // d
  555. function d(...args) {
  556. return wrapWithDeps(context => Reflect.apply(datetime, null, [context, ...args]), () => parseDateTimeArgs(...args), 'datetime format', root => Reflect.apply(root.d, root, [...args]), () => MISSING_RESOLVE_VALUE, val => isString(val));
  557. }
  558. // n
  559. function n(...args) {
  560. return wrapWithDeps(context => Reflect.apply(number, null, [context, ...args]), () => parseNumberArgs(...args), 'number format', root => Reflect.apply(root.n, root, [...args]), () => MISSING_RESOLVE_VALUE, val => isString(val));
  561. }
  562. // for custom processor
  563. function normalize(values) {
  564. return values.map(val => isString(val) || isNumber(val) || isBoolean(val)
  565. ? createTextNode(String(val))
  566. : val);
  567. }
  568. const interpolate = (val) => val;
  569. const processor = {
  570. normalize,
  571. interpolate,
  572. type: 'vnode'
  573. };
  574. // translateVNode, using for `i18n-t` component
  575. function translateVNode(...args) {
  576. return wrapWithDeps(context => {
  577. let ret;
  578. const _context = context;
  579. try {
  580. _context.processor = processor;
  581. ret = Reflect.apply(translate, null, [_context, ...args]);
  582. }
  583. finally {
  584. _context.processor = null;
  585. }
  586. return ret;
  587. }, () => parseTranslateArgs(...args), 'translate',
  588. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  589. root => root[TranslateVNodeSymbol](...args), key => [createTextNode(key)], val => isArray(val));
  590. }
  591. // numberParts, using for `i18n-n` component
  592. function numberParts(...args) {
  593. return wrapWithDeps(context => Reflect.apply(number, null, [context, ...args]), () => parseNumberArgs(...args), 'number format',
  594. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  595. root => root[NumberPartsSymbol](...args), NOOP_RETURN_ARRAY, val => isString(val) || isArray(val));
  596. }
  597. // datetimeParts, using for `i18n-d` component
  598. function datetimeParts(...args) {
  599. return wrapWithDeps(context => Reflect.apply(datetime, null, [context, ...args]), () => parseDateTimeArgs(...args), 'datetime format',
  600. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  601. root => root[DatetimePartsSymbol](...args), NOOP_RETURN_ARRAY, val => isString(val) || isArray(val));
  602. }
  603. function setPluralRules(rules) {
  604. _pluralRules = rules;
  605. _context.pluralRules = _pluralRules;
  606. }
  607. // te
  608. function te(key, locale) {
  609. return wrapWithDeps(() => {
  610. if (!key) {
  611. return false;
  612. }
  613. const targetLocale = isString(locale) ? locale : _locale.value;
  614. const message = getLocaleMessage(targetLocale);
  615. const resolved = _context.messageResolver(message, key);
  616. return (isMessageAST(resolved) ||
  617. isMessageFunction(resolved) ||
  618. isString(resolved));
  619. }, () => [key], 'translate exists', root => {
  620. return Reflect.apply(root.te, root, [key, locale]);
  621. }, NOOP_RETURN_FALSE, val => isBoolean(val));
  622. }
  623. function resolveMessages(key) {
  624. let messages = null;
  625. const locales = fallbackWithLocaleChain(_context, _fallbackLocale.value, _locale.value);
  626. for (let i = 0; i < locales.length; i++) {
  627. const targetLocaleMessages = _messages.value[locales[i]] || {};
  628. const messageValue = _context.messageResolver(targetLocaleMessages, key);
  629. if (messageValue != null) {
  630. messages = messageValue;
  631. break;
  632. }
  633. }
  634. return messages;
  635. }
  636. // tm
  637. function tm(key) {
  638. const messages = resolveMessages(key);
  639. // prettier-ignore
  640. return messages != null
  641. ? messages
  642. : __root
  643. ? __root.tm(key) || {}
  644. : {};
  645. }
  646. // getLocaleMessage
  647. function getLocaleMessage(locale) {
  648. return (_messages.value[locale] || {});
  649. }
  650. // setLocaleMessage
  651. function setLocaleMessage(locale, message) {
  652. if (flatJson) {
  653. const _message = { [locale]: message };
  654. for (const key in _message) {
  655. if (hasOwn(_message, key)) {
  656. handleFlatJson(_message[key]);
  657. }
  658. }
  659. message = _message[locale];
  660. }
  661. _messages.value[locale] = message;
  662. _context.messages = _messages.value;
  663. }
  664. // mergeLocaleMessage
  665. function mergeLocaleMessage(locale, message) {
  666. _messages.value[locale] = _messages.value[locale] || {};
  667. const _message = { [locale]: message };
  668. for (const key in _message) {
  669. if (hasOwn(_message, key)) {
  670. handleFlatJson(_message[key]);
  671. }
  672. }
  673. message = _message[locale];
  674. deepCopy(message, _messages.value[locale]);
  675. _context.messages = _messages.value;
  676. }
  677. // getDateTimeFormat
  678. function getDateTimeFormat(locale) {
  679. return _datetimeFormats.value[locale] || {};
  680. }
  681. // setDateTimeFormat
  682. function setDateTimeFormat(locale, format) {
  683. _datetimeFormats.value[locale] = format;
  684. _context.datetimeFormats = _datetimeFormats.value;
  685. clearDateTimeFormat(_context, locale, format);
  686. }
  687. // mergeDateTimeFormat
  688. function mergeDateTimeFormat(locale, format) {
  689. _datetimeFormats.value[locale] = assign(_datetimeFormats.value[locale] || {}, format);
  690. _context.datetimeFormats = _datetimeFormats.value;
  691. clearDateTimeFormat(_context, locale, format);
  692. }
  693. // getNumberFormat
  694. function getNumberFormat(locale) {
  695. return _numberFormats.value[locale] || {};
  696. }
  697. // setNumberFormat
  698. function setNumberFormat(locale, format) {
  699. _numberFormats.value[locale] = format;
  700. _context.numberFormats = _numberFormats.value;
  701. clearNumberFormat(_context, locale, format);
  702. }
  703. // mergeNumberFormat
  704. function mergeNumberFormat(locale, format) {
  705. _numberFormats.value[locale] = assign(_numberFormats.value[locale] || {}, format);
  706. _context.numberFormats = _numberFormats.value;
  707. clearNumberFormat(_context, locale, format);
  708. }
  709. // for debug
  710. composerID++;
  711. // watch root locale & fallbackLocale
  712. if (__root && inBrowser) {
  713. watch(__root.locale, (val) => {
  714. if (_inheritLocale) {
  715. _locale.value = val;
  716. _context.locale = val;
  717. updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  718. }
  719. });
  720. watch(__root.fallbackLocale, (val) => {
  721. if (_inheritLocale) {
  722. _fallbackLocale.value = val;
  723. _context.fallbackLocale = val;
  724. updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  725. }
  726. });
  727. }
  728. // define basic composition API!
  729. const composer = {
  730. id: composerID,
  731. locale,
  732. fallbackLocale,
  733. get inheritLocale() {
  734. return _inheritLocale;
  735. },
  736. set inheritLocale(val) {
  737. _inheritLocale = val;
  738. if (val && __root) {
  739. _locale.value = __root.locale.value;
  740. _fallbackLocale.value = __root.fallbackLocale.value;
  741. updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  742. }
  743. },
  744. get availableLocales() {
  745. return Object.keys(_messages.value).sort();
  746. },
  747. messages,
  748. get modifiers() {
  749. return _modifiers;
  750. },
  751. get pluralRules() {
  752. return _pluralRules || {};
  753. },
  754. get isGlobal() {
  755. return _isGlobal;
  756. },
  757. get missingWarn() {
  758. return _missingWarn;
  759. },
  760. set missingWarn(val) {
  761. _missingWarn = val;
  762. _context.missingWarn = _missingWarn;
  763. },
  764. get fallbackWarn() {
  765. return _fallbackWarn;
  766. },
  767. set fallbackWarn(val) {
  768. _fallbackWarn = val;
  769. _context.fallbackWarn = _fallbackWarn;
  770. },
  771. get fallbackRoot() {
  772. return _fallbackRoot;
  773. },
  774. set fallbackRoot(val) {
  775. _fallbackRoot = val;
  776. },
  777. get fallbackFormat() {
  778. return _fallbackFormat;
  779. },
  780. set fallbackFormat(val) {
  781. _fallbackFormat = val;
  782. _context.fallbackFormat = _fallbackFormat;
  783. },
  784. get warnHtmlMessage() {
  785. return _warnHtmlMessage;
  786. },
  787. set warnHtmlMessage(val) {
  788. _warnHtmlMessage = val;
  789. _context.warnHtmlMessage = val;
  790. },
  791. get escapeParameter() {
  792. return _escapeParameter;
  793. },
  794. set escapeParameter(val) {
  795. _escapeParameter = val;
  796. _context.escapeParameter = val;
  797. },
  798. t,
  799. getLocaleMessage,
  800. setLocaleMessage,
  801. mergeLocaleMessage,
  802. getPostTranslationHandler,
  803. setPostTranslationHandler,
  804. getMissingHandler,
  805. setMissingHandler,
  806. [SetPluralRulesSymbol]: setPluralRules
  807. };
  808. {
  809. composer.datetimeFormats = datetimeFormats;
  810. composer.numberFormats = numberFormats;
  811. composer.rt = rt;
  812. composer.te = te;
  813. composer.tm = tm;
  814. composer.d = d;
  815. composer.n = n;
  816. composer.getDateTimeFormat = getDateTimeFormat;
  817. composer.setDateTimeFormat = setDateTimeFormat;
  818. composer.mergeDateTimeFormat = mergeDateTimeFormat;
  819. composer.getNumberFormat = getNumberFormat;
  820. composer.setNumberFormat = setNumberFormat;
  821. composer.mergeNumberFormat = mergeNumberFormat;
  822. composer[InejctWithOptionSymbol] = __injectWithOption;
  823. composer[TranslateVNodeSymbol] = translateVNode;
  824. composer[DatetimePartsSymbol] = datetimeParts;
  825. composer[NumberPartsSymbol] = numberParts;
  826. }
  827. // for vue-devtools timeline event
  828. if ((process.env.NODE_ENV !== 'production')) {
  829. composer[EnableEmitter] = (emitter) => {
  830. _context.__v_emitter = emitter;
  831. };
  832. composer[DisableEmitter] = () => {
  833. _context.__v_emitter = undefined;
  834. };
  835. }
  836. return composer;
  837. }
  838. /* eslint-enable @typescript-eslint/no-explicit-any */
  839. /* eslint-disable @typescript-eslint/no-explicit-any */
  840. /**
  841. * Convert to I18n Composer Options from VueI18n Options
  842. *
  843. * @internal
  844. */
  845. function convertComposerOptions(options) {
  846. const locale = isString(options.locale) ? options.locale : DEFAULT_LOCALE;
  847. const fallbackLocale = isString(options.fallbackLocale) ||
  848. isArray(options.fallbackLocale) ||
  849. isPlainObject(options.fallbackLocale) ||
  850. options.fallbackLocale === false
  851. ? options.fallbackLocale
  852. : locale;
  853. const missing = isFunction(options.missing) ? options.missing : undefined;
  854. const missingWarn = isBoolean(options.silentTranslationWarn) ||
  855. isRegExp(options.silentTranslationWarn)
  856. ? !options.silentTranslationWarn
  857. : true;
  858. const fallbackWarn = isBoolean(options.silentFallbackWarn) ||
  859. isRegExp(options.silentFallbackWarn)
  860. ? !options.silentFallbackWarn
  861. : true;
  862. const fallbackRoot = isBoolean(options.fallbackRoot)
  863. ? options.fallbackRoot
  864. : true;
  865. const fallbackFormat = !!options.formatFallbackMessages;
  866. const modifiers = isPlainObject(options.modifiers) ? options.modifiers : {};
  867. const pluralizationRules = options.pluralizationRules;
  868. const postTranslation = isFunction(options.postTranslation)
  869. ? options.postTranslation
  870. : undefined;
  871. const warnHtmlMessage = isString(options.warnHtmlInMessage)
  872. ? options.warnHtmlInMessage !== 'off'
  873. : true;
  874. const escapeParameter = !!options.escapeParameterHtml;
  875. const inheritLocale = isBoolean(options.sync) ? options.sync : true;
  876. if ((process.env.NODE_ENV !== 'production') && options.formatter) {
  877. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_FORMATTER));
  878. }
  879. if ((process.env.NODE_ENV !== 'production') && options.preserveDirectiveContent) {
  880. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE));
  881. }
  882. let messages = options.messages;
  883. if (isPlainObject(options.sharedMessages)) {
  884. const sharedMessages = options.sharedMessages;
  885. const locales = Object.keys(sharedMessages);
  886. messages = locales.reduce((messages, locale) => {
  887. const message = messages[locale] || (messages[locale] = {});
  888. assign(message, sharedMessages[locale]);
  889. return messages;
  890. }, (messages || {}));
  891. }
  892. const { __i18n, __root, __injectWithOption } = options;
  893. const datetimeFormats = options.datetimeFormats;
  894. const numberFormats = options.numberFormats;
  895. const flatJson = options.flatJson;
  896. return {
  897. locale,
  898. fallbackLocale,
  899. messages,
  900. flatJson,
  901. datetimeFormats,
  902. numberFormats,
  903. missing,
  904. missingWarn,
  905. fallbackWarn,
  906. fallbackRoot,
  907. fallbackFormat,
  908. modifiers,
  909. pluralRules: pluralizationRules,
  910. postTranslation,
  911. warnHtmlMessage,
  912. escapeParameter,
  913. messageResolver: options.messageResolver,
  914. inheritLocale,
  915. __i18n,
  916. __root,
  917. __injectWithOption
  918. };
  919. }
  920. /**
  921. * create VueI18n interface factory
  922. *
  923. * @internal
  924. */
  925. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  926. function createVueI18n(options = {}, VueI18nLegacy) {
  927. {
  928. const composer = createComposer(convertComposerOptions(options));
  929. const { __extender } = options;
  930. // defines VueI18n
  931. const vueI18n = {
  932. // id
  933. id: composer.id,
  934. // locale
  935. get locale() {
  936. return composer.locale.value;
  937. },
  938. set locale(val) {
  939. composer.locale.value = val;
  940. },
  941. // fallbackLocale
  942. get fallbackLocale() {
  943. return composer.fallbackLocale.value;
  944. },
  945. set fallbackLocale(val) {
  946. composer.fallbackLocale.value = val;
  947. },
  948. // messages
  949. get messages() {
  950. return composer.messages.value;
  951. },
  952. // datetimeFormats
  953. get datetimeFormats() {
  954. return composer.datetimeFormats.value;
  955. },
  956. // numberFormats
  957. get numberFormats() {
  958. return composer.numberFormats.value;
  959. },
  960. // availableLocales
  961. get availableLocales() {
  962. return composer.availableLocales;
  963. },
  964. // formatter
  965. get formatter() {
  966. (process.env.NODE_ENV !== 'production') && warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_FORMATTER));
  967. // dummy
  968. return {
  969. interpolate() {
  970. return [];
  971. }
  972. };
  973. },
  974. set formatter(val) {
  975. (process.env.NODE_ENV !== 'production') && warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_FORMATTER));
  976. },
  977. // missing
  978. get missing() {
  979. return composer.getMissingHandler();
  980. },
  981. set missing(handler) {
  982. composer.setMissingHandler(handler);
  983. },
  984. // silentTranslationWarn
  985. get silentTranslationWarn() {
  986. return isBoolean(composer.missingWarn)
  987. ? !composer.missingWarn
  988. : composer.missingWarn;
  989. },
  990. set silentTranslationWarn(val) {
  991. composer.missingWarn = isBoolean(val) ? !val : val;
  992. },
  993. // silentFallbackWarn
  994. get silentFallbackWarn() {
  995. return isBoolean(composer.fallbackWarn)
  996. ? !composer.fallbackWarn
  997. : composer.fallbackWarn;
  998. },
  999. set silentFallbackWarn(val) {
  1000. composer.fallbackWarn = isBoolean(val) ? !val : val;
  1001. },
  1002. // modifiers
  1003. get modifiers() {
  1004. return composer.modifiers;
  1005. },
  1006. // formatFallbackMessages
  1007. get formatFallbackMessages() {
  1008. return composer.fallbackFormat;
  1009. },
  1010. set formatFallbackMessages(val) {
  1011. composer.fallbackFormat = val;
  1012. },
  1013. // postTranslation
  1014. get postTranslation() {
  1015. return composer.getPostTranslationHandler();
  1016. },
  1017. set postTranslation(handler) {
  1018. composer.setPostTranslationHandler(handler);
  1019. },
  1020. // sync
  1021. get sync() {
  1022. return composer.inheritLocale;
  1023. },
  1024. set sync(val) {
  1025. composer.inheritLocale = val;
  1026. },
  1027. // warnInHtmlMessage
  1028. get warnHtmlInMessage() {
  1029. return composer.warnHtmlMessage ? 'warn' : 'off';
  1030. },
  1031. set warnHtmlInMessage(val) {
  1032. composer.warnHtmlMessage = val !== 'off';
  1033. },
  1034. // escapeParameterHtml
  1035. get escapeParameterHtml() {
  1036. return composer.escapeParameter;
  1037. },
  1038. set escapeParameterHtml(val) {
  1039. composer.escapeParameter = val;
  1040. },
  1041. // preserveDirectiveContent
  1042. get preserveDirectiveContent() {
  1043. (process.env.NODE_ENV !== 'production') &&
  1044. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE));
  1045. return true;
  1046. },
  1047. set preserveDirectiveContent(val) {
  1048. (process.env.NODE_ENV !== 'production') &&
  1049. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE));
  1050. },
  1051. // pluralizationRules
  1052. get pluralizationRules() {
  1053. return composer.pluralRules || {};
  1054. },
  1055. // for internal
  1056. __composer: composer,
  1057. // t
  1058. t(...args) {
  1059. const [arg1, arg2, arg3] = args;
  1060. const options = {};
  1061. let list = null;
  1062. let named = null;
  1063. if (!isString(arg1)) {
  1064. throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
  1065. }
  1066. const key = arg1;
  1067. if (isString(arg2)) {
  1068. options.locale = arg2;
  1069. }
  1070. else if (isArray(arg2)) {
  1071. list = arg2;
  1072. }
  1073. else if (isPlainObject(arg2)) {
  1074. named = arg2;
  1075. }
  1076. if (isArray(arg3)) {
  1077. list = arg3;
  1078. }
  1079. else if (isPlainObject(arg3)) {
  1080. named = arg3;
  1081. }
  1082. // return composer.t(key, (list || named || {}) as any, options)
  1083. return Reflect.apply(composer.t, composer, [
  1084. key,
  1085. (list || named || {}),
  1086. options
  1087. ]);
  1088. },
  1089. rt(...args) {
  1090. return Reflect.apply(composer.rt, composer, [...args]);
  1091. },
  1092. // tc
  1093. tc(...args) {
  1094. const [arg1, arg2, arg3] = args;
  1095. const options = { plural: 1 };
  1096. let list = null;
  1097. let named = null;
  1098. if (!isString(arg1)) {
  1099. throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
  1100. }
  1101. const key = arg1;
  1102. if (isString(arg2)) {
  1103. options.locale = arg2;
  1104. }
  1105. else if (isNumber(arg2)) {
  1106. options.plural = arg2;
  1107. }
  1108. else if (isArray(arg2)) {
  1109. list = arg2;
  1110. }
  1111. else if (isPlainObject(arg2)) {
  1112. named = arg2;
  1113. }
  1114. if (isString(arg3)) {
  1115. options.locale = arg3;
  1116. }
  1117. else if (isArray(arg3)) {
  1118. list = arg3;
  1119. }
  1120. else if (isPlainObject(arg3)) {
  1121. named = arg3;
  1122. }
  1123. // return composer.t(key, (list || named || {}) as any, options)
  1124. return Reflect.apply(composer.t, composer, [
  1125. key,
  1126. (list || named || {}),
  1127. options
  1128. ]);
  1129. },
  1130. // te
  1131. te(key, locale) {
  1132. return composer.te(key, locale);
  1133. },
  1134. // tm
  1135. tm(key) {
  1136. return composer.tm(key);
  1137. },
  1138. // getLocaleMessage
  1139. getLocaleMessage(locale) {
  1140. return composer.getLocaleMessage(locale);
  1141. },
  1142. // setLocaleMessage
  1143. setLocaleMessage(locale, message) {
  1144. composer.setLocaleMessage(locale, message);
  1145. },
  1146. // mergeLocaleMessage
  1147. mergeLocaleMessage(locale, message) {
  1148. composer.mergeLocaleMessage(locale, message);
  1149. },
  1150. // d
  1151. d(...args) {
  1152. return Reflect.apply(composer.d, composer, [...args]);
  1153. },
  1154. // getDateTimeFormat
  1155. getDateTimeFormat(locale) {
  1156. return composer.getDateTimeFormat(locale);
  1157. },
  1158. // setDateTimeFormat
  1159. setDateTimeFormat(locale, format) {
  1160. composer.setDateTimeFormat(locale, format);
  1161. },
  1162. // mergeDateTimeFormat
  1163. mergeDateTimeFormat(locale, format) {
  1164. composer.mergeDateTimeFormat(locale, format);
  1165. },
  1166. // n
  1167. n(...args) {
  1168. return Reflect.apply(composer.n, composer, [...args]);
  1169. },
  1170. // getNumberFormat
  1171. getNumberFormat(locale) {
  1172. return composer.getNumberFormat(locale);
  1173. },
  1174. // setNumberFormat
  1175. setNumberFormat(locale, format) {
  1176. composer.setNumberFormat(locale, format);
  1177. },
  1178. // mergeNumberFormat
  1179. mergeNumberFormat(locale, format) {
  1180. composer.mergeNumberFormat(locale, format);
  1181. },
  1182. // getChoiceIndex
  1183. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  1184. getChoiceIndex(choice, choicesLength) {
  1185. (process.env.NODE_ENV !== 'production') &&
  1186. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX));
  1187. return -1;
  1188. }
  1189. };
  1190. vueI18n.__extender = __extender;
  1191. // for vue-devtools timeline event
  1192. if ((process.env.NODE_ENV !== 'production')) {
  1193. vueI18n.__enableEmitter = (emitter) => {
  1194. const __composer = composer;
  1195. __composer[EnableEmitter] && __composer[EnableEmitter](emitter);
  1196. };
  1197. vueI18n.__disableEmitter = () => {
  1198. const __composer = composer;
  1199. __composer[DisableEmitter] && __composer[DisableEmitter]();
  1200. };
  1201. }
  1202. return vueI18n;
  1203. }
  1204. }
  1205. /* eslint-enable @typescript-eslint/no-explicit-any */
  1206. const baseFormatProps = {
  1207. tag: {
  1208. type: [String, Object]
  1209. },
  1210. locale: {
  1211. type: String
  1212. },
  1213. scope: {
  1214. type: String,
  1215. // NOTE: avoid https://github.com/microsoft/rushstack/issues/1050
  1216. validator: (val /* ComponentI18nScope */) => val === 'parent' || val === 'global',
  1217. default: 'parent' /* ComponentI18nScope */
  1218. },
  1219. i18n: {
  1220. type: Object
  1221. }
  1222. };
  1223. function getInterpolateArg(
  1224. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1225. { slots }, // SetupContext,
  1226. keys) {
  1227. if (keys.length === 1 && keys[0] === 'default') {
  1228. // default slot with list
  1229. const ret = slots.default ? slots.default() : [];
  1230. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1231. return ret.reduce((slot, current) => {
  1232. return [
  1233. ...slot,
  1234. // prettier-ignore
  1235. ...(current.type === Fragment ? current.children : [current]
  1236. )
  1237. ];
  1238. }, []);
  1239. }
  1240. else {
  1241. // named slots
  1242. return keys.reduce((arg, key) => {
  1243. const slot = slots[key];
  1244. if (slot) {
  1245. arg[key] = slot();
  1246. }
  1247. return arg;
  1248. }, {});
  1249. }
  1250. }
  1251. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1252. function getFragmentableTag(tag) {
  1253. return Fragment ;
  1254. }
  1255. const TranslationImpl = /*#__PURE__*/ defineComponent({
  1256. /* eslint-disable */
  1257. name: 'i18n-t',
  1258. props: assign({
  1259. keypath: {
  1260. type: String,
  1261. required: true
  1262. },
  1263. plural: {
  1264. type: [Number, String],
  1265. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1266. validator: (val) => isNumber(val) || !isNaN(val)
  1267. }
  1268. }, baseFormatProps),
  1269. /* eslint-enable */
  1270. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1271. setup(props, context) {
  1272. const { slots, attrs } = context;
  1273. // NOTE: avoid https://github.com/microsoft/rushstack/issues/1050
  1274. const i18n = props.i18n ||
  1275. useI18n({
  1276. useScope: props.scope,
  1277. __useComponent: true
  1278. });
  1279. return () => {
  1280. const keys = Object.keys(slots).filter(key => key !== '_');
  1281. const options = {};
  1282. if (props.locale) {
  1283. options.locale = props.locale;
  1284. }
  1285. if (props.plural !== undefined) {
  1286. options.plural = isString(props.plural) ? +props.plural : props.plural;
  1287. }
  1288. const arg = getInterpolateArg(context, keys);
  1289. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1290. const children = i18n[TranslateVNodeSymbol](props.keypath, arg, options);
  1291. const assignedAttrs = assign({}, attrs);
  1292. const tag = isString(props.tag) || isObject(props.tag)
  1293. ? props.tag
  1294. : getFragmentableTag();
  1295. return h(tag, assignedAttrs, children);
  1296. };
  1297. }
  1298. });
  1299. /**
  1300. * export the public type for h/tsx inference
  1301. * also to avoid inline import() in generated d.ts files
  1302. */
  1303. /**
  1304. * Translation Component
  1305. *
  1306. * @remarks
  1307. * See the following items for property about details
  1308. *
  1309. * @VueI18nSee [TranslationProps](component#translationprops)
  1310. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  1311. * @VueI18nSee [Component Interpolation](../guide/advanced/component)
  1312. *
  1313. * @example
  1314. * ```html
  1315. * <div id="app">
  1316. * <!-- ... -->
  1317. * <i18n keypath="term" tag="label" for="tos">
  1318. * <a :href="url" target="_blank">{{ $t('tos') }}</a>
  1319. * </i18n>
  1320. * <!-- ... -->
  1321. * </div>
  1322. * ```
  1323. * ```js
  1324. * import { createApp } from 'vue'
  1325. * import { createI18n } from 'vue-i18n'
  1326. *
  1327. * const messages = {
  1328. * en: {
  1329. * tos: 'Term of Service',
  1330. * term: 'I accept xxx {0}.'
  1331. * },
  1332. * ja: {
  1333. * tos: '利用規約',
  1334. * term: '私は xxx の{0}に同意します。'
  1335. * }
  1336. * }
  1337. *
  1338. * const i18n = createI18n({
  1339. * locale: 'en',
  1340. * messages
  1341. * })
  1342. *
  1343. * const app = createApp({
  1344. * data: {
  1345. * url: '/term'
  1346. * }
  1347. * }).use(i18n).mount('#app')
  1348. * ```
  1349. *
  1350. * @VueI18nComponent
  1351. */
  1352. const Translation = TranslationImpl;
  1353. const I18nT = Translation;
  1354. function isVNode(target) {
  1355. return isArray(target) && !isString(target[0]);
  1356. }
  1357. function renderFormatter(props, context, slotKeys, partFormatter) {
  1358. const { slots, attrs } = context;
  1359. return () => {
  1360. const options = { part: true };
  1361. let overrides = {};
  1362. if (props.locale) {
  1363. options.locale = props.locale;
  1364. }
  1365. if (isString(props.format)) {
  1366. options.key = props.format;
  1367. }
  1368. else if (isObject(props.format)) {
  1369. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1370. if (isString(props.format.key)) {
  1371. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1372. options.key = props.format.key;
  1373. }
  1374. // Filter out number format options only
  1375. overrides = Object.keys(props.format).reduce((options, prop) => {
  1376. return slotKeys.includes(prop)
  1377. ? assign({}, options, { [prop]: props.format[prop] }) // eslint-disable-line @typescript-eslint/no-explicit-any
  1378. : options;
  1379. }, {});
  1380. }
  1381. const parts = partFormatter(...[props.value, options, overrides]);
  1382. let children = [options.key];
  1383. if (isArray(parts)) {
  1384. children = parts.map((part, index) => {
  1385. const slot = slots[part.type];
  1386. const node = slot
  1387. ? slot({ [part.type]: part.value, index, parts })
  1388. : [part.value];
  1389. if (isVNode(node)) {
  1390. node[0].key = `${part.type}-${index}`;
  1391. }
  1392. return node;
  1393. });
  1394. }
  1395. else if (isString(parts)) {
  1396. children = [parts];
  1397. }
  1398. const assignedAttrs = assign({}, attrs);
  1399. const tag = isString(props.tag) || isObject(props.tag)
  1400. ? props.tag
  1401. : getFragmentableTag();
  1402. return h(tag, assignedAttrs, children);
  1403. };
  1404. }
  1405. const NumberFormatImpl = /*#__PURE__*/ defineComponent({
  1406. /* eslint-disable */
  1407. name: 'i18n-n',
  1408. props: assign({
  1409. value: {
  1410. type: Number,
  1411. required: true
  1412. },
  1413. format: {
  1414. type: [String, Object]
  1415. }
  1416. }, baseFormatProps),
  1417. /* eslint-enable */
  1418. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1419. setup(props, context) {
  1420. const i18n = props.i18n ||
  1421. useI18n({
  1422. useScope: 'parent',
  1423. __useComponent: true
  1424. });
  1425. return renderFormatter(props, context, NUMBER_FORMAT_OPTIONS_KEYS, (...args) =>
  1426. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1427. i18n[NumberPartsSymbol](...args));
  1428. }
  1429. });
  1430. /**
  1431. * export the public type for h/tsx inference
  1432. * also to avoid inline import() in generated d.ts files
  1433. */
  1434. /**
  1435. * Number Format Component
  1436. *
  1437. * @remarks
  1438. * See the following items for property about details
  1439. *
  1440. * @VueI18nSee [FormattableProps](component#formattableprops)
  1441. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  1442. * @VueI18nSee [Custom Formatting](../guide/essentials/number#custom-formatting)
  1443. *
  1444. * @VueI18nDanger
  1445. * 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)
  1446. *
  1447. * If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-numberformat)
  1448. *
  1449. * @VueI18nComponent
  1450. */
  1451. const NumberFormat = NumberFormatImpl;
  1452. const I18nN = NumberFormat;
  1453. const DatetimeFormatImpl = /* #__PURE__*/ defineComponent({
  1454. /* eslint-disable */
  1455. name: 'i18n-d',
  1456. props: assign({
  1457. value: {
  1458. type: [Number, Date],
  1459. required: true
  1460. },
  1461. format: {
  1462. type: [String, Object]
  1463. }
  1464. }, baseFormatProps),
  1465. /* eslint-enable */
  1466. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1467. setup(props, context) {
  1468. const i18n = props.i18n ||
  1469. useI18n({
  1470. useScope: 'parent',
  1471. __useComponent: true
  1472. });
  1473. return renderFormatter(props, context, DATETIME_FORMAT_OPTIONS_KEYS, (...args) =>
  1474. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1475. i18n[DatetimePartsSymbol](...args));
  1476. }
  1477. });
  1478. /**
  1479. * Datetime Format Component
  1480. *
  1481. * @remarks
  1482. * See the following items for property about details
  1483. *
  1484. * @VueI18nSee [FormattableProps](component#formattableprops)
  1485. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  1486. * @VueI18nSee [Custom Formatting](../guide/essentials/datetime#custom-formatting)
  1487. *
  1488. * @VueI18nDanger
  1489. * 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)
  1490. *
  1491. * If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-datetimeformat)
  1492. *
  1493. * @VueI18nComponent
  1494. */
  1495. const DatetimeFormat = DatetimeFormatImpl;
  1496. const I18nD = DatetimeFormat;
  1497. function getComposer$2(i18n, instance) {
  1498. const i18nInternal = i18n;
  1499. if (i18n.mode === 'composition') {
  1500. return (i18nInternal.__getInstance(instance) || i18n.global);
  1501. }
  1502. else {
  1503. const vueI18n = i18nInternal.__getInstance(instance);
  1504. return vueI18n != null
  1505. ? vueI18n.__composer
  1506. : i18n.global.__composer;
  1507. }
  1508. }
  1509. function vTDirective(i18n) {
  1510. const _process = (binding) => {
  1511. const { instance, modifiers, value } = binding;
  1512. /* istanbul ignore if */
  1513. if (!instance || !instance.$) {
  1514. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  1515. }
  1516. const composer = getComposer$2(i18n, instance.$);
  1517. if ((process.env.NODE_ENV !== 'production') && modifiers.preserve) {
  1518. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE));
  1519. }
  1520. const parsedValue = parseValue(value);
  1521. return [
  1522. Reflect.apply(composer.t, composer, [...makeParams(parsedValue)]),
  1523. composer
  1524. ];
  1525. };
  1526. const register = (el, binding) => {
  1527. const [textContent, composer] = _process(binding);
  1528. if (inBrowser && i18n.global === composer) {
  1529. // global scope only
  1530. el.__i18nWatcher = watch(composer.locale, () => {
  1531. binding.instance && binding.instance.$forceUpdate();
  1532. });
  1533. }
  1534. el.__composer = composer;
  1535. el.textContent = textContent;
  1536. };
  1537. const unregister = (el) => {
  1538. if (inBrowser && el.__i18nWatcher) {
  1539. el.__i18nWatcher();
  1540. el.__i18nWatcher = undefined;
  1541. delete el.__i18nWatcher;
  1542. }
  1543. if (el.__composer) {
  1544. el.__composer = undefined;
  1545. delete el.__composer;
  1546. }
  1547. };
  1548. const update = (el, { value }) => {
  1549. if (el.__composer) {
  1550. const composer = el.__composer;
  1551. const parsedValue = parseValue(value);
  1552. el.textContent = Reflect.apply(composer.t, composer, [
  1553. ...makeParams(parsedValue)
  1554. ]);
  1555. }
  1556. };
  1557. const getSSRProps = (binding) => {
  1558. const [textContent] = _process(binding);
  1559. return { textContent };
  1560. };
  1561. return {
  1562. created: register,
  1563. unmounted: unregister,
  1564. beforeUpdate: update,
  1565. getSSRProps
  1566. };
  1567. }
  1568. function parseValue(value) {
  1569. if (isString(value)) {
  1570. return { path: value };
  1571. }
  1572. else if (isPlainObject(value)) {
  1573. if (!('path' in value)) {
  1574. throw createI18nError(I18nErrorCodes.REQUIRED_VALUE, 'path');
  1575. }
  1576. return value;
  1577. }
  1578. else {
  1579. throw createI18nError(I18nErrorCodes.INVALID_VALUE);
  1580. }
  1581. }
  1582. function makeParams(value) {
  1583. const { path, locale, args, choice, plural } = value;
  1584. const options = {};
  1585. const named = args || {};
  1586. if (isString(locale)) {
  1587. options.locale = locale;
  1588. }
  1589. if (isNumber(choice)) {
  1590. options.plural = choice;
  1591. }
  1592. if (isNumber(plural)) {
  1593. options.plural = plural;
  1594. }
  1595. return [path, named, options];
  1596. }
  1597. function apply(app, i18n, ...options) {
  1598. const pluginOptions = isPlainObject(options[0])
  1599. ? options[0]
  1600. : {};
  1601. const useI18nComponentName = !!pluginOptions.useI18nComponentName;
  1602. const globalInstall = isBoolean(pluginOptions.globalInstall)
  1603. ? pluginOptions.globalInstall
  1604. : true;
  1605. if ((process.env.NODE_ENV !== 'production') && globalInstall && useI18nComponentName) {
  1606. warn(getWarnMessage(I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE, {
  1607. name: Translation.name
  1608. }));
  1609. }
  1610. if (globalInstall) {
  1611. [!useI18nComponentName ? Translation.name : 'i18n', 'I18nT'].forEach(name => app.component(name, Translation));
  1612. [NumberFormat.name, 'I18nN'].forEach(name => app.component(name, NumberFormat));
  1613. [DatetimeFormat.name, 'I18nD'].forEach(name => app.component(name, DatetimeFormat));
  1614. }
  1615. // install directive
  1616. {
  1617. app.directive('t', vTDirective(i18n));
  1618. }
  1619. }
  1620. const VueDevToolsLabels = {
  1621. ["vue-devtools-plugin-vue-i18n" /* VueDevToolsIDs.PLUGIN */]: 'Vue I18n devtools',
  1622. ["vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */]: 'I18n Resources',
  1623. ["vue-i18n-timeline" /* VueDevToolsIDs.TIMELINE */]: 'Vue I18n'
  1624. };
  1625. const VueDevToolsPlaceholders = {
  1626. ["vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */]: 'Search for scopes ...'
  1627. };
  1628. const VueDevToolsTimelineColors = {
  1629. ["vue-i18n-timeline" /* VueDevToolsIDs.TIMELINE */]: 0xffcd19
  1630. };
  1631. const VUE_I18N_COMPONENT_TYPES = 'vue-i18n: composer properties';
  1632. let devtoolsApi;
  1633. async function enableDevTools(app, i18n) {
  1634. return new Promise((resolve, reject) => {
  1635. try {
  1636. setupDevtoolsPlugin({
  1637. id: "vue-devtools-plugin-vue-i18n" /* VueDevToolsIDs.PLUGIN */,
  1638. label: VueDevToolsLabels["vue-devtools-plugin-vue-i18n" /* VueDevToolsIDs.PLUGIN */],
  1639. packageName: 'vue-i18n',
  1640. homepage: 'https://vue-i18n.intlify.dev',
  1641. logo: 'https://vue-i18n.intlify.dev/vue-i18n-devtools-logo.png',
  1642. componentStateTypes: [VUE_I18N_COMPONENT_TYPES],
  1643. app: app // eslint-disable-line @typescript-eslint/no-explicit-any
  1644. }, api => {
  1645. devtoolsApi = api;
  1646. api.on.visitComponentTree(({ componentInstance, treeNode }) => {
  1647. updateComponentTreeTags(componentInstance, treeNode, i18n);
  1648. });
  1649. api.on.inspectComponent(({ componentInstance, instanceData }) => {
  1650. if (componentInstance.vnode.el &&
  1651. componentInstance.vnode.el.__VUE_I18N__ &&
  1652. instanceData) {
  1653. if (i18n.mode === 'legacy') {
  1654. // ignore global scope on legacy mode
  1655. if (componentInstance.vnode.el.__VUE_I18N__ !==
  1656. i18n.global.__composer) {
  1657. inspectComposer(instanceData, componentInstance.vnode.el.__VUE_I18N__);
  1658. }
  1659. }
  1660. else {
  1661. inspectComposer(instanceData, componentInstance.vnode.el.__VUE_I18N__);
  1662. }
  1663. }
  1664. });
  1665. api.addInspector({
  1666. id: "vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */,
  1667. label: VueDevToolsLabels["vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */],
  1668. icon: 'language',
  1669. treeFilterPlaceholder: VueDevToolsPlaceholders["vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */]
  1670. });
  1671. api.on.getInspectorTree(payload => {
  1672. if (payload.app === app &&
  1673. payload.inspectorId === "vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */) {
  1674. registerScope(payload, i18n);
  1675. }
  1676. });
  1677. const roots = new Map();
  1678. api.on.getInspectorState(async (payload) => {
  1679. if (payload.app === app &&
  1680. payload.inspectorId === "vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */) {
  1681. api.unhighlightElement();
  1682. inspectScope(payload, i18n);
  1683. if (payload.nodeId === 'global') {
  1684. if (!roots.has(payload.app)) {
  1685. const [root] = await api.getComponentInstances(payload.app);
  1686. roots.set(payload.app, root);
  1687. }
  1688. api.highlightElement(roots.get(payload.app));
  1689. }
  1690. else {
  1691. const instance = getComponentInstance(payload.nodeId, i18n);
  1692. instance && api.highlightElement(instance);
  1693. }
  1694. }
  1695. });
  1696. api.on.editInspectorState(payload => {
  1697. if (payload.app === app &&
  1698. payload.inspectorId === "vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */) {
  1699. editScope(payload, i18n);
  1700. }
  1701. });
  1702. api.addTimelineLayer({
  1703. id: "vue-i18n-timeline" /* VueDevToolsIDs.TIMELINE */,
  1704. label: VueDevToolsLabels["vue-i18n-timeline" /* VueDevToolsIDs.TIMELINE */],
  1705. color: VueDevToolsTimelineColors["vue-i18n-timeline" /* VueDevToolsIDs.TIMELINE */]
  1706. });
  1707. resolve(true);
  1708. });
  1709. }
  1710. catch (e) {
  1711. console.error(e);
  1712. reject(false);
  1713. }
  1714. });
  1715. }
  1716. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1717. function getI18nScopeLable(instance) {
  1718. return (instance.type.name ||
  1719. instance.type.displayName ||
  1720. instance.type.__file ||
  1721. 'Anonymous');
  1722. }
  1723. function updateComponentTreeTags(instance, // eslint-disable-line @typescript-eslint/no-explicit-any
  1724. treeNode, i18n) {
  1725. // prettier-ignore
  1726. const global = i18n.mode === 'composition'
  1727. ? i18n.global
  1728. : i18n.global.__composer;
  1729. if (instance && instance.vnode.el && instance.vnode.el.__VUE_I18N__) {
  1730. // add custom tags local scope only
  1731. if (instance.vnode.el.__VUE_I18N__ !== global) {
  1732. const tag = {
  1733. label: `i18n (${getI18nScopeLable(instance)} Scope)`,
  1734. textColor: 0x000000,
  1735. backgroundColor: 0xffcd19
  1736. };
  1737. treeNode.tags.push(tag);
  1738. }
  1739. }
  1740. }
  1741. function inspectComposer(instanceData, composer) {
  1742. const type = VUE_I18N_COMPONENT_TYPES;
  1743. instanceData.state.push({
  1744. type,
  1745. key: 'locale',
  1746. editable: true,
  1747. value: composer.locale.value
  1748. });
  1749. instanceData.state.push({
  1750. type,
  1751. key: 'availableLocales',
  1752. editable: false,
  1753. value: composer.availableLocales
  1754. });
  1755. instanceData.state.push({
  1756. type,
  1757. key: 'fallbackLocale',
  1758. editable: true,
  1759. value: composer.fallbackLocale.value
  1760. });
  1761. instanceData.state.push({
  1762. type,
  1763. key: 'inheritLocale',
  1764. editable: true,
  1765. value: composer.inheritLocale
  1766. });
  1767. instanceData.state.push({
  1768. type,
  1769. key: 'messages',
  1770. editable: false,
  1771. value: getLocaleMessageValue(composer.messages.value)
  1772. });
  1773. {
  1774. instanceData.state.push({
  1775. type,
  1776. key: 'datetimeFormats',
  1777. editable: false,
  1778. value: composer.datetimeFormats.value
  1779. });
  1780. instanceData.state.push({
  1781. type,
  1782. key: 'numberFormats',
  1783. editable: false,
  1784. value: composer.numberFormats.value
  1785. });
  1786. }
  1787. }
  1788. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1789. function getLocaleMessageValue(messages) {
  1790. const value = {};
  1791. Object.keys(messages).forEach((key) => {
  1792. const v = messages[key];
  1793. if (isFunction(v) && 'source' in v) {
  1794. value[key] = getMessageFunctionDetails(v);
  1795. }
  1796. else if (isMessageAST(v) && v.loc && v.loc.source) {
  1797. value[key] = v.loc.source;
  1798. }
  1799. else if (isObject(v)) {
  1800. value[key] = getLocaleMessageValue(v);
  1801. }
  1802. else {
  1803. value[key] = v;
  1804. }
  1805. });
  1806. return value;
  1807. }
  1808. const ESC = {
  1809. '<': '&lt;',
  1810. '>': '&gt;',
  1811. '"': '&quot;',
  1812. '&': '&amp;'
  1813. };
  1814. function escape(s) {
  1815. return s.replace(/[<>"&]/g, escapeChar);
  1816. }
  1817. function escapeChar(a) {
  1818. return ESC[a] || a;
  1819. }
  1820. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1821. function getMessageFunctionDetails(func) {
  1822. const argString = func.source ? `("${escape(func.source)}")` : `(?)`;
  1823. return {
  1824. _custom: {
  1825. type: 'function',
  1826. display: `<span>ƒ</span> ${argString}`
  1827. }
  1828. };
  1829. }
  1830. function registerScope(payload, i18n) {
  1831. payload.rootNodes.push({
  1832. id: 'global',
  1833. label: 'Global Scope'
  1834. });
  1835. // prettier-ignore
  1836. const global = i18n.mode === 'composition'
  1837. ? i18n.global
  1838. : i18n.global.__composer;
  1839. for (const [keyInstance, instance] of i18n.__instances) {
  1840. // prettier-ignore
  1841. const composer = i18n.mode === 'composition'
  1842. ? instance
  1843. : instance.__composer;
  1844. if (global === composer) {
  1845. continue;
  1846. }
  1847. payload.rootNodes.push({
  1848. id: composer.id.toString(),
  1849. label: `${getI18nScopeLable(keyInstance)} Scope`
  1850. });
  1851. }
  1852. }
  1853. function getComponentInstance(nodeId, i18n) {
  1854. let instance = null;
  1855. if (nodeId !== 'global') {
  1856. for (const [component, composer] of i18n.__instances.entries()) {
  1857. if (composer.id.toString() === nodeId) {
  1858. instance = component;
  1859. break;
  1860. }
  1861. }
  1862. }
  1863. return instance;
  1864. }
  1865. function getComposer$1(nodeId, i18n) {
  1866. if (nodeId === 'global') {
  1867. return i18n.mode === 'composition'
  1868. ? i18n.global
  1869. : i18n.global.__composer;
  1870. }
  1871. else {
  1872. const instance = Array.from(i18n.__instances.values()).find(item => item.id.toString() === nodeId);
  1873. if (instance) {
  1874. return i18n.mode === 'composition'
  1875. ? instance
  1876. : instance.__composer;
  1877. }
  1878. else {
  1879. return null;
  1880. }
  1881. }
  1882. }
  1883. function inspectScope(payload, i18n
  1884. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1885. ) {
  1886. const composer = getComposer$1(payload.nodeId, i18n);
  1887. if (composer) {
  1888. // TODO:
  1889. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1890. payload.state = makeScopeInspectState(composer);
  1891. }
  1892. return null;
  1893. }
  1894. function makeScopeInspectState(composer) {
  1895. const state = {};
  1896. const localeType = 'Locale related info';
  1897. const localeStates = [
  1898. {
  1899. type: localeType,
  1900. key: 'locale',
  1901. editable: true,
  1902. value: composer.locale.value
  1903. },
  1904. {
  1905. type: localeType,
  1906. key: 'fallbackLocale',
  1907. editable: true,
  1908. value: composer.fallbackLocale.value
  1909. },
  1910. {
  1911. type: localeType,
  1912. key: 'availableLocales',
  1913. editable: false,
  1914. value: composer.availableLocales
  1915. },
  1916. {
  1917. type: localeType,
  1918. key: 'inheritLocale',
  1919. editable: true,
  1920. value: composer.inheritLocale
  1921. }
  1922. ];
  1923. state[localeType] = localeStates;
  1924. const localeMessagesType = 'Locale messages info';
  1925. const localeMessagesStates = [
  1926. {
  1927. type: localeMessagesType,
  1928. key: 'messages',
  1929. editable: false,
  1930. value: getLocaleMessageValue(composer.messages.value)
  1931. }
  1932. ];
  1933. state[localeMessagesType] = localeMessagesStates;
  1934. {
  1935. const datetimeFormatsType = 'Datetime formats info';
  1936. const datetimeFormatsStates = [
  1937. {
  1938. type: datetimeFormatsType,
  1939. key: 'datetimeFormats',
  1940. editable: false,
  1941. value: composer.datetimeFormats.value
  1942. }
  1943. ];
  1944. state[datetimeFormatsType] = datetimeFormatsStates;
  1945. const numberFormatsType = 'Datetime formats info';
  1946. const numberFormatsStates = [
  1947. {
  1948. type: numberFormatsType,
  1949. key: 'numberFormats',
  1950. editable: false,
  1951. value: composer.numberFormats.value
  1952. }
  1953. ];
  1954. state[numberFormatsType] = numberFormatsStates;
  1955. }
  1956. return state;
  1957. }
  1958. function addTimelineEvent(event, payload) {
  1959. if (devtoolsApi) {
  1960. let groupId;
  1961. if (payload && 'groupId' in payload) {
  1962. groupId = payload.groupId;
  1963. delete payload.groupId;
  1964. }
  1965. devtoolsApi.addTimelineEvent({
  1966. layerId: "vue-i18n-timeline" /* VueDevToolsIDs.TIMELINE */,
  1967. event: {
  1968. title: event,
  1969. groupId,
  1970. time: Date.now(),
  1971. meta: {},
  1972. data: payload || {},
  1973. logType: event === "compile-error" /* VueDevToolsTimelineEvents.COMPILE_ERROR */
  1974. ? 'error'
  1975. : event === "fallback" /* VueDevToolsTimelineEvents.FALBACK */ ||
  1976. event === "missing" /* VueDevToolsTimelineEvents.MISSING */
  1977. ? 'warning'
  1978. : 'default'
  1979. }
  1980. });
  1981. }
  1982. }
  1983. function editScope(payload, i18n) {
  1984. const composer = getComposer$1(payload.nodeId, i18n);
  1985. if (composer) {
  1986. const [field] = payload.path;
  1987. if (field === 'locale' && isString(payload.state.value)) {
  1988. composer.locale.value = payload.state.value;
  1989. }
  1990. else if (field === 'fallbackLocale' &&
  1991. (isString(payload.state.value) ||
  1992. isArray(payload.state.value) ||
  1993. isObject(payload.state.value))) {
  1994. composer.fallbackLocale.value = payload.state.value;
  1995. }
  1996. else if (field === 'inheritLocale' && isBoolean(payload.state.value)) {
  1997. composer.inheritLocale = payload.state.value;
  1998. }
  1999. }
  2000. }
  2001. /**
  2002. * Supports compatibility for legacy vue-i18n APIs
  2003. * This mixin is used when we use vue-i18n@v9.x or later
  2004. */
  2005. function defineMixin(vuei18n, composer, i18n) {
  2006. return {
  2007. beforeCreate() {
  2008. const instance = getCurrentInstance();
  2009. /* istanbul ignore if */
  2010. if (!instance) {
  2011. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  2012. }
  2013. const options = this.$options;
  2014. if (options.i18n) {
  2015. const optionsI18n = options.i18n;
  2016. if (options.__i18n) {
  2017. optionsI18n.__i18n = options.__i18n;
  2018. }
  2019. optionsI18n.__root = composer;
  2020. if (this === this.$root) {
  2021. // merge option and gttach global
  2022. this.$i18n = mergeToGlobal(vuei18n, optionsI18n);
  2023. }
  2024. else {
  2025. optionsI18n.__injectWithOption = true;
  2026. optionsI18n.__extender = i18n.__vueI18nExtend;
  2027. // atttach local VueI18n instance
  2028. this.$i18n = createVueI18n(optionsI18n);
  2029. // extend VueI18n instance
  2030. const _vueI18n = this.$i18n;
  2031. if (_vueI18n.__extender) {
  2032. _vueI18n.__disposer = _vueI18n.__extender(this.$i18n);
  2033. }
  2034. }
  2035. }
  2036. else if (options.__i18n) {
  2037. if (this === this.$root) {
  2038. // merge option and gttach global
  2039. this.$i18n = mergeToGlobal(vuei18n, options);
  2040. }
  2041. else {
  2042. // atttach local VueI18n instance
  2043. this.$i18n = createVueI18n({
  2044. __i18n: options.__i18n,
  2045. __injectWithOption: true,
  2046. __extender: i18n.__vueI18nExtend,
  2047. __root: composer
  2048. });
  2049. // extend VueI18n instance
  2050. const _vueI18n = this.$i18n;
  2051. if (_vueI18n.__extender) {
  2052. _vueI18n.__disposer = _vueI18n.__extender(this.$i18n);
  2053. }
  2054. }
  2055. }
  2056. else {
  2057. // attach global VueI18n instance
  2058. this.$i18n = vuei18n;
  2059. }
  2060. if (options.__i18nGlobal) {
  2061. adjustI18nResources(composer, options, options);
  2062. }
  2063. // defines vue-i18n legacy APIs
  2064. this.$t = (...args) => this.$i18n.t(...args);
  2065. this.$rt = (...args) => this.$i18n.rt(...args);
  2066. this.$tc = (...args) => this.$i18n.tc(...args);
  2067. this.$te = (key, locale) => this.$i18n.te(key, locale);
  2068. this.$d = (...args) => this.$i18n.d(...args);
  2069. this.$n = (...args) => this.$i18n.n(...args);
  2070. this.$tm = (key) => this.$i18n.tm(key);
  2071. i18n.__setInstance(instance, this.$i18n);
  2072. },
  2073. mounted() {
  2074. /* istanbul ignore if */
  2075. if (((process.env.NODE_ENV !== 'production') || __VUE_PROD_DEVTOOLS__) &&
  2076. !false &&
  2077. this.$el &&
  2078. this.$i18n) {
  2079. const _vueI18n = this.$i18n;
  2080. this.$el.__VUE_I18N__ = _vueI18n.__composer;
  2081. const emitter = (this.__v_emitter =
  2082. createEmitter());
  2083. _vueI18n.__enableEmitter && _vueI18n.__enableEmitter(emitter);
  2084. emitter.on('*', addTimelineEvent);
  2085. }
  2086. },
  2087. unmounted() {
  2088. const instance = getCurrentInstance();
  2089. /* istanbul ignore if */
  2090. if (!instance) {
  2091. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  2092. }
  2093. const _vueI18n = this.$i18n;
  2094. /* istanbul ignore if */
  2095. if (((process.env.NODE_ENV !== 'production') || __VUE_PROD_DEVTOOLS__) &&
  2096. !false &&
  2097. this.$el &&
  2098. this.$el.__VUE_I18N__) {
  2099. if (this.__v_emitter) {
  2100. this.__v_emitter.off('*', addTimelineEvent);
  2101. delete this.__v_emitter;
  2102. }
  2103. if (this.$i18n) {
  2104. _vueI18n.__disableEmitter && _vueI18n.__disableEmitter();
  2105. delete this.$el.__VUE_I18N__;
  2106. }
  2107. }
  2108. delete this.$t;
  2109. delete this.$rt;
  2110. delete this.$tc;
  2111. delete this.$te;
  2112. delete this.$d;
  2113. delete this.$n;
  2114. delete this.$tm;
  2115. if (_vueI18n.__disposer) {
  2116. _vueI18n.__disposer();
  2117. delete _vueI18n.__disposer;
  2118. delete _vueI18n.__extender;
  2119. }
  2120. i18n.__deleteInstance(instance);
  2121. delete this.$i18n;
  2122. }
  2123. };
  2124. }
  2125. function mergeToGlobal(g, options) {
  2126. g.locale = options.locale || g.locale;
  2127. g.fallbackLocale = options.fallbackLocale || g.fallbackLocale;
  2128. g.missing = options.missing || g.missing;
  2129. g.silentTranslationWarn =
  2130. options.silentTranslationWarn || g.silentFallbackWarn;
  2131. g.silentFallbackWarn = options.silentFallbackWarn || g.silentFallbackWarn;
  2132. g.formatFallbackMessages =
  2133. options.formatFallbackMessages || g.formatFallbackMessages;
  2134. g.postTranslation = options.postTranslation || g.postTranslation;
  2135. g.warnHtmlInMessage = options.warnHtmlInMessage || g.warnHtmlInMessage;
  2136. g.escapeParameterHtml = options.escapeParameterHtml || g.escapeParameterHtml;
  2137. g.sync = options.sync || g.sync;
  2138. g.__composer[SetPluralRulesSymbol](options.pluralizationRules || g.pluralizationRules);
  2139. const messages = getLocaleMessages(g.locale, {
  2140. messages: options.messages,
  2141. __i18n: options.__i18n
  2142. });
  2143. Object.keys(messages).forEach(locale => g.mergeLocaleMessage(locale, messages[locale]));
  2144. if (options.datetimeFormats) {
  2145. Object.keys(options.datetimeFormats).forEach(locale => g.mergeDateTimeFormat(locale, options.datetimeFormats[locale]));
  2146. }
  2147. if (options.numberFormats) {
  2148. Object.keys(options.numberFormats).forEach(locale => g.mergeNumberFormat(locale, options.numberFormats[locale]));
  2149. }
  2150. return g;
  2151. }
  2152. /**
  2153. * Injection key for {@link useI18n}
  2154. *
  2155. * @remarks
  2156. * The global injection key for I18n instances with `useI18n`. this injection key is used in Web Components.
  2157. * Specify the i18n instance created by {@link createI18n} together with `provide` function.
  2158. *
  2159. * @VueI18nGeneral
  2160. */
  2161. const I18nInjectionKey =
  2162. /* #__PURE__*/ makeSymbol('global-vue-i18n');
  2163. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
  2164. function createI18n(options = {}, VueI18nLegacy) {
  2165. // prettier-ignore
  2166. const __legacyMode = __VUE_I18N_LEGACY_API__ && isBoolean(options.legacy)
  2167. ? options.legacy
  2168. : __VUE_I18N_LEGACY_API__;
  2169. // prettier-ignore
  2170. const __globalInjection = isBoolean(options.globalInjection)
  2171. ? options.globalInjection
  2172. : true;
  2173. // prettier-ignore
  2174. const __allowComposition = __VUE_I18N_LEGACY_API__ && __legacyMode
  2175. ? !!options.allowComposition
  2176. : true;
  2177. const __instances = new Map();
  2178. const [globalScope, __global] = createGlobal(options, __legacyMode);
  2179. const symbol = /* #__PURE__*/ makeSymbol((process.env.NODE_ENV !== 'production') ? 'vue-i18n' : '');
  2180. if ((process.env.NODE_ENV !== 'production')) {
  2181. if (__legacyMode && __allowComposition && !false) {
  2182. warn(getWarnMessage(I18nWarnCodes.NOTICE_DROP_ALLOW_COMPOSITION));
  2183. }
  2184. }
  2185. function __getInstance(component) {
  2186. return __instances.get(component) || null;
  2187. }
  2188. function __setInstance(component, instance) {
  2189. __instances.set(component, instance);
  2190. }
  2191. function __deleteInstance(component) {
  2192. __instances.delete(component);
  2193. }
  2194. {
  2195. const i18n = {
  2196. // mode
  2197. get mode() {
  2198. return __VUE_I18N_LEGACY_API__ && __legacyMode
  2199. ? 'legacy'
  2200. : 'composition';
  2201. },
  2202. // allowComposition
  2203. get allowComposition() {
  2204. return __allowComposition;
  2205. },
  2206. // install plugin
  2207. async install(app, ...options) {
  2208. if (((process.env.NODE_ENV !== 'production') || __VUE_PROD_DEVTOOLS__) &&
  2209. !false) {
  2210. app.__VUE_I18N__ = i18n;
  2211. }
  2212. // setup global provider
  2213. app.__VUE_I18N_SYMBOL__ = symbol;
  2214. app.provide(app.__VUE_I18N_SYMBOL__, i18n);
  2215. // set composer & vuei18n extend hook options from plugin options
  2216. if (isPlainObject(options[0])) {
  2217. const opts = options[0];
  2218. i18n.__composerExtend =
  2219. opts.__composerExtend;
  2220. i18n.__vueI18nExtend =
  2221. opts.__vueI18nExtend;
  2222. }
  2223. // global method and properties injection for Composition API
  2224. let globalReleaseHandler = null;
  2225. if (!__legacyMode && __globalInjection) {
  2226. globalReleaseHandler = injectGlobalFields(app, i18n.global);
  2227. }
  2228. // install built-in components and directive
  2229. if (__VUE_I18N_FULL_INSTALL__) {
  2230. apply(app, i18n, ...options);
  2231. }
  2232. // setup mixin for Legacy API
  2233. if (__VUE_I18N_LEGACY_API__ && __legacyMode) {
  2234. app.mixin(defineMixin(__global, __global.__composer, i18n));
  2235. }
  2236. // release global scope
  2237. const unmountApp = app.unmount;
  2238. app.unmount = () => {
  2239. globalReleaseHandler && globalReleaseHandler();
  2240. i18n.dispose();
  2241. unmountApp();
  2242. };
  2243. // setup vue-devtools plugin
  2244. if (((process.env.NODE_ENV !== 'production') || __VUE_PROD_DEVTOOLS__) && !false) {
  2245. const ret = await enableDevTools(app, i18n);
  2246. if (!ret) {
  2247. throw createI18nError(I18nErrorCodes.CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN);
  2248. }
  2249. const emitter = createEmitter();
  2250. if (__legacyMode) {
  2251. const _vueI18n = __global;
  2252. _vueI18n.__enableEmitter && _vueI18n.__enableEmitter(emitter);
  2253. }
  2254. else {
  2255. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2256. const _composer = __global;
  2257. _composer[EnableEmitter] && _composer[EnableEmitter](emitter);
  2258. }
  2259. emitter.on('*', addTimelineEvent);
  2260. }
  2261. },
  2262. // global accessor
  2263. get global() {
  2264. return __global;
  2265. },
  2266. dispose() {
  2267. globalScope.stop();
  2268. },
  2269. // @internal
  2270. __instances,
  2271. // @internal
  2272. __getInstance,
  2273. // @internal
  2274. __setInstance,
  2275. // @internal
  2276. __deleteInstance
  2277. };
  2278. return i18n;
  2279. }
  2280. }
  2281. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  2282. function useI18n(options = {}) {
  2283. const instance = getCurrentInstance();
  2284. if (instance == null) {
  2285. throw createI18nError(I18nErrorCodes.MUST_BE_CALL_SETUP_TOP);
  2286. }
  2287. if (!instance.isCE &&
  2288. instance.appContext.app != null &&
  2289. !instance.appContext.app.__VUE_I18N_SYMBOL__) {
  2290. throw createI18nError(I18nErrorCodes.NOT_INSTALLED);
  2291. }
  2292. const i18n = getI18nInstance(instance);
  2293. const gl = getGlobalComposer(i18n);
  2294. const componentOptions = getComponentOptions(instance);
  2295. const scope = getScope(options, componentOptions);
  2296. if (__VUE_I18N_LEGACY_API__) {
  2297. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2298. if (i18n.mode === 'legacy' && !options.__useComponent) {
  2299. if (!i18n.allowComposition) {
  2300. throw createI18nError(I18nErrorCodes.NOT_AVAILABLE_IN_LEGACY_MODE);
  2301. }
  2302. return useI18nForLegacy(instance, scope, gl, options);
  2303. }
  2304. }
  2305. if (scope === 'global') {
  2306. adjustI18nResources(gl, options, componentOptions);
  2307. return gl;
  2308. }
  2309. if (scope === 'parent') {
  2310. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2311. let composer = getComposer(i18n, instance, options.__useComponent);
  2312. if (composer == null) {
  2313. if ((process.env.NODE_ENV !== 'production')) {
  2314. warn(getWarnMessage(I18nWarnCodes.NOT_FOUND_PARENT_SCOPE));
  2315. }
  2316. composer = gl;
  2317. }
  2318. return composer;
  2319. }
  2320. const i18nInternal = i18n;
  2321. let composer = i18nInternal.__getInstance(instance);
  2322. if (composer == null) {
  2323. const composerOptions = assign({}, options);
  2324. if ('__i18n' in componentOptions) {
  2325. composerOptions.__i18n = componentOptions.__i18n;
  2326. }
  2327. if (gl) {
  2328. composerOptions.__root = gl;
  2329. }
  2330. composer = createComposer(composerOptions);
  2331. if (i18nInternal.__composerExtend) {
  2332. composer[DisposeSymbol] =
  2333. i18nInternal.__composerExtend(composer);
  2334. }
  2335. setupLifeCycle(i18nInternal, instance, composer);
  2336. i18nInternal.__setInstance(instance, composer);
  2337. }
  2338. return composer;
  2339. }
  2340. /**
  2341. * Cast to VueI18n legacy compatible type
  2342. *
  2343. * @remarks
  2344. * This API is provided only with [vue-i18n-bridge](https://vue-i18n.intlify.dev/guide/migration/ways.html#what-is-vue-i18n-bridge).
  2345. *
  2346. * 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.
  2347. *
  2348. * @param i18n - An instance of {@link I18n}
  2349. * @returns A i18n instance which is casted to {@link VueI18n} type
  2350. *
  2351. * @VueI18nTip
  2352. * :new: provided by **vue-i18n-bridge only**
  2353. *
  2354. * @VueI18nGeneral
  2355. */
  2356. /* #__NO_SIDE_EFFECTS__ */
  2357. const castToVueI18n = (i18n
  2358. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2359. ) => {
  2360. if (!(__VUE_I18N_BRIDGE__ in i18n)) {
  2361. throw createI18nError(I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N);
  2362. }
  2363. return i18n;
  2364. };
  2365. function createGlobal(options, legacyMode, VueI18nLegacy // eslint-disable-line @typescript-eslint/no-explicit-any
  2366. ) {
  2367. const scope = effectScope();
  2368. {
  2369. const obj = __VUE_I18N_LEGACY_API__ && legacyMode
  2370. ? scope.run(() => createVueI18n(options))
  2371. : scope.run(() => createComposer(options));
  2372. if (obj == null) {
  2373. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  2374. }
  2375. return [scope, obj];
  2376. }
  2377. }
  2378. function getI18nInstance(instance) {
  2379. {
  2380. const i18n = inject(!instance.isCE
  2381. ? instance.appContext.app.__VUE_I18N_SYMBOL__
  2382. : I18nInjectionKey);
  2383. /* istanbul ignore if */
  2384. if (!i18n) {
  2385. throw createI18nError(!instance.isCE
  2386. ? I18nErrorCodes.UNEXPECTED_ERROR
  2387. : I18nErrorCodes.NOT_INSTALLED_WITH_PROVIDE);
  2388. }
  2389. return i18n;
  2390. }
  2391. }
  2392. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2393. function getScope(options, componentOptions) {
  2394. // prettier-ignore
  2395. return isEmptyObject(options)
  2396. ? ('__i18n' in componentOptions)
  2397. ? 'local'
  2398. : 'global'
  2399. : !options.useScope
  2400. ? 'local'
  2401. : options.useScope;
  2402. }
  2403. function getGlobalComposer(i18n) {
  2404. // prettier-ignore
  2405. return i18n.mode === 'composition'
  2406. ? i18n.global
  2407. : i18n.global.__composer
  2408. ;
  2409. }
  2410. function getComposer(i18n, target, useComponent = false) {
  2411. let composer = null;
  2412. const root = target.root;
  2413. let current = getParentComponentInstance(target, useComponent);
  2414. while (current != null) {
  2415. const i18nInternal = i18n;
  2416. if (i18n.mode === 'composition') {
  2417. composer = i18nInternal.__getInstance(current);
  2418. }
  2419. else {
  2420. if (__VUE_I18N_LEGACY_API__) {
  2421. const vueI18n = i18nInternal.__getInstance(current);
  2422. if (vueI18n != null) {
  2423. composer = vueI18n
  2424. .__composer;
  2425. if (useComponent &&
  2426. composer &&
  2427. !composer[InejctWithOptionSymbol] // eslint-disable-line @typescript-eslint/no-explicit-any
  2428. ) {
  2429. composer = null;
  2430. }
  2431. }
  2432. }
  2433. }
  2434. if (composer != null) {
  2435. break;
  2436. }
  2437. if (root === current) {
  2438. break;
  2439. }
  2440. current = current.parent;
  2441. }
  2442. return composer;
  2443. }
  2444. function getParentComponentInstance(target, useComponent = false) {
  2445. if (target == null) {
  2446. return null;
  2447. }
  2448. {
  2449. // if `useComponent: true` will be specified, we get lexical scope owner instance for use-case slots
  2450. return !useComponent
  2451. ? target.parent
  2452. : target.vnode.ctx || target.parent; // eslint-disable-line @typescript-eslint/no-explicit-any
  2453. }
  2454. }
  2455. function setupLifeCycle(i18n, target, composer) {
  2456. let emitter = null;
  2457. {
  2458. onMounted(() => {
  2459. // inject composer instance to DOM for intlify-devtools
  2460. if (((process.env.NODE_ENV !== 'production') || __VUE_PROD_DEVTOOLS__) &&
  2461. !false &&
  2462. target.vnode.el) {
  2463. target.vnode.el.__VUE_I18N__ = composer;
  2464. emitter = createEmitter();
  2465. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2466. const _composer = composer;
  2467. _composer[EnableEmitter] && _composer[EnableEmitter](emitter);
  2468. emitter.on('*', addTimelineEvent);
  2469. }
  2470. }, target);
  2471. onUnmounted(() => {
  2472. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2473. const _composer = composer;
  2474. // remove composer instance from DOM for intlify-devtools
  2475. if (((process.env.NODE_ENV !== 'production') || __VUE_PROD_DEVTOOLS__) &&
  2476. !false &&
  2477. target.vnode.el &&
  2478. target.vnode.el.__VUE_I18N__) {
  2479. emitter && emitter.off('*', addTimelineEvent);
  2480. _composer[DisableEmitter] && _composer[DisableEmitter]();
  2481. delete target.vnode.el.__VUE_I18N__;
  2482. }
  2483. i18n.__deleteInstance(target);
  2484. // dispose extended resources
  2485. const dispose = _composer[DisposeSymbol];
  2486. if (dispose) {
  2487. dispose();
  2488. delete _composer[DisposeSymbol];
  2489. }
  2490. }, target);
  2491. }
  2492. }
  2493. function useI18nForLegacy(instance, scope, root, options = {} // eslint-disable-line @typescript-eslint/no-explicit-any
  2494. ) {
  2495. const isLocalScope = scope === 'local';
  2496. const _composer = shallowRef(null);
  2497. if (isLocalScope &&
  2498. instance.proxy &&
  2499. !(instance.proxy.$options.i18n || instance.proxy.$options.__i18n)) {
  2500. throw createI18nError(I18nErrorCodes.MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION);
  2501. }
  2502. const _inheritLocale = isBoolean(options.inheritLocale)
  2503. ? options.inheritLocale
  2504. : !isString(options.locale);
  2505. const _locale = ref(
  2506. // prettier-ignore
  2507. !isLocalScope || _inheritLocale
  2508. ? root.locale.value
  2509. : isString(options.locale)
  2510. ? options.locale
  2511. : DEFAULT_LOCALE);
  2512. const _fallbackLocale = ref(
  2513. // prettier-ignore
  2514. !isLocalScope || _inheritLocale
  2515. ? root.fallbackLocale.value
  2516. : isString(options.fallbackLocale) ||
  2517. isArray(options.fallbackLocale) ||
  2518. isPlainObject(options.fallbackLocale) ||
  2519. options.fallbackLocale === false
  2520. ? options.fallbackLocale
  2521. : _locale.value);
  2522. const _messages = ref(getLocaleMessages(_locale.value, options));
  2523. // prettier-ignore
  2524. const _datetimeFormats = ref(isPlainObject(options.datetimeFormats)
  2525. ? options.datetimeFormats
  2526. : { [_locale.value]: {} });
  2527. // prettier-ignore
  2528. const _numberFormats = ref(isPlainObject(options.numberFormats)
  2529. ? options.numberFormats
  2530. : { [_locale.value]: {} });
  2531. // prettier-ignore
  2532. const _missingWarn = isLocalScope
  2533. ? root.missingWarn
  2534. : isBoolean(options.missingWarn) || isRegExp(options.missingWarn)
  2535. ? options.missingWarn
  2536. : true;
  2537. // prettier-ignore
  2538. const _fallbackWarn = isLocalScope
  2539. ? root.fallbackWarn
  2540. : isBoolean(options.fallbackWarn) || isRegExp(options.fallbackWarn)
  2541. ? options.fallbackWarn
  2542. : true;
  2543. // prettier-ignore
  2544. const _fallbackRoot = isLocalScope
  2545. ? root.fallbackRoot
  2546. : isBoolean(options.fallbackRoot)
  2547. ? options.fallbackRoot
  2548. : true;
  2549. // configure fall back to root
  2550. const _fallbackFormat = !!options.fallbackFormat;
  2551. // runtime missing
  2552. const _missing = isFunction(options.missing) ? options.missing : null;
  2553. // postTranslation handler
  2554. const _postTranslation = isFunction(options.postTranslation)
  2555. ? options.postTranslation
  2556. : null;
  2557. // prettier-ignore
  2558. const _warnHtmlMessage = isLocalScope
  2559. ? root.warnHtmlMessage
  2560. : isBoolean(options.warnHtmlMessage)
  2561. ? options.warnHtmlMessage
  2562. : true;
  2563. const _escapeParameter = !!options.escapeParameter;
  2564. // prettier-ignore
  2565. const _modifiers = isLocalScope
  2566. ? root.modifiers
  2567. : isPlainObject(options.modifiers)
  2568. ? options.modifiers
  2569. : {};
  2570. // pluralRules
  2571. const _pluralRules = options.pluralRules || (isLocalScope && root.pluralRules);
  2572. // track reactivity
  2573. function trackReactivityValues() {
  2574. return [
  2575. _locale.value,
  2576. _fallbackLocale.value,
  2577. _messages.value,
  2578. _datetimeFormats.value,
  2579. _numberFormats.value
  2580. ];
  2581. }
  2582. // locale
  2583. const locale = computed({
  2584. get: () => {
  2585. return _composer.value ? _composer.value.locale.value : _locale.value;
  2586. },
  2587. set: val => {
  2588. if (_composer.value) {
  2589. _composer.value.locale.value = val;
  2590. }
  2591. _locale.value = val;
  2592. }
  2593. });
  2594. // fallbackLocale
  2595. const fallbackLocale = computed({
  2596. get: () => {
  2597. return _composer.value
  2598. ? _composer.value.fallbackLocale.value
  2599. : _fallbackLocale.value;
  2600. },
  2601. set: val => {
  2602. if (_composer.value) {
  2603. _composer.value.fallbackLocale.value = val;
  2604. }
  2605. _fallbackLocale.value = val;
  2606. }
  2607. });
  2608. // messages
  2609. const messages = computed(() => {
  2610. if (_composer.value) {
  2611. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2612. return _composer.value.messages.value;
  2613. }
  2614. else {
  2615. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2616. return _messages.value;
  2617. }
  2618. });
  2619. const datetimeFormats = computed(() => _datetimeFormats.value);
  2620. const numberFormats = computed(() => _numberFormats.value);
  2621. function getPostTranslationHandler() {
  2622. return _composer.value
  2623. ? _composer.value.getPostTranslationHandler()
  2624. : _postTranslation;
  2625. }
  2626. function setPostTranslationHandler(handler) {
  2627. if (_composer.value) {
  2628. _composer.value.setPostTranslationHandler(handler);
  2629. }
  2630. }
  2631. function getMissingHandler() {
  2632. return _composer.value ? _composer.value.getMissingHandler() : _missing;
  2633. }
  2634. function setMissingHandler(handler) {
  2635. if (_composer.value) {
  2636. _composer.value.setMissingHandler(handler);
  2637. }
  2638. }
  2639. function warpWithDeps(fn) {
  2640. trackReactivityValues();
  2641. return fn();
  2642. }
  2643. function t(...args) {
  2644. return _composer.value
  2645. ? warpWithDeps(() => Reflect.apply(_composer.value.t, null, [...args]))
  2646. : warpWithDeps(() => '');
  2647. }
  2648. function rt(...args) {
  2649. return _composer.value
  2650. ? Reflect.apply(_composer.value.rt, null, [...args])
  2651. : '';
  2652. }
  2653. function d(...args) {
  2654. return _composer.value
  2655. ? warpWithDeps(() => Reflect.apply(_composer.value.d, null, [...args]))
  2656. : warpWithDeps(() => '');
  2657. }
  2658. function n(...args) {
  2659. return _composer.value
  2660. ? warpWithDeps(() => Reflect.apply(_composer.value.n, null, [...args]))
  2661. : warpWithDeps(() => '');
  2662. }
  2663. function tm(key) {
  2664. return _composer.value ? _composer.value.tm(key) : {};
  2665. }
  2666. function te(key, locale) {
  2667. return _composer.value ? _composer.value.te(key, locale) : false;
  2668. }
  2669. function getLocaleMessage(locale) {
  2670. return _composer.value ? _composer.value.getLocaleMessage(locale) : {};
  2671. }
  2672. function setLocaleMessage(locale, message) {
  2673. if (_composer.value) {
  2674. _composer.value.setLocaleMessage(locale, message);
  2675. _messages.value[locale] = message;
  2676. }
  2677. }
  2678. function mergeLocaleMessage(locale, message) {
  2679. if (_composer.value) {
  2680. _composer.value.mergeLocaleMessage(locale, message);
  2681. }
  2682. }
  2683. function getDateTimeFormat(locale) {
  2684. return _composer.value ? _composer.value.getDateTimeFormat(locale) : {};
  2685. }
  2686. function setDateTimeFormat(locale, format) {
  2687. if (_composer.value) {
  2688. _composer.value.setDateTimeFormat(locale, format);
  2689. _datetimeFormats.value[locale] = format;
  2690. }
  2691. }
  2692. function mergeDateTimeFormat(locale, format) {
  2693. if (_composer.value) {
  2694. _composer.value.mergeDateTimeFormat(locale, format);
  2695. }
  2696. }
  2697. function getNumberFormat(locale) {
  2698. return _composer.value ? _composer.value.getNumberFormat(locale) : {};
  2699. }
  2700. function setNumberFormat(locale, format) {
  2701. if (_composer.value) {
  2702. _composer.value.setNumberFormat(locale, format);
  2703. _numberFormats.value[locale] = format;
  2704. }
  2705. }
  2706. function mergeNumberFormat(locale, format) {
  2707. if (_composer.value) {
  2708. _composer.value.mergeNumberFormat(locale, format);
  2709. }
  2710. }
  2711. const wrapper = {
  2712. get id() {
  2713. return _composer.value ? _composer.value.id : -1;
  2714. },
  2715. locale,
  2716. fallbackLocale,
  2717. messages,
  2718. datetimeFormats,
  2719. numberFormats,
  2720. get inheritLocale() {
  2721. return _composer.value ? _composer.value.inheritLocale : _inheritLocale;
  2722. },
  2723. set inheritLocale(val) {
  2724. if (_composer.value) {
  2725. _composer.value.inheritLocale = val;
  2726. }
  2727. },
  2728. get availableLocales() {
  2729. return _composer.value
  2730. ? _composer.value.availableLocales
  2731. : Object.keys(_messages.value);
  2732. },
  2733. get modifiers() {
  2734. return (_composer.value ? _composer.value.modifiers : _modifiers);
  2735. },
  2736. get pluralRules() {
  2737. return (_composer.value ? _composer.value.pluralRules : _pluralRules);
  2738. },
  2739. get isGlobal() {
  2740. return _composer.value ? _composer.value.isGlobal : false;
  2741. },
  2742. get missingWarn() {
  2743. return _composer.value ? _composer.value.missingWarn : _missingWarn;
  2744. },
  2745. set missingWarn(val) {
  2746. if (_composer.value) {
  2747. _composer.value.missingWarn = val;
  2748. }
  2749. },
  2750. get fallbackWarn() {
  2751. return _composer.value ? _composer.value.fallbackWarn : _fallbackWarn;
  2752. },
  2753. set fallbackWarn(val) {
  2754. if (_composer.value) {
  2755. _composer.value.missingWarn = val;
  2756. }
  2757. },
  2758. get fallbackRoot() {
  2759. return _composer.value ? _composer.value.fallbackRoot : _fallbackRoot;
  2760. },
  2761. set fallbackRoot(val) {
  2762. if (_composer.value) {
  2763. _composer.value.fallbackRoot = val;
  2764. }
  2765. },
  2766. get fallbackFormat() {
  2767. return _composer.value ? _composer.value.fallbackFormat : _fallbackFormat;
  2768. },
  2769. set fallbackFormat(val) {
  2770. if (_composer.value) {
  2771. _composer.value.fallbackFormat = val;
  2772. }
  2773. },
  2774. get warnHtmlMessage() {
  2775. return _composer.value
  2776. ? _composer.value.warnHtmlMessage
  2777. : _warnHtmlMessage;
  2778. },
  2779. set warnHtmlMessage(val) {
  2780. if (_composer.value) {
  2781. _composer.value.warnHtmlMessage = val;
  2782. }
  2783. },
  2784. get escapeParameter() {
  2785. return _composer.value
  2786. ? _composer.value.escapeParameter
  2787. : _escapeParameter;
  2788. },
  2789. set escapeParameter(val) {
  2790. if (_composer.value) {
  2791. _composer.value.escapeParameter = val;
  2792. }
  2793. },
  2794. t,
  2795. getPostTranslationHandler,
  2796. setPostTranslationHandler,
  2797. getMissingHandler,
  2798. setMissingHandler,
  2799. rt,
  2800. d,
  2801. n,
  2802. tm,
  2803. te,
  2804. getLocaleMessage,
  2805. setLocaleMessage,
  2806. mergeLocaleMessage,
  2807. getDateTimeFormat,
  2808. setDateTimeFormat,
  2809. mergeDateTimeFormat,
  2810. getNumberFormat,
  2811. setNumberFormat,
  2812. mergeNumberFormat
  2813. };
  2814. function sync(composer) {
  2815. composer.locale.value = _locale.value;
  2816. composer.fallbackLocale.value = _fallbackLocale.value;
  2817. Object.keys(_messages.value).forEach(locale => {
  2818. composer.mergeLocaleMessage(locale, _messages.value[locale]);
  2819. });
  2820. Object.keys(_datetimeFormats.value).forEach(locale => {
  2821. composer.mergeDateTimeFormat(locale, _datetimeFormats.value[locale]);
  2822. });
  2823. Object.keys(_numberFormats.value).forEach(locale => {
  2824. composer.mergeNumberFormat(locale, _numberFormats.value[locale]);
  2825. });
  2826. composer.escapeParameter = _escapeParameter;
  2827. composer.fallbackFormat = _fallbackFormat;
  2828. composer.fallbackRoot = _fallbackRoot;
  2829. composer.fallbackWarn = _fallbackWarn;
  2830. composer.missingWarn = _missingWarn;
  2831. composer.warnHtmlMessage = _warnHtmlMessage;
  2832. }
  2833. onBeforeMount(() => {
  2834. if (instance.proxy == null || instance.proxy.$i18n == null) {
  2835. throw createI18nError(I18nErrorCodes.NOT_AVAILABLE_COMPOSITION_IN_LEGACY);
  2836. }
  2837. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2838. const composer = (_composer.value = instance.proxy.$i18n
  2839. .__composer);
  2840. if (scope === 'global') {
  2841. _locale.value = composer.locale.value;
  2842. _fallbackLocale.value = composer.fallbackLocale.value;
  2843. _messages.value = composer.messages.value;
  2844. _datetimeFormats.value = composer.datetimeFormats.value;
  2845. _numberFormats.value = composer.numberFormats.value;
  2846. }
  2847. else if (isLocalScope) {
  2848. sync(composer);
  2849. }
  2850. });
  2851. return wrapper;
  2852. }
  2853. const globalExportProps = [
  2854. 'locale',
  2855. 'fallbackLocale',
  2856. 'availableLocales'
  2857. ];
  2858. const globalExportMethods = ['t', 'rt', 'd', 'n', 'tm', 'te']
  2859. ;
  2860. function injectGlobalFields(app, composer) {
  2861. const i18n = Object.create(null);
  2862. globalExportProps.forEach(prop => {
  2863. const desc = Object.getOwnPropertyDescriptor(composer, prop);
  2864. if (!desc) {
  2865. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  2866. }
  2867. const wrap = isRef(desc.value) // check computed props
  2868. ? {
  2869. get() {
  2870. return desc.value.value;
  2871. },
  2872. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2873. set(val) {
  2874. desc.value.value = val;
  2875. }
  2876. }
  2877. : {
  2878. get() {
  2879. return desc.get && desc.get();
  2880. }
  2881. };
  2882. Object.defineProperty(i18n, prop, wrap);
  2883. });
  2884. app.config.globalProperties.$i18n = i18n;
  2885. globalExportMethods.forEach(method => {
  2886. const desc = Object.getOwnPropertyDescriptor(composer, method);
  2887. if (!desc || !desc.value) {
  2888. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  2889. }
  2890. Object.defineProperty(app.config.globalProperties, `$${method}`, desc);
  2891. });
  2892. const dispose = () => {
  2893. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2894. delete app.config.globalProperties.$i18n;
  2895. globalExportMethods.forEach(method => {
  2896. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2897. delete app.config.globalProperties[`$${method}`];
  2898. });
  2899. };
  2900. return dispose;
  2901. }
  2902. {
  2903. initFeatureFlags();
  2904. }
  2905. // register message compiler for jit compilation
  2906. if (__INTLIFY_JIT_COMPILATION__) {
  2907. registerMessageCompiler(compile);
  2908. }
  2909. // register message resolver at vue-i18n
  2910. registerMessageResolver(resolveValue);
  2911. // register fallback locale at vue-i18n
  2912. registerLocaleFallbacker(fallbackWithLocaleChain);
  2913. // NOTE: experimental !!
  2914. if ((process.env.NODE_ENV !== 'production') || __INTLIFY_PROD_DEVTOOLS__) {
  2915. const target = getGlobalThis();
  2916. target.__INTLIFY__ = true;
  2917. setDevToolsHook(target.__INTLIFY_DEVTOOLS_GLOBAL_HOOK__);
  2918. }
  2919. if ((process.env.NODE_ENV !== 'production')) ;
  2920. export { DatetimeFormat, I18nD, I18nInjectionKey, I18nN, I18nT, NumberFormat, Translation, VERSION, castToVueI18n, createI18n, useI18n, vTDirective };