vue-i18n.cjs 88 KB

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