vue-i18n.prod.cjs 80 KB

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