vue-i18n.prod.cjs 81 KB

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