vue-i18n.cjs 87 KB

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