Skip to content

Commit d85b11a

Browse files
authored
fix: te fallback to root (#1613)
1 parent 605df05 commit d85b11a

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

packages/core-base/src/runtime.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ export type FallbackLocale =
3838
| { [locale in string]: Locale[] }
3939
| false
4040

41-
export type CoreMissingType = 'translate' | 'datetime format' | 'number format'
41+
export type CoreMissingType =
42+
| 'translate'
43+
| 'datetime format'
44+
| 'number format'
45+
| 'translate exists'
4246

4347
export type MessageType<T = string> = T extends string
4448
? string

packages/vue-i18n-core/src/composer.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,7 @@ export interface ComposerInternal {
17631763
type ComposerWarnType = CoreMissingType
17641764

17651765
const NOOP_RETURN_ARRAY = () => []
1766+
const NOOP_RETURN_FALSE = () => false
17661767

17671768
let composerID = 0
17681769

@@ -2173,7 +2174,12 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
21732174
_context.fallbackContext = undefined
21742175
}
21752176
}
2176-
if (isNumber(ret) && ret === NOT_REOSLVED) {
2177+
if (
2178+
(warnType !== 'translate exists' && // for not `te` (e.g `t`)
2179+
isNumber(ret) &&
2180+
ret === NOT_REOSLVED) ||
2181+
(warnType === 'translate exists' && !ret) // for `te`
2182+
) {
21772183
const [key, arg2] = argumentParser()
21782184
if (
21792185
__DEV__ &&
@@ -2341,6 +2347,30 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
23412347

23422348
// te
23432349
function te(key: Path, locale?: Locale): boolean {
2350+
return wrapWithDeps<{}, boolean>(
2351+
() => {
2352+
if (!key) {
2353+
return false
2354+
}
2355+
const targetLocale = isString(locale) ? locale : _locale.value
2356+
const message = getLocaleMessage(targetLocale)
2357+
const resolved = _context.messageResolver(message, key)
2358+
return (
2359+
isMessageAST(resolved) ||
2360+
isMessageFunction(resolved) ||
2361+
isString(resolved)
2362+
)
2363+
},
2364+
() => [key],
2365+
'translate exists',
2366+
root => {
2367+
console.log('root ... te')
2368+
return Reflect.apply(root.te, root, [key, locale])
2369+
},
2370+
NOOP_RETURN_FALSE,
2371+
val => isBoolean(val)
2372+
)
2373+
/*
23442374
if (!key) {
23452375
return false
23462376
}
@@ -2352,6 +2382,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
23522382
isMessageFunction(resolved) ||
23532383
isString(resolved)
23542384
)
2385+
*/
23552386
}
23562387

23572388
function resolveMessages(key: Path): LocaleMessageValue<Message> | null {

packages/vue-i18n-core/test/composer.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,16 @@ describe('tm', () => {
12991299
})
13001300

13011301
test('te', async () => {
1302+
const __root = createComposer({
1303+
locale: 'en',
1304+
messages: {
1305+
en: {
1306+
message: {
1307+
fallback: 'fallback'
1308+
}
1309+
}
1310+
}
1311+
})
13021312
const { te } = createComposer({
13031313
locale: 'en',
13041314
messages: {
@@ -1307,12 +1317,14 @@ test('te', async () => {
13071317
hello: 'Hello!'
13081318
}
13091319
}
1310-
}
1320+
},
1321+
__root
13111322
})
13121323

13131324
expect(te('message.hello')).toEqual(true)
1314-
expect(te('message.hallo')).toEqual(false)
1315-
expect(te('message.hallo', 'ja' as any)).toEqual(false)
1325+
expect(te('message.fallback')).toEqual(true) // fallback
1326+
expect(te('message.hallo')).toEqual(false) // missing for 'en' and 'ja'
1327+
expect(te('message.hallo', 'ja' as any)).toEqual(false) // missing for 'ja'
13161328

13171329
expect(te(null as any)).toEqual(false)
13181330
expect(te(undefined as any)).toEqual(false)

0 commit comments

Comments
 (0)