Skip to content

Commit

Permalink
fix: te fallback to root (#1613)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Oct 27, 2023
1 parent 605df05 commit d85b11a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
6 changes: 5 additions & 1 deletion packages/core-base/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export type FallbackLocale =
| { [locale in string]: Locale[] }
| false

export type CoreMissingType = 'translate' | 'datetime format' | 'number format'
export type CoreMissingType =
| 'translate'
| 'datetime format'
| 'number format'
| 'translate exists'

export type MessageType<T = string> = T extends string
? string
Expand Down
33 changes: 32 additions & 1 deletion packages/vue-i18n-core/src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,7 @@ export interface ComposerInternal {
type ComposerWarnType = CoreMissingType

const NOOP_RETURN_ARRAY = () => []
const NOOP_RETURN_FALSE = () => false

let composerID = 0

Expand Down Expand Up @@ -2173,7 +2174,12 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
_context.fallbackContext = undefined
}
}
if (isNumber(ret) && ret === NOT_REOSLVED) {
if (
(warnType !== 'translate exists' && // for not `te` (e.g `t`)
isNumber(ret) &&
ret === NOT_REOSLVED) ||
(warnType === 'translate exists' && !ret) // for `te`
) {
const [key, arg2] = argumentParser()
if (
__DEV__ &&
Expand Down Expand Up @@ -2341,6 +2347,30 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {

// te
function te(key: Path, locale?: Locale): boolean {
return wrapWithDeps<{}, boolean>(
() => {
if (!key) {
return false
}
const targetLocale = isString(locale) ? locale : _locale.value
const message = getLocaleMessage(targetLocale)
const resolved = _context.messageResolver(message, key)
return (
isMessageAST(resolved) ||
isMessageFunction(resolved) ||
isString(resolved)
)
},
() => [key],
'translate exists',
root => {
console.log('root ... te')
return Reflect.apply(root.te, root, [key, locale])
},
NOOP_RETURN_FALSE,
val => isBoolean(val)
)
/*
if (!key) {
return false
}
Expand All @@ -2352,6 +2382,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
isMessageFunction(resolved) ||
isString(resolved)
)
*/
}

function resolveMessages(key: Path): LocaleMessageValue<Message> | null {
Expand Down
18 changes: 15 additions & 3 deletions packages/vue-i18n-core/test/composer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,16 @@ describe('tm', () => {
})

test('te', async () => {
const __root = createComposer({
locale: 'en',
messages: {
en: {
message: {
fallback: 'fallback'
}
}
}
})
const { te } = createComposer({
locale: 'en',
messages: {
Expand All @@ -1307,12 +1317,14 @@ test('te', async () => {
hello: 'Hello!'
}
}
}
},
__root
})

expect(te('message.hello')).toEqual(true)
expect(te('message.hallo')).toEqual(false)
expect(te('message.hallo', 'ja' as any)).toEqual(false)
expect(te('message.fallback')).toEqual(true) // fallback
expect(te('message.hallo')).toEqual(false) // missing for 'en' and 'ja'
expect(te('message.hallo', 'ja' as any)).toEqual(false) // missing for 'ja'

expect(te(null as any)).toEqual(false)
expect(te(undefined as any)).toEqual(false)
Expand Down

0 comments on commit d85b11a

Please sign in to comment.