e84eaece1061846067e5235e212e71d4af833bea0ac924c49b74ce7e79d1e0ed53479aeaf2f52c247bbafb9b7c74dcfb056b6c33d9b60beeea3d463f82afff 82 KB

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