Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: more te logic strictly #1612

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions packages/vue-i18n-core/src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
setAdditionalMeta,
getFallbackContext,
setFallbackContext,
DEFAULT_LOCALE
DEFAULT_LOCALE,
isMessageAST,
isMessageFunction
} from '@intlify/core-base'
import { VueDevToolsTimelineEvents } from '@intlify/vue-devtools'
import { I18nWarnCodes, getWarnMessage } from './warnings'
Expand Down Expand Up @@ -108,7 +110,8 @@ import type {
RemoveIndexSignature,
RemovedIndexResources,
IsNever,
IsEmptyObject
IsEmptyObject,
CoreMissingType
} from '@intlify/core-base'
import type { VueDevToolsEmitter } from '@intlify/vue-devtools'
import { isLegacyVueI18n } from './utils'
Expand Down Expand Up @@ -1757,7 +1760,9 @@ export interface ComposerInternal {
__setPluralRules(rules: PluralizationRules): void
}

type ComposerWarnType = 'translate' | 'number format' | 'datetime format'
type ComposerWarnType = CoreMissingType

const NOOP_RETURN_ARRAY = () => []

let composerID = 0

Expand Down Expand Up @@ -2309,7 +2314,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
'number format',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
root => (root as any)[NumberPartsSymbol](...args),
() => [],
NOOP_RETURN_ARRAY,
val => isString(val) || isArray(val)
)
}
Expand All @@ -2324,7 +2329,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
'datetime format',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
root => (root as any)[DatetimePartsSymbol](...args),
() => [],
NOOP_RETURN_ARRAY,
val => isString(val) || isArray(val)
)
}
Expand All @@ -2336,10 +2341,17 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {

// te
function te(key: Path, locale?: Locale): boolean {
if (!key) return false
if (!key) {
return false
}
const targetLocale = isString(locale) ? locale : _locale.value
const message = getLocaleMessage(targetLocale)
return isString(_context.messageResolver(message, key))
const resolved = _context.messageResolver(message, key)
return (
isMessageAST(resolved) ||
isMessageFunction(resolved) ||
isString(resolved)
)
}

function resolveMessages(key: Path): LocaleMessageValue<Message> | null {
Expand Down
30 changes: 30 additions & 0 deletions packages/vue-i18n-core/test/issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1197,3 +1197,33 @@ test('issue #1595 merge case', async () => {
'<form><select><option value="en">en</option><option value="ja">ja</option></select></form> シンプル ディープ'
)
})

test('issue #1610 merge case', async () => {
const en = {
hello: 'Hello, Vue I18n',
language: 'Languages'
}
const i18n = createI18n({
legacy: false,
locale: 'en',
globalInjection: true,
messages: {
en: {}
}
})

const App = defineComponent({
template: `
<h1>{{ $t('hello') }}</h1>
{{ $te('hello') }} (...but this should be true)
`
})
const wrapper = await mount(App, i18n)

i18n.global.setLocaleMessage('en', en)
await nextTick()

expect(wrapper.html()).include(
`<h1>Hello, Vue I18n</h1> true (...but this should be true)`
)
})