vue-i18n.cjs 87 KB

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