vue-i18n.runtime.mjs 104 KB

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