vue-i18n.runtime.mjs 105 KB

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