Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: return null for fucntion #1617

Merged
merged 2 commits 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
5 changes: 4 additions & 1 deletion packages/core-base/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject } from '@intlify/shared'
import { isObject, isFunction } from '@intlify/shared'

/** @VueI18nGeneral */
export type Path = string
Expand Down Expand Up @@ -348,6 +348,9 @@ export function resolveValue(obj: unknown, path: Path): PathValue {
if (val === undefined) {
return null
}
if (isFunction(last)) {
return null
}
last = val
i++
}
Expand Down
5 changes: 5 additions & 0 deletions packages/core-base/test/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,9 @@ test('resolveValue', () => {
expect(resolveValue({}, 'a.b.c[]')).toEqual(null)
// blanket middle
expect(resolveValue({}, 'a.b.c[]d')).toEqual(null)
// function
const fn = () => 1
expect(resolveValue({ a: fn }, 'a.name')).toEqual(null)
expect(resolveValue({ a: fn }, 'a.toString')).toEqual(null)
expect(resolveValue({ a: fn }, 'a')).toEqual(fn)
})
14 changes: 0 additions & 14 deletions packages/vue-i18n-core/src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2364,25 +2364,11 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
() => [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
}
const targetLocale = isString(locale) ? locale : _locale.value
const message = getLocaleMessage(targetLocale)
const resolved = _context.messageResolver(message, key)
return (
isMessageAST(resolved) ||
isMessageFunction(resolved) ||
isString(resolved)
)
*/
}

function resolveMessages(key: Path): LocaleMessageValue<Message> | null {
Expand Down
48 changes: 47 additions & 1 deletion packages/vue-i18n-core/test/issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ test('issue #1595 merge case', async () => {
)
})

test('issue #1610 merge case', async () => {
test('issue #1610', async () => {
const en = {
hello: 'Hello, Vue I18n',
language: 'Languages'
Expand Down Expand Up @@ -1227,3 +1227,49 @@ test('issue #1610 merge case', async () => {
`<h1>Hello, Vue I18n</h1> true (...but this should be true)`
)
})

test('issue #1615', async () => {
console.log('----')
const en = {
hello: (() => {
const fn = ctx => {
const { normalize: _normalize } = ctx
return _normalize(['Hello, Vue I18n'])
}
fn.source = 'Hello, Vue I18n'
return fn
})(),
language: (() => {
const fn = ctx => {
const { normalize: _normalize } = ctx
return _normalize(['Languages'])
}
fn.source = 'Languages'
return fn
})()
}
const i18n = createI18n({
legacy: false,
locale: 'en',
globalInjection: true,
messages: {
en: {}
}
})

const App = defineComponent({
template: `
<h1>{{ $t('hello.name') }}</h1>
<p>(( "hello.name" does not exist. correct path would just be "hello")</p>
<p id="te">{{ $te('hello.name') }} (...but this should be false)</p>
`
})
const wrapper = await mount(App, i18n)

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

expect(wrapper.find('#te')?.textContent).toEqual(
`false (...but this should be false)`
)
})