vue-i18n.cjs.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. /*!
  2. * vue-i18n v9.1.9
  3. * (c) 2021 kazuya kawaguchi
  4. * Released under the MIT License.
  5. */
  6. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. var shared = require('@intlify/shared');
  9. var coreBase = require('@intlify/core-base');
  10. var vue = require('vue');
  11. /**
  12. * Vue I18n Version
  13. *
  14. * @remarks
  15. * Semver format. Same format as the package.json `version` field.
  16. *
  17. * @VueI18nGeneral
  18. */
  19. const VERSION = '9.1.9';
  20. const warnMessages = {
  21. [6 /* FALLBACK_TO_ROOT */]: `Fall back to {type} '{key}' with root locale.`,
  22. [7 /* NOT_SUPPORTED_PRESERVE */]: `Not supported 'preserve'.`,
  23. [8 /* NOT_SUPPORTED_FORMATTER */]: `Not supported 'formatter'.`,
  24. [9 /* NOT_SUPPORTED_PRESERVE_DIRECTIVE */]: `Not supported 'preserveDirectiveContent'.`,
  25. [10 /* NOT_SUPPORTED_GET_CHOICE_INDEX */]: `Not supported 'getChoiceIndex'.`,
  26. [11 /* COMPONENT_NAME_LEGACY_COMPATIBLE */]: `Component name legacy compatible: '{name}' -> 'i18n'`,
  27. [12 /* NOT_FOUND_PARENT_SCOPE */]: `Not found parent scope. use the global scope.`
  28. };
  29. function getWarnMessage(code, ...args) {
  30. return shared.format(warnMessages[code], ...args);
  31. }
  32. function createI18nError(code, ...args) {
  33. return coreBase.createCompileError(code, null, { messages: errorMessages, args } );
  34. }
  35. const errorMessages = {
  36. [14 /* UNEXPECTED_RETURN_TYPE */]: 'Unexpected return type in composer',
  37. [15 /* INVALID_ARGUMENT */]: 'Invalid argument',
  38. [16 /* MUST_BE_CALL_SETUP_TOP */]: 'Must be called at the top of a `setup` function',
  39. [17 /* NOT_INSLALLED */]: 'Need to install with `app.use` function',
  40. [22 /* UNEXPECTED_ERROR */]: 'Unexpected error',
  41. [18 /* NOT_AVAILABLE_IN_LEGACY_MODE */]: 'Not available in legacy mode',
  42. [19 /* REQUIRED_VALUE */]: `Required in value: {0}`,
  43. [20 /* INVALID_VALUE */]: `Invalid value`,
  44. [21 /* CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN */]: `Cannot setup vue-devtools plugin`
  45. };
  46. const DEVTOOLS_META = '__INTLIFY_META__';
  47. const TransrateVNodeSymbol = shared.makeSymbol('__transrateVNode');
  48. const DatetimePartsSymbol = shared.makeSymbol('__datetimeParts');
  49. const NumberPartsSymbol = shared.makeSymbol('__numberParts');
  50. const EnableEmitter = shared.makeSymbol('__enableEmitter');
  51. const DisableEmitter = shared.makeSymbol('__disableEmitter');
  52. const SetPluralRulesSymbol = shared.makeSymbol('__setPluralRules');
  53. shared.makeSymbol('__intlifyMeta');
  54. const InejctWithOption = shared.makeSymbol('__injectWithOption');
  55. let composerID = 0;
  56. function defineCoreMissingHandler(missing) {
  57. return ((ctx, locale, key, type) => {
  58. return missing(locale, key, vue.getCurrentInstance() || undefined, type);
  59. });
  60. }
  61. function getLocaleMessages(locale, options) {
  62. const { messages, __i18n } = options;
  63. // prettier-ignore
  64. const ret = shared.isPlainObject(messages)
  65. ? messages
  66. : shared.isArray(__i18n)
  67. ? {}
  68. : { [locale]: {} };
  69. // merge locale messages of i18n custom block
  70. if (shared.isArray(__i18n)) {
  71. __i18n.forEach(({ locale, resource }) => {
  72. if (locale) {
  73. ret[locale] = ret[locale] || {};
  74. deepCopy(resource, ret[locale]);
  75. }
  76. else {
  77. deepCopy(resource, ret);
  78. }
  79. });
  80. }
  81. // handle messages for flat json
  82. if (options.flatJson) {
  83. for (const key in ret) {
  84. if (shared.hasOwn(ret, key)) {
  85. coreBase.handleFlatJson(ret[key]);
  86. }
  87. }
  88. }
  89. return ret;
  90. }
  91. const isNotObjectOrIsArray = (val) => !shared.isObject(val) || shared.isArray(val);
  92. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  93. function deepCopy(src, des) {
  94. // src and des should both be objects, and non of then can be a array
  95. if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) {
  96. throw createI18nError(20 /* INVALID_VALUE */);
  97. }
  98. for (const key in src) {
  99. if (shared.hasOwn(src, key)) {
  100. if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) {
  101. // replace with src[key] when:
  102. // src[key] or des[key] is not a object, or
  103. // src[key] or des[key] is a array
  104. des[key] = src[key];
  105. }
  106. else {
  107. // src[key] and des[key] are both object, merge them
  108. deepCopy(src[key], des[key]);
  109. }
  110. }
  111. }
  112. }
  113. // for Intlify DevTools
  114. const getMetaInfo = /* #__PURE__*/ () => {
  115. const instance = vue.getCurrentInstance();
  116. return instance && instance.type[DEVTOOLS_META] // eslint-disable-line @typescript-eslint/no-explicit-any
  117. ? { [DEVTOOLS_META]: instance.type[DEVTOOLS_META] } // eslint-disable-line @typescript-eslint/no-explicit-any
  118. : null;
  119. };
  120. /**
  121. * Create composer interface factory
  122. *
  123. * @internal
  124. */
  125. function createComposer(options = {}) {
  126. const { __root } = options;
  127. const _isGlobal = __root === undefined;
  128. let _inheritLocale = shared.isBoolean(options.inheritLocale)
  129. ? options.inheritLocale
  130. : true;
  131. const _locale = vue.ref(
  132. // prettier-ignore
  133. __root && _inheritLocale
  134. ? __root.locale.value
  135. : shared.isString(options.locale)
  136. ? options.locale
  137. : 'en-US');
  138. const _fallbackLocale = vue.ref(
  139. // prettier-ignore
  140. __root && _inheritLocale
  141. ? __root.fallbackLocale.value
  142. : shared.isString(options.fallbackLocale) ||
  143. shared.isArray(options.fallbackLocale) ||
  144. shared.isPlainObject(options.fallbackLocale) ||
  145. options.fallbackLocale === false
  146. ? options.fallbackLocale
  147. : _locale.value);
  148. const _messages = vue.ref(getLocaleMessages(_locale.value, options));
  149. const _datetimeFormats = vue.ref(shared.isPlainObject(options.datetimeFormats)
  150. ? options.datetimeFormats
  151. : { [_locale.value]: {} });
  152. const _numberFormats = vue.ref(shared.isPlainObject(options.numberFormats)
  153. ? options.numberFormats
  154. : { [_locale.value]: {} });
  155. // warning suppress options
  156. // prettier-ignore
  157. let _missingWarn = __root
  158. ? __root.missingWarn
  159. : shared.isBoolean(options.missingWarn) || shared.isRegExp(options.missingWarn)
  160. ? options.missingWarn
  161. : true;
  162. // prettier-ignore
  163. let _fallbackWarn = __root
  164. ? __root.fallbackWarn
  165. : shared.isBoolean(options.fallbackWarn) || shared.isRegExp(options.fallbackWarn)
  166. ? options.fallbackWarn
  167. : true;
  168. // prettier-ignore
  169. let _fallbackRoot = __root
  170. ? __root.fallbackRoot
  171. : shared.isBoolean(options.fallbackRoot)
  172. ? options.fallbackRoot
  173. : true;
  174. // configure fall back to root
  175. let _fallbackFormat = !!options.fallbackFormat;
  176. // runtime missing
  177. let _missing = shared.isFunction(options.missing) ? options.missing : null;
  178. let _runtimeMissing = shared.isFunction(options.missing)
  179. ? defineCoreMissingHandler(options.missing)
  180. : null;
  181. // postTranslation handler
  182. let _postTranslation = shared.isFunction(options.postTranslation)
  183. ? options.postTranslation
  184. : null;
  185. let _warnHtmlMessage = shared.isBoolean(options.warnHtmlMessage)
  186. ? options.warnHtmlMessage
  187. : true;
  188. let _escapeParameter = !!options.escapeParameter;
  189. // custom linked modifiers
  190. // prettier-ignore
  191. const _modifiers = __root
  192. ? __root.modifiers
  193. : shared.isPlainObject(options.modifiers)
  194. ? options.modifiers
  195. : {};
  196. // pluralRules
  197. let _pluralRules = options.pluralRules || (__root && __root.pluralRules);
  198. // runtime context
  199. // eslint-disable-next-line prefer-const
  200. let _context;
  201. function getCoreContext() {
  202. return coreBase.createCoreContext({
  203. version: VERSION,
  204. locale: _locale.value,
  205. fallbackLocale: _fallbackLocale.value,
  206. messages: _messages.value,
  207. datetimeFormats: _datetimeFormats.value,
  208. numberFormats: _numberFormats.value,
  209. modifiers: _modifiers,
  210. pluralRules: _pluralRules,
  211. missing: _runtimeMissing === null ? undefined : _runtimeMissing,
  212. missingWarn: _missingWarn,
  213. fallbackWarn: _fallbackWarn,
  214. fallbackFormat: _fallbackFormat,
  215. unresolving: true,
  216. postTranslation: _postTranslation === null ? undefined : _postTranslation,
  217. warnHtmlMessage: _warnHtmlMessage,
  218. escapeParameter: _escapeParameter,
  219. __datetimeFormatters: shared.isPlainObject(_context)
  220. ? _context.__datetimeFormatters
  221. : undefined,
  222. __numberFormatters: shared.isPlainObject(_context)
  223. ? _context.__numberFormatters
  224. : undefined,
  225. __v_emitter: shared.isPlainObject(_context)
  226. ? _context.__v_emitter
  227. : undefined,
  228. __meta: { framework: 'vue' }
  229. });
  230. }
  231. _context = getCoreContext();
  232. coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  233. // track reactivity
  234. function trackReactivityValues() {
  235. return [
  236. _locale.value,
  237. _fallbackLocale.value,
  238. _messages.value,
  239. _datetimeFormats.value,
  240. _numberFormats.value
  241. ];
  242. }
  243. // locale
  244. const locale = vue.computed({
  245. get: () => _locale.value,
  246. set: val => {
  247. _locale.value = val;
  248. _context.locale = _locale.value;
  249. }
  250. });
  251. // fallbackLocale
  252. const fallbackLocale = vue.computed({
  253. get: () => _fallbackLocale.value,
  254. set: val => {
  255. _fallbackLocale.value = val;
  256. _context.fallbackLocale = _fallbackLocale.value;
  257. coreBase.updateFallbackLocale(_context, _locale.value, val);
  258. }
  259. });
  260. // messages
  261. const messages = vue.computed(() => _messages.value);
  262. // datetimeFormats
  263. const datetimeFormats = vue.computed(() => _datetimeFormats.value);
  264. // numberFormats
  265. const numberFormats = vue.computed(() => _numberFormats.value);
  266. // getPostTranslationHandler
  267. function getPostTranslationHandler() {
  268. return shared.isFunction(_postTranslation) ? _postTranslation : null;
  269. }
  270. // setPostTranslationHandler
  271. function setPostTranslationHandler(handler) {
  272. _postTranslation = handler;
  273. _context.postTranslation = handler;
  274. }
  275. // getMissingHandler
  276. function getMissingHandler() {
  277. return _missing;
  278. }
  279. // setMissingHandler
  280. function setMissingHandler(handler) {
  281. if (handler !== null) {
  282. _runtimeMissing = defineCoreMissingHandler(handler);
  283. }
  284. _missing = handler;
  285. _context.missing = _runtimeMissing;
  286. }
  287. function isResolvedTranslateMessage(type, arg // eslint-disable-line @typescript-eslint/no-explicit-any
  288. ) {
  289. return type !== 'translate' || !!arg.resolvedMessage === false;
  290. }
  291. function wrapWithDeps(fn, argumentParser, warnType, fallbackSuccess, fallbackFail, successCondition) {
  292. trackReactivityValues(); // track reactive dependency
  293. // NOTE: experimental !!
  294. let ret;
  295. {
  296. try {
  297. coreBase.setAdditionalMeta(getMetaInfo());
  298. ret = fn(_context);
  299. }
  300. finally {
  301. coreBase.setAdditionalMeta(null);
  302. }
  303. }
  304. if (shared.isNumber(ret) && ret === coreBase.NOT_REOSLVED) {
  305. const [key, arg2] = argumentParser();
  306. if (__root &&
  307. shared.isString(key) &&
  308. isResolvedTranslateMessage(warnType, arg2)) {
  309. if (_fallbackRoot &&
  310. (coreBase.isTranslateFallbackWarn(_fallbackWarn, key) ||
  311. coreBase.isTranslateMissingWarn(_missingWarn, key))) {
  312. shared.warn(getWarnMessage(6 /* FALLBACK_TO_ROOT */, {
  313. key,
  314. type: warnType
  315. }));
  316. }
  317. // for vue-devtools timeline event
  318. {
  319. const { __v_emitter: emitter } = _context;
  320. if (emitter && _fallbackRoot) {
  321. emitter.emit("fallback" /* FALBACK */, {
  322. type: warnType,
  323. key,
  324. to: 'global',
  325. groupId: `${warnType}:${key}`
  326. });
  327. }
  328. }
  329. }
  330. return __root && _fallbackRoot
  331. ? fallbackSuccess(__root)
  332. : fallbackFail(key);
  333. }
  334. else if (successCondition(ret)) {
  335. return ret;
  336. }
  337. else {
  338. /* istanbul ignore next */
  339. throw createI18nError(14 /* UNEXPECTED_RETURN_TYPE */);
  340. }
  341. }
  342. // t
  343. function t(...args) {
  344. return wrapWithDeps(context => coreBase.translate(context, ...args), () => coreBase.parseTranslateArgs(...args), 'translate', root => root.t(...args), key => key, val => shared.isString(val));
  345. }
  346. // rt
  347. function rt(...args) {
  348. const [arg1, arg2, arg3] = args;
  349. if (arg3 && !shared.isObject(arg3)) {
  350. throw createI18nError(15 /* INVALID_ARGUMENT */);
  351. }
  352. return t(...[arg1, arg2, shared.assign({ resolvedMessage: true }, arg3 || {})]);
  353. }
  354. // d
  355. function d(...args) {
  356. return wrapWithDeps(context => coreBase.datetime(context, ...args), () => coreBase.parseDateTimeArgs(...args), 'datetime format', root => root.d(...args), () => coreBase.MISSING_RESOLVE_VALUE, val => shared.isString(val));
  357. }
  358. // n
  359. function n(...args) {
  360. return wrapWithDeps(context => coreBase.number(context, ...args), () => coreBase.parseNumberArgs(...args), 'number format', root => root.n(...args), () => coreBase.MISSING_RESOLVE_VALUE, val => shared.isString(val));
  361. }
  362. // for custom processor
  363. function normalize(values) {
  364. return values.map(val => shared.isString(val) ? vue.createVNode(vue.Text, null, val, 0) : val);
  365. }
  366. const interpolate = (val) => val;
  367. const processor = {
  368. normalize,
  369. interpolate,
  370. type: 'vnode'
  371. };
  372. // transrateVNode, using for `i18n-t` component
  373. function transrateVNode(...args) {
  374. return wrapWithDeps(context => {
  375. let ret;
  376. const _context = context;
  377. try {
  378. _context.processor = processor;
  379. ret = coreBase.translate(_context, ...args);
  380. }
  381. finally {
  382. _context.processor = null;
  383. }
  384. return ret;
  385. }, () => coreBase.parseTranslateArgs(...args), 'translate',
  386. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  387. root => root[TransrateVNodeSymbol](...args), key => [vue.createVNode(vue.Text, null, key, 0)], val => shared.isArray(val));
  388. }
  389. // numberParts, using for `i18n-n` component
  390. function numberParts(...args) {
  391. return wrapWithDeps(context => coreBase.number(context, ...args), () => coreBase.parseNumberArgs(...args), 'number format',
  392. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  393. root => root[NumberPartsSymbol](...args), () => [], val => shared.isString(val) || shared.isArray(val));
  394. }
  395. // datetimeParts, using for `i18n-d` component
  396. function datetimeParts(...args) {
  397. return wrapWithDeps(context => coreBase.datetime(context, ...args), () => coreBase.parseDateTimeArgs(...args), 'datetime format',
  398. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  399. root => root[DatetimePartsSymbol](...args), () => [], val => shared.isString(val) || shared.isArray(val));
  400. }
  401. function setPluralRules(rules) {
  402. _pluralRules = rules;
  403. _context.pluralRules = _pluralRules;
  404. }
  405. // te
  406. function te(key, locale) {
  407. const targetLocale = shared.isString(locale) ? locale : _locale.value;
  408. const message = getLocaleMessage(targetLocale);
  409. return coreBase.resolveValue(message, key) !== null;
  410. }
  411. function resolveMessages(key) {
  412. let messages = null;
  413. const locales = coreBase.getLocaleChain(_context, _fallbackLocale.value, _locale.value);
  414. for (let i = 0; i < locales.length; i++) {
  415. const targetLocaleMessages = _messages.value[locales[i]] || {};
  416. const messageValue = coreBase.resolveValue(targetLocaleMessages, key);
  417. if (messageValue != null) {
  418. messages = messageValue;
  419. break;
  420. }
  421. }
  422. return messages;
  423. }
  424. // tm
  425. function tm(key) {
  426. const messages = resolveMessages(key);
  427. // prettier-ignore
  428. return messages != null
  429. ? messages
  430. : __root
  431. ? __root.tm(key) || {}
  432. : {};
  433. }
  434. // getLocaleMessage
  435. function getLocaleMessage(locale) {
  436. return (_messages.value[locale] || {});
  437. }
  438. // setLocaleMessage
  439. function setLocaleMessage(locale, message) {
  440. _messages.value[locale] = message;
  441. _context.messages = _messages.value;
  442. }
  443. // mergeLocaleMessage
  444. function mergeLocaleMessage(locale, message) {
  445. _messages.value[locale] = _messages.value[locale] || {};
  446. deepCopy(message, _messages.value[locale]);
  447. _context.messages = _messages.value;
  448. }
  449. // getDateTimeFormat
  450. function getDateTimeFormat(locale) {
  451. return _datetimeFormats.value[locale] || {};
  452. }
  453. // setDateTimeFormat
  454. function setDateTimeFormat(locale, format) {
  455. _datetimeFormats.value[locale] = format;
  456. _context.datetimeFormats = _datetimeFormats.value;
  457. coreBase.clearDateTimeFormat(_context, locale, format);
  458. }
  459. // mergeDateTimeFormat
  460. function mergeDateTimeFormat(locale, format) {
  461. _datetimeFormats.value[locale] = shared.assign(_datetimeFormats.value[locale] || {}, format);
  462. _context.datetimeFormats = _datetimeFormats.value;
  463. coreBase.clearDateTimeFormat(_context, locale, format);
  464. }
  465. // getNumberFormat
  466. function getNumberFormat(locale) {
  467. return _numberFormats.value[locale] || {};
  468. }
  469. // setNumberFormat
  470. function setNumberFormat(locale, format) {
  471. _numberFormats.value[locale] = format;
  472. _context.numberFormats = _numberFormats.value;
  473. coreBase.clearNumberFormat(_context, locale, format);
  474. }
  475. // mergeNumberFormat
  476. function mergeNumberFormat(locale, format) {
  477. _numberFormats.value[locale] = shared.assign(_numberFormats.value[locale] || {}, format);
  478. _context.numberFormats = _numberFormats.value;
  479. coreBase.clearNumberFormat(_context, locale, format);
  480. }
  481. // for debug
  482. composerID++;
  483. // watch root locale & fallbackLocale
  484. if (__root) {
  485. vue.watch(__root.locale, (val) => {
  486. if (_inheritLocale) {
  487. _locale.value = val;
  488. _context.locale = val;
  489. coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  490. }
  491. });
  492. vue.watch(__root.fallbackLocale, (val) => {
  493. if (_inheritLocale) {
  494. _fallbackLocale.value = val;
  495. _context.fallbackLocale = val;
  496. coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  497. }
  498. });
  499. }
  500. // define composition API!
  501. const composer = {
  502. id: composerID,
  503. locale,
  504. fallbackLocale,
  505. get inheritLocale() {
  506. return _inheritLocale;
  507. },
  508. set inheritLocale(val) {
  509. _inheritLocale = val;
  510. if (val && __root) {
  511. _locale.value = __root.locale.value;
  512. _fallbackLocale.value = __root.fallbackLocale.value;
  513. coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  514. }
  515. },
  516. get availableLocales() {
  517. return Object.keys(_messages.value).sort();
  518. },
  519. messages,
  520. datetimeFormats,
  521. numberFormats,
  522. get modifiers() {
  523. return _modifiers;
  524. },
  525. get pluralRules() {
  526. return _pluralRules || {};
  527. },
  528. get isGlobal() {
  529. return _isGlobal;
  530. },
  531. get missingWarn() {
  532. return _missingWarn;
  533. },
  534. set missingWarn(val) {
  535. _missingWarn = val;
  536. _context.missingWarn = _missingWarn;
  537. },
  538. get fallbackWarn() {
  539. return _fallbackWarn;
  540. },
  541. set fallbackWarn(val) {
  542. _fallbackWarn = val;
  543. _context.fallbackWarn = _fallbackWarn;
  544. },
  545. get fallbackRoot() {
  546. return _fallbackRoot;
  547. },
  548. set fallbackRoot(val) {
  549. _fallbackRoot = val;
  550. },
  551. get fallbackFormat() {
  552. return _fallbackFormat;
  553. },
  554. set fallbackFormat(val) {
  555. _fallbackFormat = val;
  556. _context.fallbackFormat = _fallbackFormat;
  557. },
  558. get warnHtmlMessage() {
  559. return _warnHtmlMessage;
  560. },
  561. set warnHtmlMessage(val) {
  562. _warnHtmlMessage = val;
  563. _context.warnHtmlMessage = val;
  564. },
  565. get escapeParameter() {
  566. return _escapeParameter;
  567. },
  568. set escapeParameter(val) {
  569. _escapeParameter = val;
  570. _context.escapeParameter = val;
  571. },
  572. t,
  573. rt,
  574. d,
  575. n,
  576. te,
  577. tm,
  578. getLocaleMessage,
  579. setLocaleMessage,
  580. mergeLocaleMessage,
  581. getDateTimeFormat,
  582. setDateTimeFormat,
  583. mergeDateTimeFormat,
  584. getNumberFormat,
  585. setNumberFormat,
  586. mergeNumberFormat,
  587. getPostTranslationHandler,
  588. setPostTranslationHandler,
  589. getMissingHandler,
  590. setMissingHandler,
  591. [TransrateVNodeSymbol]: transrateVNode,
  592. [NumberPartsSymbol]: numberParts,
  593. [DatetimePartsSymbol]: datetimeParts,
  594. [SetPluralRulesSymbol]: setPluralRules,
  595. [InejctWithOption]: options.__injectWithOption // eslint-disable-line @typescript-eslint/no-explicit-any
  596. };
  597. // for vue-devtools timeline event
  598. {
  599. composer[EnableEmitter] = (emitter) => {
  600. _context.__v_emitter = emitter;
  601. };
  602. composer[DisableEmitter] = () => {
  603. _context.__v_emitter = undefined;
  604. };
  605. }
  606. return composer;
  607. }
  608. /**
  609. * Convert to I18n Composer Options from VueI18n Options
  610. *
  611. * @internal
  612. */
  613. function convertComposerOptions(options) {
  614. const locale = shared.isString(options.locale) ? options.locale : 'en-US';
  615. const fallbackLocale = shared.isString(options.fallbackLocale) ||
  616. shared.isArray(options.fallbackLocale) ||
  617. shared.isPlainObject(options.fallbackLocale) ||
  618. options.fallbackLocale === false
  619. ? options.fallbackLocale
  620. : locale;
  621. const missing = shared.isFunction(options.missing) ? options.missing : undefined;
  622. const missingWarn = shared.isBoolean(options.silentTranslationWarn) ||
  623. shared.isRegExp(options.silentTranslationWarn)
  624. ? !options.silentTranslationWarn
  625. : true;
  626. const fallbackWarn = shared.isBoolean(options.silentFallbackWarn) ||
  627. shared.isRegExp(options.silentFallbackWarn)
  628. ? !options.silentFallbackWarn
  629. : true;
  630. const fallbackRoot = shared.isBoolean(options.fallbackRoot)
  631. ? options.fallbackRoot
  632. : true;
  633. const fallbackFormat = !!options.formatFallbackMessages;
  634. const modifiers = shared.isPlainObject(options.modifiers) ? options.modifiers : {};
  635. const pluralizationRules = options.pluralizationRules;
  636. const postTranslation = shared.isFunction(options.postTranslation)
  637. ? options.postTranslation
  638. : undefined;
  639. const warnHtmlMessage = shared.isString(options.warnHtmlInMessage)
  640. ? options.warnHtmlInMessage !== 'off'
  641. : true;
  642. const escapeParameter = !!options.escapeParameterHtml;
  643. const inheritLocale = shared.isBoolean(options.sync) ? options.sync : true;
  644. if (options.formatter) {
  645. shared.warn(getWarnMessage(8 /* NOT_SUPPORTED_FORMATTER */));
  646. }
  647. if (options.preserveDirectiveContent) {
  648. shared.warn(getWarnMessage(9 /* NOT_SUPPORTED_PRESERVE_DIRECTIVE */));
  649. }
  650. let messages = options.messages;
  651. if (shared.isPlainObject(options.sharedMessages)) {
  652. const sharedMessages = options.sharedMessages;
  653. const locales = Object.keys(sharedMessages);
  654. messages = locales.reduce((messages, locale) => {
  655. const message = messages[locale] || (messages[locale] = {});
  656. shared.assign(message, sharedMessages[locale]);
  657. return messages;
  658. }, (messages || {}));
  659. }
  660. const { __i18n, __root, __injectWithOption } = options;
  661. const datetimeFormats = options.datetimeFormats;
  662. const numberFormats = options.numberFormats;
  663. const flatJson = options.flatJson;
  664. return {
  665. locale,
  666. fallbackLocale,
  667. messages,
  668. flatJson,
  669. datetimeFormats,
  670. numberFormats,
  671. missing,
  672. missingWarn,
  673. fallbackWarn,
  674. fallbackRoot,
  675. fallbackFormat,
  676. modifiers,
  677. pluralRules: pluralizationRules,
  678. postTranslation,
  679. warnHtmlMessage,
  680. escapeParameter,
  681. inheritLocale,
  682. __i18n,
  683. __root,
  684. __injectWithOption
  685. };
  686. }
  687. /**
  688. * create VueI18n interface factory
  689. *
  690. * @internal
  691. */
  692. function createVueI18n(options = {}) {
  693. const composer = createComposer(convertComposerOptions(options));
  694. // defines VueI18n
  695. const vueI18n = {
  696. // id
  697. id: composer.id,
  698. // locale
  699. get locale() {
  700. return composer.locale.value;
  701. },
  702. set locale(val) {
  703. composer.locale.value = val;
  704. },
  705. // fallbackLocale
  706. get fallbackLocale() {
  707. return composer.fallbackLocale.value;
  708. },
  709. set fallbackLocale(val) {
  710. composer.fallbackLocale.value = val;
  711. },
  712. // messages
  713. get messages() {
  714. return composer.messages.value;
  715. },
  716. // datetimeFormats
  717. get datetimeFormats() {
  718. return composer.datetimeFormats.value;
  719. },
  720. // numberFormats
  721. get numberFormats() {
  722. return composer.numberFormats.value;
  723. },
  724. // availableLocales
  725. get availableLocales() {
  726. return composer.availableLocales;
  727. },
  728. // formatter
  729. get formatter() {
  730. shared.warn(getWarnMessage(8 /* NOT_SUPPORTED_FORMATTER */));
  731. // dummy
  732. return {
  733. interpolate() {
  734. return [];
  735. }
  736. };
  737. },
  738. set formatter(val) {
  739. shared.warn(getWarnMessage(8 /* NOT_SUPPORTED_FORMATTER */));
  740. },
  741. // missing
  742. get missing() {
  743. return composer.getMissingHandler();
  744. },
  745. set missing(handler) {
  746. composer.setMissingHandler(handler);
  747. },
  748. // silentTranslationWarn
  749. get silentTranslationWarn() {
  750. return shared.isBoolean(composer.missingWarn)
  751. ? !composer.missingWarn
  752. : composer.missingWarn;
  753. },
  754. set silentTranslationWarn(val) {
  755. composer.missingWarn = shared.isBoolean(val) ? !val : val;
  756. },
  757. // silentFallbackWarn
  758. get silentFallbackWarn() {
  759. return shared.isBoolean(composer.fallbackWarn)
  760. ? !composer.fallbackWarn
  761. : composer.fallbackWarn;
  762. },
  763. set silentFallbackWarn(val) {
  764. composer.fallbackWarn = shared.isBoolean(val) ? !val : val;
  765. },
  766. // modifiers
  767. get modifiers() {
  768. return composer.modifiers;
  769. },
  770. // formatFallbackMessages
  771. get formatFallbackMessages() {
  772. return composer.fallbackFormat;
  773. },
  774. set formatFallbackMessages(val) {
  775. composer.fallbackFormat = val;
  776. },
  777. // postTranslation
  778. get postTranslation() {
  779. return composer.getPostTranslationHandler();
  780. },
  781. set postTranslation(handler) {
  782. composer.setPostTranslationHandler(handler);
  783. },
  784. // sync
  785. get sync() {
  786. return composer.inheritLocale;
  787. },
  788. set sync(val) {
  789. composer.inheritLocale = val;
  790. },
  791. // warnInHtmlMessage
  792. get warnHtmlInMessage() {
  793. return composer.warnHtmlMessage ? 'warn' : 'off';
  794. },
  795. set warnHtmlInMessage(val) {
  796. composer.warnHtmlMessage = val !== 'off';
  797. },
  798. // escapeParameterHtml
  799. get escapeParameterHtml() {
  800. return composer.escapeParameter;
  801. },
  802. set escapeParameterHtml(val) {
  803. composer.escapeParameter = val;
  804. },
  805. // preserveDirectiveContent
  806. get preserveDirectiveContent() {
  807. shared.warn(getWarnMessage(9 /* NOT_SUPPORTED_PRESERVE_DIRECTIVE */));
  808. return true;
  809. },
  810. set preserveDirectiveContent(val) {
  811. shared.warn(getWarnMessage(9 /* NOT_SUPPORTED_PRESERVE_DIRECTIVE */));
  812. },
  813. // pluralizationRules
  814. get pluralizationRules() {
  815. return composer.pluralRules || {};
  816. },
  817. // for internal
  818. __composer: composer,
  819. // t
  820. t(...args) {
  821. const [arg1, arg2, arg3] = args;
  822. const options = {};
  823. let list = null;
  824. let named = null;
  825. if (!shared.isString(arg1)) {
  826. throw createI18nError(15 /* INVALID_ARGUMENT */);
  827. }
  828. const key = arg1;
  829. if (shared.isString(arg2)) {
  830. options.locale = arg2;
  831. }
  832. else if (shared.isArray(arg2)) {
  833. list = arg2;
  834. }
  835. else if (shared.isPlainObject(arg2)) {
  836. named = arg2;
  837. }
  838. if (shared.isArray(arg3)) {
  839. list = arg3;
  840. }
  841. else if (shared.isPlainObject(arg3)) {
  842. named = arg3;
  843. }
  844. return composer.t(key, list || named || {}, options);
  845. },
  846. rt(...args) {
  847. return composer.rt(...args);
  848. },
  849. // tc
  850. tc(...args) {
  851. const [arg1, arg2, arg3] = args;
  852. const options = { plural: 1 };
  853. let list = null;
  854. let named = null;
  855. if (!shared.isString(arg1)) {
  856. throw createI18nError(15 /* INVALID_ARGUMENT */);
  857. }
  858. const key = arg1;
  859. if (shared.isString(arg2)) {
  860. options.locale = arg2;
  861. }
  862. else if (shared.isNumber(arg2)) {
  863. options.plural = arg2;
  864. }
  865. else if (shared.isArray(arg2)) {
  866. list = arg2;
  867. }
  868. else if (shared.isPlainObject(arg2)) {
  869. named = arg2;
  870. }
  871. if (shared.isString(arg3)) {
  872. options.locale = arg3;
  873. }
  874. else if (shared.isArray(arg3)) {
  875. list = arg3;
  876. }
  877. else if (shared.isPlainObject(arg3)) {
  878. named = arg3;
  879. }
  880. return composer.t(key, list || named || {}, options);
  881. },
  882. // te
  883. te(key, locale) {
  884. return composer.te(key, locale);
  885. },
  886. // tm
  887. tm(key) {
  888. return composer.tm(key);
  889. },
  890. // getLocaleMessage
  891. getLocaleMessage(locale) {
  892. return composer.getLocaleMessage(locale);
  893. },
  894. // setLocaleMessage
  895. setLocaleMessage(locale, message) {
  896. composer.setLocaleMessage(locale, message);
  897. },
  898. // mergeLocaleMessage
  899. mergeLocaleMessage(locale, message) {
  900. composer.mergeLocaleMessage(locale, message);
  901. },
  902. // d
  903. d(...args) {
  904. return composer.d(...args);
  905. },
  906. // getDateTimeFormat
  907. getDateTimeFormat(locale) {
  908. return composer.getDateTimeFormat(locale);
  909. },
  910. // setDateTimeFormat
  911. setDateTimeFormat(locale, format) {
  912. composer.setDateTimeFormat(locale, format);
  913. },
  914. // mergeDateTimeFormat
  915. mergeDateTimeFormat(locale, format) {
  916. composer.mergeDateTimeFormat(locale, format);
  917. },
  918. // n
  919. n(...args) {
  920. return composer.n(...args);
  921. },
  922. // getNumberFormat
  923. getNumberFormat(locale) {
  924. return composer.getNumberFormat(locale);
  925. },
  926. // setNumberFormat
  927. setNumberFormat(locale, format) {
  928. composer.setNumberFormat(locale, format);
  929. },
  930. // mergeNumberFormat
  931. mergeNumberFormat(locale, format) {
  932. composer.mergeNumberFormat(locale, format);
  933. },
  934. // getChoiceIndex
  935. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  936. getChoiceIndex(choice, choicesLength) {
  937. shared.warn(getWarnMessage(10 /* NOT_SUPPORTED_GET_CHOICE_INDEX */));
  938. return -1;
  939. },
  940. // for internal
  941. __onComponentInstanceCreated(target) {
  942. const { componentInstanceCreatedListener } = options;
  943. if (componentInstanceCreatedListener) {
  944. componentInstanceCreatedListener(target, vueI18n);
  945. }
  946. }
  947. };
  948. // for vue-devtools timeline event
  949. {
  950. vueI18n.__enableEmitter = (emitter) => {
  951. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  952. const __composer = composer;
  953. __composer[EnableEmitter] && __composer[EnableEmitter](emitter);
  954. };
  955. vueI18n.__disableEmitter = () => {
  956. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  957. const __composer = composer;
  958. __composer[DisableEmitter] && __composer[DisableEmitter]();
  959. };
  960. }
  961. return vueI18n;
  962. }
  963. const baseFormatProps = {
  964. tag: {
  965. type: [String, Object]
  966. },
  967. locale: {
  968. type: String
  969. },
  970. scope: {
  971. type: String,
  972. validator: (val) => val === 'parent' || val === 'global',
  973. default: 'parent'
  974. },
  975. i18n: {
  976. type: Object
  977. }
  978. };
  979. /**
  980. * Translation Component
  981. *
  982. * @remarks
  983. * See the following items for property about details
  984. *
  985. * @VueI18nSee [TranslationProps](component#translationprops)
  986. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  987. * @VueI18nSee [Component Interpolation](../guide/advanced/component)
  988. *
  989. * @example
  990. * ```html
  991. * <div id="app">
  992. * <!-- ... -->
  993. * <i18n path="term" tag="label" for="tos">
  994. * <a :href="url" target="_blank">{{ $t('tos') }}</a>
  995. * </i18n>
  996. * <!-- ... -->
  997. * </div>
  998. * ```
  999. * ```js
  1000. * import { createApp } from 'vue'
  1001. * import { createI18n } from 'vue-i18n'
  1002. *
  1003. * const messages = {
  1004. * en: {
  1005. * tos: 'Term of Service',
  1006. * term: 'I accept xxx {0}.'
  1007. * },
  1008. * ja: {
  1009. * tos: '利用規約',
  1010. * term: '私は xxx の{0}に同意します。'
  1011. * }
  1012. * }
  1013. *
  1014. * const i18n = createI18n({
  1015. * locale: 'en',
  1016. * messages
  1017. * })
  1018. *
  1019. * const app = createApp({
  1020. * data: {
  1021. * url: '/term'
  1022. * }
  1023. * }).use(i18n).mount('#app')
  1024. * ```
  1025. *
  1026. * @VueI18nComponent
  1027. */
  1028. const Translation = {
  1029. /* eslint-disable */
  1030. name: 'i18n-t',
  1031. props: shared.assign({
  1032. keypath: {
  1033. type: String,
  1034. required: true
  1035. },
  1036. plural: {
  1037. type: [Number, String],
  1038. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1039. validator: (val) => shared.isNumber(val) || !isNaN(val)
  1040. }
  1041. }, baseFormatProps),
  1042. /* eslint-enable */
  1043. setup(props, context) {
  1044. const { slots, attrs } = context;
  1045. const i18n = props.i18n ||
  1046. useI18n({
  1047. useScope: props.scope,
  1048. __useComponent: true
  1049. });
  1050. const keys = Object.keys(slots).filter(key => key !== '_');
  1051. return () => {
  1052. const options = {};
  1053. if (props.locale) {
  1054. options.locale = props.locale;
  1055. }
  1056. if (props.plural !== undefined) {
  1057. options.plural = shared.isString(props.plural) ? +props.plural : props.plural;
  1058. }
  1059. const arg = getInterpolateArg(context, keys);
  1060. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1061. const children = i18n[TransrateVNodeSymbol](props.keypath, arg, options);
  1062. const assignedAttrs = shared.assign({}, attrs);
  1063. // prettier-ignore
  1064. return shared.isString(props.tag)
  1065. ? vue.h(props.tag, assignedAttrs, children)
  1066. : shared.isObject(props.tag)
  1067. ? vue.h(props.tag, assignedAttrs, children)
  1068. : vue.h(vue.Fragment, assignedAttrs, children);
  1069. };
  1070. }
  1071. };
  1072. function getInterpolateArg({ slots }, keys) {
  1073. if (keys.length === 1 && keys[0] === 'default') {
  1074. // default slot only
  1075. return slots.default ? slots.default() : [];
  1076. }
  1077. else {
  1078. // named slots
  1079. return keys.reduce((arg, key) => {
  1080. const slot = slots[key];
  1081. if (slot) {
  1082. arg[key] = slot();
  1083. }
  1084. return arg;
  1085. }, {});
  1086. }
  1087. }
  1088. function renderFormatter(props, context, slotKeys, partFormatter) {
  1089. const { slots, attrs } = context;
  1090. return () => {
  1091. const options = { part: true };
  1092. let overrides = {};
  1093. if (props.locale) {
  1094. options.locale = props.locale;
  1095. }
  1096. if (shared.isString(props.format)) {
  1097. options.key = props.format;
  1098. }
  1099. else if (shared.isObject(props.format)) {
  1100. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1101. if (shared.isString(props.format.key)) {
  1102. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1103. options.key = props.format.key;
  1104. }
  1105. // Filter out number format options only
  1106. overrides = Object.keys(props.format).reduce((options, prop) => {
  1107. return slotKeys.includes(prop)
  1108. ? shared.assign({}, options, { [prop]: props.format[prop] }) // eslint-disable-line @typescript-eslint/no-explicit-any
  1109. : options;
  1110. }, {});
  1111. }
  1112. const parts = partFormatter(...[props.value, options, overrides]);
  1113. let children = [options.key];
  1114. if (shared.isArray(parts)) {
  1115. children = parts.map((part, index) => {
  1116. const slot = slots[part.type];
  1117. return slot
  1118. ? slot({ [part.type]: part.value, index, parts })
  1119. : [part.value];
  1120. });
  1121. }
  1122. else if (shared.isString(parts)) {
  1123. children = [parts];
  1124. }
  1125. const assignedAttrs = shared.assign({}, attrs);
  1126. // prettier-ignore
  1127. return shared.isString(props.tag)
  1128. ? vue.h(props.tag, assignedAttrs, children)
  1129. : shared.isObject(props.tag)
  1130. ? vue.h(props.tag, assignedAttrs, children)
  1131. : vue.h(vue.Fragment, assignedAttrs, children);
  1132. };
  1133. }
  1134. const NUMBER_FORMAT_KEYS = [
  1135. 'localeMatcher',
  1136. 'style',
  1137. 'unit',
  1138. 'unitDisplay',
  1139. 'currency',
  1140. 'currencyDisplay',
  1141. 'useGrouping',
  1142. 'numberingSystem',
  1143. 'minimumIntegerDigits',
  1144. 'minimumFractionDigits',
  1145. 'maximumFractionDigits',
  1146. 'minimumSignificantDigits',
  1147. 'maximumSignificantDigits',
  1148. 'notation',
  1149. 'formatMatcher'
  1150. ];
  1151. /**
  1152. * Number Format Component
  1153. *
  1154. * @remarks
  1155. * See the following items for property about details
  1156. *
  1157. * @VueI18nSee [FormattableProps](component#formattableprops)
  1158. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  1159. * @VueI18nSee [Custom Formatting](../guide/essentials/number#custom-formatting)
  1160. *
  1161. * @VueI18nDanger
  1162. * 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)
  1163. *
  1164. * If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-numberformat)
  1165. *
  1166. * @VueI18nComponent
  1167. */
  1168. const NumberFormat = {
  1169. /* eslint-disable */
  1170. name: 'i18n-n',
  1171. props: shared.assign({
  1172. value: {
  1173. type: Number,
  1174. required: true
  1175. },
  1176. format: {
  1177. type: [String, Object]
  1178. }
  1179. }, baseFormatProps),
  1180. /* eslint-enable */
  1181. setup(props, context) {
  1182. const i18n = props.i18n ||
  1183. useI18n({ useScope: 'parent', __useComponent: true });
  1184. return renderFormatter(props, context, NUMBER_FORMAT_KEYS, (...args) =>
  1185. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1186. i18n[NumberPartsSymbol](...args));
  1187. }
  1188. };
  1189. const DATETIME_FORMAT_KEYS = [
  1190. 'dateStyle',
  1191. 'timeStyle',
  1192. 'fractionalSecondDigits',
  1193. 'calendar',
  1194. 'dayPeriod',
  1195. 'numberingSystem',
  1196. 'localeMatcher',
  1197. 'timeZone',
  1198. 'hour12',
  1199. 'hourCycle',
  1200. 'formatMatcher',
  1201. 'weekday',
  1202. 'era',
  1203. 'year',
  1204. 'month',
  1205. 'day',
  1206. 'hour',
  1207. 'minute',
  1208. 'second',
  1209. 'timeZoneName'
  1210. ];
  1211. /**
  1212. * Datetime Format Component
  1213. *
  1214. * @remarks
  1215. * See the following items for property about details
  1216. *
  1217. * @VueI18nSee [FormattableProps](component#formattableprops)
  1218. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  1219. * @VueI18nSee [Custom Formatting](../guide/essentials/datetime#custom-formatting)
  1220. *
  1221. * @VueI18nDanger
  1222. * 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)
  1223. *
  1224. * If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-datetimeformat)
  1225. *
  1226. * @VueI18nComponent
  1227. */
  1228. const DatetimeFormat = {
  1229. /* eslint-disable */
  1230. name: 'i18n-d',
  1231. props: shared.assign({
  1232. value: {
  1233. type: [Number, Date],
  1234. required: true
  1235. },
  1236. format: {
  1237. type: [String, Object]
  1238. }
  1239. }, baseFormatProps),
  1240. /* eslint-enable */
  1241. setup(props, context) {
  1242. const i18n = props.i18n ||
  1243. useI18n({ useScope: 'parent', __useComponent: true });
  1244. return renderFormatter(props, context, DATETIME_FORMAT_KEYS, (...args) =>
  1245. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1246. i18n[DatetimePartsSymbol](...args));
  1247. }
  1248. };
  1249. function getComposer$1(i18n, instance) {
  1250. const i18nInternal = i18n;
  1251. if (i18n.mode === 'composition') {
  1252. return (i18nInternal.__getInstance(instance) || i18n.global);
  1253. }
  1254. else {
  1255. const vueI18n = i18nInternal.__getInstance(instance);
  1256. return vueI18n != null
  1257. ? vueI18n.__composer
  1258. : i18n.global.__composer;
  1259. }
  1260. }
  1261. function vTDirective(i18n) {
  1262. const bind = (el, { instance, value, modifiers }) => {
  1263. /* istanbul ignore if */
  1264. if (!instance || !instance.$) {
  1265. throw createI18nError(22 /* UNEXPECTED_ERROR */);
  1266. }
  1267. const composer = getComposer$1(i18n, instance.$);
  1268. if (modifiers.preserve) {
  1269. shared.warn(getWarnMessage(7 /* NOT_SUPPORTED_PRESERVE */));
  1270. }
  1271. const parsedValue = parseValue(value);
  1272. el.textContent = composer.t(...makeParams(parsedValue));
  1273. };
  1274. return {
  1275. beforeMount: bind,
  1276. beforeUpdate: bind
  1277. };
  1278. }
  1279. function parseValue(value) {
  1280. if (shared.isString(value)) {
  1281. return { path: value };
  1282. }
  1283. else if (shared.isPlainObject(value)) {
  1284. if (!('path' in value)) {
  1285. throw createI18nError(19 /* REQUIRED_VALUE */, 'path');
  1286. }
  1287. return value;
  1288. }
  1289. else {
  1290. throw createI18nError(20 /* INVALID_VALUE */);
  1291. }
  1292. }
  1293. function makeParams(value) {
  1294. const { path, locale, args, choice, plural } = value;
  1295. const options = {};
  1296. const named = args || {};
  1297. if (shared.isString(locale)) {
  1298. options.locale = locale;
  1299. }
  1300. if (shared.isNumber(choice)) {
  1301. options.plural = choice;
  1302. }
  1303. if (shared.isNumber(plural)) {
  1304. options.plural = plural;
  1305. }
  1306. return [path, named, options];
  1307. }
  1308. function apply(app, i18n, ...options) {
  1309. const pluginOptions = shared.isPlainObject(options[0])
  1310. ? options[0]
  1311. : {};
  1312. const useI18nComponentName = !!pluginOptions.useI18nComponentName;
  1313. const globalInstall = shared.isBoolean(pluginOptions.globalInstall)
  1314. ? pluginOptions.globalInstall
  1315. : true;
  1316. if (globalInstall && useI18nComponentName) {
  1317. shared.warn(getWarnMessage(11 /* COMPONENT_NAME_LEGACY_COMPATIBLE */, {
  1318. name: Translation.name
  1319. }));
  1320. }
  1321. if (globalInstall) {
  1322. // install components
  1323. app.component(!useI18nComponentName ? Translation.name : 'i18n', Translation);
  1324. app.component(NumberFormat.name, NumberFormat);
  1325. app.component(DatetimeFormat.name, DatetimeFormat);
  1326. }
  1327. // install directive
  1328. app.directive('t', vTDirective(i18n));
  1329. }
  1330. // supports compatibility for legacy vue-i18n APIs
  1331. function defineMixin(vuei18n, composer, i18n) {
  1332. return {
  1333. beforeCreate() {
  1334. const instance = vue.getCurrentInstance();
  1335. /* istanbul ignore if */
  1336. if (!instance) {
  1337. throw createI18nError(22 /* UNEXPECTED_ERROR */);
  1338. }
  1339. const options = this.$options;
  1340. if (options.i18n) {
  1341. const optionsI18n = options.i18n;
  1342. if (options.__i18n) {
  1343. optionsI18n.__i18n = options.__i18n;
  1344. }
  1345. optionsI18n.__root = composer;
  1346. if (this === this.$root) {
  1347. this.$i18n = mergeToRoot(vuei18n, optionsI18n);
  1348. }
  1349. else {
  1350. optionsI18n.__injectWithOption = true;
  1351. this.$i18n = createVueI18n(optionsI18n);
  1352. }
  1353. }
  1354. else if (options.__i18n) {
  1355. if (this === this.$root) {
  1356. this.$i18n = mergeToRoot(vuei18n, options);
  1357. }
  1358. else {
  1359. this.$i18n = createVueI18n({
  1360. __i18n: options.__i18n,
  1361. __injectWithOption: true,
  1362. __root: composer
  1363. });
  1364. }
  1365. }
  1366. else {
  1367. // set global
  1368. this.$i18n = vuei18n;
  1369. }
  1370. vuei18n.__onComponentInstanceCreated(this.$i18n);
  1371. i18n.__setInstance(instance, this.$i18n);
  1372. // defines vue-i18n legacy APIs
  1373. this.$t = (...args) => this.$i18n.t(...args);
  1374. this.$rt = (...args) => this.$i18n.rt(...args);
  1375. this.$tc = (...args) => this.$i18n.tc(...args);
  1376. this.$te = (key, locale) => this.$i18n.te(key, locale);
  1377. this.$d = (...args) => this.$i18n.d(...args);
  1378. this.$n = (...args) => this.$i18n.n(...args);
  1379. this.$tm = (key) => this.$i18n.tm(key);
  1380. },
  1381. mounted() {
  1382. },
  1383. beforeUnmount() {
  1384. const instance = vue.getCurrentInstance();
  1385. /* istanbul ignore if */
  1386. if (!instance) {
  1387. throw createI18nError(22 /* UNEXPECTED_ERROR */);
  1388. }
  1389. delete this.$t;
  1390. delete this.$rt;
  1391. delete this.$tc;
  1392. delete this.$te;
  1393. delete this.$d;
  1394. delete this.$n;
  1395. delete this.$tm;
  1396. i18n.__deleteInstance(instance);
  1397. delete this.$i18n;
  1398. }
  1399. };
  1400. }
  1401. function mergeToRoot(root, options) {
  1402. root.locale = options.locale || root.locale;
  1403. root.fallbackLocale = options.fallbackLocale || root.fallbackLocale;
  1404. root.missing = options.missing || root.missing;
  1405. root.silentTranslationWarn =
  1406. options.silentTranslationWarn || root.silentFallbackWarn;
  1407. root.silentFallbackWarn =
  1408. options.silentFallbackWarn || root.silentFallbackWarn;
  1409. root.formatFallbackMessages =
  1410. options.formatFallbackMessages || root.formatFallbackMessages;
  1411. root.postTranslation = options.postTranslation || root.postTranslation;
  1412. root.warnHtmlInMessage = options.warnHtmlInMessage || root.warnHtmlInMessage;
  1413. root.escapeParameterHtml =
  1414. options.escapeParameterHtml || root.escapeParameterHtml;
  1415. root.sync = options.sync || root.sync;
  1416. root.__composer[SetPluralRulesSymbol](options.pluralizationRules || root.pluralizationRules);
  1417. const messages = getLocaleMessages(root.locale, {
  1418. messages: options.messages,
  1419. __i18n: options.__i18n
  1420. });
  1421. Object.keys(messages).forEach(locale => root.mergeLocaleMessage(locale, messages[locale]));
  1422. if (options.datetimeFormats) {
  1423. Object.keys(options.datetimeFormats).forEach(locale => root.mergeDateTimeFormat(locale, options.datetimeFormats[locale]));
  1424. }
  1425. if (options.numberFormats) {
  1426. Object.keys(options.numberFormats).forEach(locale => root.mergeNumberFormat(locale, options.numberFormats[locale]));
  1427. }
  1428. return root;
  1429. }
  1430. /**
  1431. * Vue I18n factory
  1432. *
  1433. * @param options - An options, see the {@link I18nOptions}
  1434. *
  1435. * @returns {@link I18n} instance
  1436. *
  1437. * @remarks
  1438. * If you use Legacy API mode, you need toto specify {@link VueI18nOptions} and `legacy: true` option.
  1439. *
  1440. * If you use composition API mode, you need to specify {@link ComposerOptions}.
  1441. *
  1442. * @VueI18nSee [Getting Started](../guide/)
  1443. * @VueI18nSee [Composition API](../guide/advanced/composition)
  1444. *
  1445. * @example
  1446. * case: for Legacy API
  1447. * ```js
  1448. * import { createApp } from 'vue'
  1449. * import { createI18n } from 'vue-i18n'
  1450. *
  1451. * // call with I18n option
  1452. * const i18n = createI18n({
  1453. * locale: 'ja',
  1454. * messages: {
  1455. * en: { ... },
  1456. * ja: { ... }
  1457. * }
  1458. * })
  1459. *
  1460. * const App = {
  1461. * // ...
  1462. * }
  1463. *
  1464. * const app = createApp(App)
  1465. *
  1466. * // install!
  1467. * app.use(i18n)
  1468. * app.mount('#app')
  1469. * ```
  1470. *
  1471. * @example
  1472. * case: for composition API
  1473. * ```js
  1474. * import { createApp } from 'vue'
  1475. * import { createI18n, useI18n } from 'vue-i18n'
  1476. *
  1477. * // call with I18n option
  1478. * const i18n = createI18n({
  1479. * legacy: false, // you must specify 'legacy: false' option
  1480. * locale: 'ja',
  1481. * messages: {
  1482. * en: { ... },
  1483. * ja: { ... }
  1484. * }
  1485. * })
  1486. *
  1487. * const App = {
  1488. * setup() {
  1489. * // ...
  1490. * const { t } = useI18n({ ... })
  1491. * return { ... , t }
  1492. * }
  1493. * }
  1494. *
  1495. * const app = createApp(App)
  1496. *
  1497. * // install!
  1498. * app.use(i18n)
  1499. * app.mount('#app')
  1500. * ```
  1501. *
  1502. * @VueI18nGeneral
  1503. */
  1504. function createI18n(options = {}) {
  1505. // prettier-ignore
  1506. const __legacyMode = shared.isBoolean(options.legacy)
  1507. ? options.legacy
  1508. : true;
  1509. const __globalInjection = !!options.globalInjection;
  1510. const __instances = new Map();
  1511. // prettier-ignore
  1512. const __global = __legacyMode
  1513. ? createVueI18n(options)
  1514. : createComposer(options);
  1515. const symbol = shared.makeSymbol('vue-i18n' );
  1516. const i18n = {
  1517. // mode
  1518. get mode() {
  1519. // prettier-ignore
  1520. return __legacyMode
  1521. ? 'legacy'
  1522. : 'composition'
  1523. ;
  1524. },
  1525. // install plugin
  1526. async install(app, ...options) {
  1527. // setup global provider
  1528. app.__VUE_I18N_SYMBOL__ = symbol;
  1529. app.provide(app.__VUE_I18N_SYMBOL__, i18n);
  1530. // global method and properties injection for Composition API
  1531. if (!__legacyMode && __globalInjection) {
  1532. injectGlobalFields(app, i18n.global);
  1533. }
  1534. // install built-in components and directive
  1535. {
  1536. apply(app, i18n, ...options);
  1537. }
  1538. // setup mixin for Legacy API
  1539. if (__legacyMode) {
  1540. app.mixin(defineMixin(__global, __global.__composer, i18n));
  1541. }
  1542. },
  1543. // global accessor
  1544. get global() {
  1545. return __global;
  1546. },
  1547. // @internal
  1548. __instances,
  1549. // @internal
  1550. __getInstance(component) {
  1551. return __instances.get(component) || null;
  1552. },
  1553. // @internal
  1554. __setInstance(component, instance) {
  1555. __instances.set(component, instance);
  1556. },
  1557. // @internal
  1558. __deleteInstance(component) {
  1559. __instances.delete(component);
  1560. }
  1561. };
  1562. return i18n;
  1563. }
  1564. /**
  1565. * Use Composition API for Vue I18n
  1566. *
  1567. * @param options - An options, see {@link UseI18nOptions}
  1568. *
  1569. * @returns {@link Composer} instance
  1570. *
  1571. * @remarks
  1572. * This function is mainly used by `setup`.
  1573. *
  1574. * If options are specified, Composer instance is created for each component and you can be localized on the component.
  1575. *
  1576. * If options are not specified, you can be localized using the global Composer.
  1577. *
  1578. * @example
  1579. * case: Component resource base localization
  1580. * ```html
  1581. * <template>
  1582. * <form>
  1583. * <label>{{ t('language') }}</label>
  1584. * <select v-model="locale">
  1585. * <option value="en">en</option>
  1586. * <option value="ja">ja</option>
  1587. * </select>
  1588. * </form>
  1589. * <p>message: {{ t('hello') }}</p>
  1590. * </template>
  1591. *
  1592. * <script>
  1593. * import { useI18n } from 'vue-i18n'
  1594. *
  1595. * export default {
  1596. * setup() {
  1597. * const { t, locale } = useI18n({
  1598. * locale: 'ja',
  1599. * messages: {
  1600. * en: { ... },
  1601. * ja: { ... }
  1602. * }
  1603. * })
  1604. * // Something to do ...
  1605. *
  1606. * return { ..., t, locale }
  1607. * }
  1608. * }
  1609. * </script>
  1610. * ```
  1611. *
  1612. * @VueI18nComposition
  1613. */
  1614. function useI18n(options = {}) {
  1615. const instance = vue.getCurrentInstance();
  1616. if (instance == null) {
  1617. throw createI18nError(16 /* MUST_BE_CALL_SETUP_TOP */);
  1618. }
  1619. if (!instance.appContext.app.__VUE_I18N_SYMBOL__) {
  1620. throw createI18nError(17 /* NOT_INSLALLED */);
  1621. }
  1622. const i18n = vue.inject(instance.appContext.app.__VUE_I18N_SYMBOL__);
  1623. /* istanbul ignore if */
  1624. if (!i18n) {
  1625. throw createI18nError(22 /* UNEXPECTED_ERROR */);
  1626. }
  1627. // prettier-ignore
  1628. const global = i18n.mode === 'composition'
  1629. ? i18n.global
  1630. : i18n.global.__composer;
  1631. // prettier-ignore
  1632. const scope = shared.isEmptyObject(options)
  1633. ? ('__i18n' in instance.type)
  1634. ? 'local'
  1635. : 'global'
  1636. : !options.useScope
  1637. ? 'local'
  1638. : options.useScope;
  1639. if (scope === 'global') {
  1640. let messages = shared.isObject(options.messages) ? options.messages : {};
  1641. if ('__i18nGlobal' in instance.type) {
  1642. messages = getLocaleMessages(global.locale.value, {
  1643. messages,
  1644. __i18n: instance.type.__i18nGlobal
  1645. });
  1646. }
  1647. // merge locale messages
  1648. const locales = Object.keys(messages);
  1649. if (locales.length) {
  1650. locales.forEach(locale => {
  1651. global.mergeLocaleMessage(locale, messages[locale]);
  1652. });
  1653. }
  1654. // merge datetime formats
  1655. if (shared.isObject(options.datetimeFormats)) {
  1656. const locales = Object.keys(options.datetimeFormats);
  1657. if (locales.length) {
  1658. locales.forEach(locale => {
  1659. global.mergeDateTimeFormat(locale, options.datetimeFormats[locale]);
  1660. });
  1661. }
  1662. }
  1663. // merge number formats
  1664. if (shared.isObject(options.numberFormats)) {
  1665. const locales = Object.keys(options.numberFormats);
  1666. if (locales.length) {
  1667. locales.forEach(locale => {
  1668. global.mergeNumberFormat(locale, options.numberFormats[locale]);
  1669. });
  1670. }
  1671. }
  1672. return global;
  1673. }
  1674. if (scope === 'parent') {
  1675. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1676. let composer = getComposer(i18n, instance, options.__useComponent);
  1677. if (composer == null) {
  1678. {
  1679. shared.warn(getWarnMessage(12 /* NOT_FOUND_PARENT_SCOPE */));
  1680. }
  1681. composer = global;
  1682. }
  1683. return composer;
  1684. }
  1685. // scope 'local' case
  1686. if (i18n.mode === 'legacy') {
  1687. throw createI18nError(18 /* NOT_AVAILABLE_IN_LEGACY_MODE */);
  1688. }
  1689. const i18nInternal = i18n;
  1690. let composer = i18nInternal.__getInstance(instance);
  1691. if (composer == null) {
  1692. const type = instance.type;
  1693. const composerOptions = shared.assign({}, options);
  1694. if (type.__i18n) {
  1695. composerOptions.__i18n = type.__i18n;
  1696. }
  1697. if (global) {
  1698. composerOptions.__root = global;
  1699. }
  1700. composer = createComposer(composerOptions);
  1701. setupLifeCycle(i18nInternal, instance);
  1702. i18nInternal.__setInstance(instance, composer);
  1703. }
  1704. return composer;
  1705. }
  1706. function getComposer(i18n, target, useComponent = false) {
  1707. let composer = null;
  1708. const root = target.root;
  1709. let current = target.parent;
  1710. while (current != null) {
  1711. const i18nInternal = i18n;
  1712. if (i18n.mode === 'composition') {
  1713. composer = i18nInternal.__getInstance(current);
  1714. }
  1715. else {
  1716. const vueI18n = i18nInternal.__getInstance(current);
  1717. if (vueI18n != null) {
  1718. composer = vueI18n
  1719. .__composer;
  1720. }
  1721. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1722. if (useComponent && composer && !composer[InejctWithOption]) {
  1723. composer = null;
  1724. }
  1725. }
  1726. if (composer != null) {
  1727. break;
  1728. }
  1729. if (root === current) {
  1730. break;
  1731. }
  1732. current = current.parent;
  1733. }
  1734. return composer;
  1735. }
  1736. function setupLifeCycle(i18n, target, composer) {
  1737. vue.onMounted(() => {
  1738. }, target);
  1739. vue.onUnmounted(() => {
  1740. i18n.__deleteInstance(target);
  1741. }, target);
  1742. }
  1743. const globalExportProps = [
  1744. 'locale',
  1745. 'fallbackLocale',
  1746. 'availableLocales'
  1747. ];
  1748. const globalExportMethods = ['t', 'rt', 'd', 'n', 'tm'];
  1749. function injectGlobalFields(app, composer) {
  1750. const i18n = Object.create(null);
  1751. globalExportProps.forEach(prop => {
  1752. const desc = Object.getOwnPropertyDescriptor(composer, prop);
  1753. if (!desc) {
  1754. throw createI18nError(22 /* UNEXPECTED_ERROR */);
  1755. }
  1756. const wrap = vue.isRef(desc.value) // check computed props
  1757. ? {
  1758. get() {
  1759. return desc.value.value;
  1760. },
  1761. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1762. set(val) {
  1763. desc.value.value = val;
  1764. }
  1765. }
  1766. : {
  1767. get() {
  1768. return desc.get && desc.get();
  1769. }
  1770. };
  1771. Object.defineProperty(i18n, prop, wrap);
  1772. });
  1773. app.config.globalProperties.$i18n = i18n;
  1774. globalExportMethods.forEach(method => {
  1775. const desc = Object.getOwnPropertyDescriptor(composer, method);
  1776. if (!desc || !desc.value) {
  1777. throw createI18nError(22 /* UNEXPECTED_ERROR */);
  1778. }
  1779. Object.defineProperty(app.config.globalProperties, `$${method}`, desc);
  1780. });
  1781. }
  1782. // register message compiler at vue-i18n
  1783. coreBase.registerMessageCompiler(coreBase.compileToFunction);
  1784. // NOTE: experimental !!
  1785. {
  1786. const target = shared.getGlobalThis();
  1787. target.__INTLIFY__ = true;
  1788. coreBase.setDevToolsHook(target.__INTLIFY_DEVTOOLS_GLOBAL_HOOK__);
  1789. }
  1790. exports.DatetimeFormat = DatetimeFormat;
  1791. exports.NumberFormat = NumberFormat;
  1792. exports.Translation = Translation;
  1793. exports.VERSION = VERSION;
  1794. exports.createI18n = createI18n;
  1795. exports.useI18n = useI18n;
  1796. exports.vTDirective = vTDirective;