Skip to content

Commit d59d858

Browse files
authored
feat: legacy typings (#44)
* update core/context * feat: define legacy typings
1 parent ef670a7 commit d59d858

File tree

3 files changed

+134
-3
lines changed

3 files changed

+134
-3
lines changed

src/core/context.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ function appendBlockToChain(
260260
): unknown {
261261
let follow: unknown = true
262262
for (let i = 0; i < block.length && isBoolean(follow); i++) {
263-
follow = appendLocaleToChain(chain, block[i], blocks)
263+
const locale = block[i]
264+
if (isString(locale)) {
265+
follow = appendLocaleToChain(chain, block[i], blocks)
266+
}
264267
}
265268
return follow
266269
}

src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ export {
2323
PostTranslationHandler
2424
} from './core'
2525
export * from './core/types'
26-
export { MissingHandler, ComposerOptions, Composer } from './composer'
26+
export {
27+
MissingHandler,
28+
ComposerOptions,
29+
Composer,
30+
CustomBlocks
31+
} from './composer'
2732
export {
2833
TranslateResult,
2934
Choice,

src/mixin.ts

+124-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ComponentOptions, getCurrentInstance } from 'vue'
22
import { Path } from './path'
33
import { Locale } from './core/context'
4-
import { Composer, ComposerInternalOptions } from './composer'
4+
import { Composer, ComposerInternalOptions, CustomBlocks } from './composer'
55
import {
66
VueI18n,
77
VueI18nInternal,
@@ -13,6 +13,129 @@ import {
1313
} from './legacy'
1414
import { I18nInternal } from './i18n'
1515

16+
declare module '@vue/runtime-core' {
17+
interface ComponentCustomOptions {
18+
/**
19+
* VueI18n options
20+
*
21+
* @remarks
22+
* See the {@link VueI18nOptions}
23+
*/
24+
i18n?: VueI18nOptions
25+
/**
26+
* For custom blocks options
27+
* @internal
28+
*/
29+
__i18n?: CustomBlocks
30+
}
31+
interface ComponentCustomProperties {
32+
/**
33+
* VueI18n class compatible interface. See {@link VueI18n}
34+
*
35+
* @remarks
36+
* If you have specified an `i18n` option at component options,
37+
* you will be able to get a VueI18n instance at the component,
38+
* Otherwise, you will be able get root VueI18n instance.
39+
*
40+
* This property is supported for legacy API only
41+
*/
42+
$i18n?: VueI18n
43+
/**
44+
* translation method
45+
*
46+
* @param key - required, type {@link Path}
47+
* @param locale - optional, type {@link Locale}
48+
* @param values - optional, type `Array` or `Object`
49+
* @returns translated string
50+
*
51+
* @remarks
52+
* Localize the locale message of `key`.
53+
* Localize in preferentially component locale messages than global locale messages.
54+
* If not specified component locale messages, localize with global locale messages.
55+
* If you specified `locale`, localize the locale messages of `locale`.
56+
* If you specified `key` of list / named formatting local messages, you must specify `values` too.
57+
*
58+
* This property is supported for legacy API only
59+
*/
60+
$t?: (...args: unknown[]) => TranslateResult
61+
/**
62+
* pluralization method
63+
*
64+
* @param key - required, type {@link Path}
65+
* @param choice - optional, type `number`, default `1`
66+
* @param locale - optional, type {@link Locale}
67+
* @param values - optional, type `string` or `Array` or `Object`
68+
* @returns pluralized string
69+
*
70+
* @remarks
71+
* Localize the locale message of `key` with pluralization.
72+
* Localize in preferentially component locale messages than global locale messages.
73+
* If not specified component locale messages, localize with global locale messages.
74+
* If you specified `locale`, localize the locale messages of `locale`.
75+
* If you will specify string value to `values`, localize the locale messages of value.
76+
* If you will specify Array or Object value to `values`, you must specify with `values` of `$t`.
77+
*
78+
* This property is supported for legacy API only
79+
*/
80+
$tc?: (...args: unknown[]) => TranslateResult
81+
/**
82+
* translation exist method
83+
*
84+
* @param key - required, type {@link Path}
85+
* @param locale - optional, type {@link Locale}
86+
* @returns key exsiting result
87+
*
88+
* @remarks
89+
* Check whether key exists.
90+
* In Vue instance, If not specified component locale messages,
91+
* check with global locale messages. If you specified `locale`, check the locale messages of `locale`
92+
*
93+
* This property is supported for legacy API only
94+
*/
95+
$te?: (key: Path, locale?: Locale) => boolean
96+
/**
97+
* datetime method
98+
*
99+
* @param value - required, type `number` or `Date`
100+
* @param key - optional, type {@link Path} or `Object`
101+
* @param locale - optional, type {@link Locale}
102+
* @returns formatted datetime result
103+
*
104+
* @remarks
105+
* Localize the datetime of `value` with datetime format of `key`.
106+
* The datetime format of `key` need to register to `dateTimeFormats` option of {@link VueI18nOptions},
107+
* and depend on `locale` option of {@link VueI18nOptions}.
108+
* If you will specify locale argument, it will have priority over `locale` of {@link VueI18nOptions}.
109+
*
110+
* If the datetime format of `key` not exist in `dateTimeFormats` option,
111+
* fallback to depend on `fallbackLocale` of {@link VueI18nOptions}.
112+
*
113+
* This property is supported for legacy API only
114+
*/
115+
$d?: (...args: unknown[]) => DateTimeFormatResult
116+
/**
117+
* number method
118+
*
119+
* @param value - required, type `number`
120+
* @param format - optional, type {@link Path} or `Object`
121+
* @param locale - optional, type {@link Locale}
122+
* @returns formatted number result
123+
*
124+
* @remarks
125+
* Localize the number of `value` with number format of `format`.
126+
* The number format of `format` need to register to `numberFormats` option of {@link VueI18nOptions},
127+
* and depend on `locale` option of {@link VueI18nOptions}.
128+
* If you will specify `locale` argument, it will have priority over `locale` option of {@link VueI18nOptions}
129+
*
130+
* If the number format of `format` not exist in `numberFormats` option,
131+
* fallback to depend on `fallbackLocale` option of {@link VueI18nOptions}
132+
*
133+
* This property is supported for legacy API only
134+
*/
135+
$n?: (...args: unknown[]) => NumberFormatResult
136+
}
137+
}
138+
16139
// supports compatibility for legacy vue-i18n APIs
17140
export function defineMixin(
18141
legacy: VueI18n & VueI18nInternal,

0 commit comments

Comments
 (0)