$te returns false if the key contains dot
LasyIsLazy opened this issue · comments
Reporting a bug?
If key
has dot like a.b.c
, $t('a.b.c')
will get the correct result, but $te
will return false.
Expected behavior
$te
return true
Reproduction
{
"a.b.c": "msg"
}
$t('a.b.c')
return false
System Info
System:
OS: macOS 12.4
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 88.95 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 14.16.1 - ~/.nvm/versions/node/v14.16.1/bin/node
npm: 8.5.5 - ~/.nvm/versions/node/v14.16.1/bin/npm
Browsers:
Chrome: 103.0.5060.53
Chrome Canary: 105.0.5152.0
Firefox: 86.0.1
Safari: 15.5
npmPackages:
@vue/cli-plugin-babel: ^4.5.15 => 4.5.15
@vue/cli-plugin-eslint: ^4.5.4 => 4.5.15
@vue/cli-plugin-typescript: ^4.5.4 => 4.5.15
@vue/cli-service: ^4.5.4 => 4.5.15
@vue/composition-api: ^1.4.9 => 1.5.0
vue: ^2.6.14 => 2.6.14
vue-class-component: ^7.2.3 => 7.2.6
vue-eslint-parser: ^8.0.1 => 8.0.1
vue-jest: 3.0.7 => 3.0.7
vue-property-decorator: ^9.1.2 => 9.1.2
vue-router: ^3.1.2 => 3.5.3
vue-template-compiler: ^2.6.11 => 2.6.14
vuex: ^3.4.0 => 3.6.2
vuex-class: ^0.3.2 => 0.3.2
Screenshot
No response
Additional context
No response
Validations
- Read the Contributing Guidelines
- Read the Documentation
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion
- The provided reproduction is a minimal reproducible example of the bug.
Hi! I have the same error. When using a dot in the key - te
always returns false. Version ^9.2.0-beta.36
.
Same problem here. Please support it ( maybe just an option to choose the key separator)
Same for me.
same, had to remove $te check in the if before, was wondering why i18n didn't work after dependency update
when my key is Chinese,it can't match.
Same issue - using this workaround for now...
import _ from 'lodash';
i18n.global.te = (key, locale) => {
let effectiveLocale= !_.isEmpty(locale) ? locale : i18n.global.locale;
let messages = i18n.global.messages[effectiveLocale];
return Object.hasOwn(messages, key);
};
Still not fixed :(
@dmcknight26 solution works, some changes:
import { isEmpty } from 'lodash'
i18n.global.te = (key: string, locale: string) => {
const effectiveLocale = !isEmpty(locale) ? locale : i18n.global.locale.value
const messages = i18n.global.messages.value[effectiveLocale]
return Object.hasOwn(messages, key)
}
Thank you for your solution, here is the Typescript support (also without lodash dependency):
export const i18n = createI18n({
locale: `fr-FR`,
legacy: false,
warnHtmlMessage: false,
messages,
})
i18n.global.te = (key: Parameters<typeof i18n.global.te>[0], locale: Parameters<typeof i18n.global.te>[1]) => {
const effectiveLocale = locale && locale.length ? locale : i18n.global.locale.value
const messages = i18n.global.messages.value[effectiveLocale]
return Object.hasOwn(messages, key)
}
Bump, I'm using a flattened json for i18n,
For something like spotlight.title
, using t('spotlight.title')
works, but rt('@:spotlight.title')
doesn't, weirdly inconsistent.
Refs for attention: intlify/vue-i18n-next#271