vue-i18n.cjs.prod.js 76 KB

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