Skip to content

Commit 605df05

Browse files
authored
fix: more te logic strictly (#1612)
1 parent 337c009 commit 605df05

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ import {
3434
setAdditionalMeta,
3535
getFallbackContext,
3636
setFallbackContext,
37-
DEFAULT_LOCALE
37+
DEFAULT_LOCALE,
38+
isMessageAST,
39+
isMessageFunction
3840
} from '@intlify/core-base'
3941
import { VueDevToolsTimelineEvents } from '@intlify/vue-devtools'
4042
import { I18nWarnCodes, getWarnMessage } from './warnings'
@@ -108,7 +110,8 @@ import type {
108110
RemoveIndexSignature,
109111
RemovedIndexResources,
110112
IsNever,
111-
IsEmptyObject
113+
IsEmptyObject,
114+
CoreMissingType
112115
} from '@intlify/core-base'
113116
import type { VueDevToolsEmitter } from '@intlify/vue-devtools'
114117
import { isLegacyVueI18n } from './utils'
@@ -1757,7 +1760,9 @@ export interface ComposerInternal {
17571760
__setPluralRules(rules: PluralizationRules): void
17581761
}
17591762

1760-
type ComposerWarnType = 'translate' | 'number format' | 'datetime format'
1763+
type ComposerWarnType = CoreMissingType
1764+
1765+
const NOOP_RETURN_ARRAY = () => []
17611766

17621767
let composerID = 0
17631768

@@ -2309,7 +2314,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
23092314
'number format',
23102315
// eslint-disable-next-line @typescript-eslint/no-explicit-any
23112316
root => (root as any)[NumberPartsSymbol](...args),
2312-
() => [],
2317+
NOOP_RETURN_ARRAY,
23132318
val => isString(val) || isArray(val)
23142319
)
23152320
}
@@ -2324,7 +2329,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
23242329
'datetime format',
23252330
// eslint-disable-next-line @typescript-eslint/no-explicit-any
23262331
root => (root as any)[DatetimePartsSymbol](...args),
2327-
() => [],
2332+
NOOP_RETURN_ARRAY,
23282333
val => isString(val) || isArray(val)
23292334
)
23302335
}
@@ -2336,10 +2341,17 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
23362341

23372342
// te
23382343
function te(key: Path, locale?: Locale): boolean {
2339-
if (!key) return false
2344+
if (!key) {
2345+
return false
2346+
}
23402347
const targetLocale = isString(locale) ? locale : _locale.value
23412348
const message = getLocaleMessage(targetLocale)
2342-
return isString(_context.messageResolver(message, key))
2349+
const resolved = _context.messageResolver(message, key)
2350+
return (
2351+
isMessageAST(resolved) ||
2352+
isMessageFunction(resolved) ||
2353+
isString(resolved)
2354+
)
23432355
}
23442356

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

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,3 +1197,33 @@ test('issue #1595 merge case', async () => {
11971197
'<form><select><option value="en">en</option><option value="ja">ja</option></select></form> シンプル ディープ'
11981198
)
11991199
})
1200+
1201+
test('issue #1610 merge case', async () => {
1202+
const en = {
1203+
hello: 'Hello, Vue I18n',
1204+
language: 'Languages'
1205+
}
1206+
const i18n = createI18n({
1207+
legacy: false,
1208+
locale: 'en',
1209+
globalInjection: true,
1210+
messages: {
1211+
en: {}
1212+
}
1213+
})
1214+
1215+
const App = defineComponent({
1216+
template: `
1217+
<h1>{{ $t('hello') }}</h1>
1218+
{{ $te('hello') }} (...but this should be true)
1219+
`
1220+
})
1221+
const wrapper = await mount(App, i18n)
1222+
1223+
i18n.global.setLocaleMessage('en', en)
1224+
await nextTick()
1225+
1226+
expect(wrapper.html()).include(
1227+
`<h1>Hello, Vue I18n</h1> true (...but this should be true)`
1228+
)
1229+
})

0 commit comments

Comments
 (0)