Skip to content

Commit 0add252

Browse files
authored
fix: return null for fucntion (#1617)
* fix: return null for fucntion * fix: remove comment
1 parent 1627101 commit 0add252

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

packages/core-base/src/resolver.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isObject } from '@intlify/shared'
1+
import { isObject, isFunction } from '@intlify/shared'
22

33
/** @VueI18nGeneral */
44
export type Path = string
@@ -348,6 +348,9 @@ export function resolveValue(obj: unknown, path: Path): PathValue {
348348
if (val === undefined) {
349349
return null
350350
}
351+
if (isFunction(last)) {
352+
return null
353+
}
351354
last = val
352355
i++
353356
}

packages/core-base/test/resolver.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,9 @@ test('resolveValue', () => {
120120
expect(resolveValue({}, 'a.b.c[]')).toEqual(null)
121121
// blanket middle
122122
expect(resolveValue({}, 'a.b.c[]d')).toEqual(null)
123+
// function
124+
const fn = () => 1
125+
expect(resolveValue({ a: fn }, 'a.name')).toEqual(null)
126+
expect(resolveValue({ a: fn }, 'a.toString')).toEqual(null)
127+
expect(resolveValue({ a: fn }, 'a')).toEqual(fn)
123128
})

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,25 +2364,11 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
23642364
() => [key],
23652365
'translate exists',
23662366
root => {
2367-
console.log('root ... te')
23682367
return Reflect.apply(root.te, root, [key, locale])
23692368
},
23702369
NOOP_RETURN_FALSE,
23712370
val => isBoolean(val)
23722371
)
2373-
/*
2374-
if (!key) {
2375-
return false
2376-
}
2377-
const targetLocale = isString(locale) ? locale : _locale.value
2378-
const message = getLocaleMessage(targetLocale)
2379-
const resolved = _context.messageResolver(message, key)
2380-
return (
2381-
isMessageAST(resolved) ||
2382-
isMessageFunction(resolved) ||
2383-
isString(resolved)
2384-
)
2385-
*/
23862372
}
23872373

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

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ test('issue #1595 merge case', async () => {
11981198
)
11991199
})
12001200

1201-
test('issue #1610 merge case', async () => {
1201+
test('issue #1610', async () => {
12021202
const en = {
12031203
hello: 'Hello, Vue I18n',
12041204
language: 'Languages'
@@ -1227,3 +1227,49 @@ test('issue #1610 merge case', async () => {
12271227
`<h1>Hello, Vue I18n</h1> true (...but this should be true)`
12281228
)
12291229
})
1230+
1231+
test('issue #1615', async () => {
1232+
console.log('----')
1233+
const en = {
1234+
hello: (() => {
1235+
const fn = ctx => {
1236+
const { normalize: _normalize } = ctx
1237+
return _normalize(['Hello, Vue I18n'])
1238+
}
1239+
fn.source = 'Hello, Vue I18n'
1240+
return fn
1241+
})(),
1242+
language: (() => {
1243+
const fn = ctx => {
1244+
const { normalize: _normalize } = ctx
1245+
return _normalize(['Languages'])
1246+
}
1247+
fn.source = 'Languages'
1248+
return fn
1249+
})()
1250+
}
1251+
const i18n = createI18n({
1252+
legacy: false,
1253+
locale: 'en',
1254+
globalInjection: true,
1255+
messages: {
1256+
en: {}
1257+
}
1258+
})
1259+
1260+
const App = defineComponent({
1261+
template: `
1262+
<h1>{{ $t('hello.name') }}</h1>
1263+
<p>(( "hello.name" does not exist. correct path would just be "hello")</p>
1264+
<p id="te">{{ $te('hello.name') }} (...but this should be false)</p>
1265+
`
1266+
})
1267+
const wrapper = await mount(App, i18n)
1268+
1269+
i18n.global.setLocaleMessage('en', en)
1270+
await nextTick()
1271+
1272+
expect(wrapper.find('#te')?.textContent).toEqual(
1273+
`false (...but this should be false)`
1274+
)
1275+
})

0 commit comments

Comments
 (0)