kazupon / vue-i18n

:globe_with_meridians: Internationalization plugin for Vue.js
https://kazupon.github.io/vue-i18n/
MIT License
7.28k stars 861 forks source link

$te returns false if the key contains dot #1521

Open LasyIsLazy opened 2 years ago

LasyIsLazy commented 2 years ago

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

domoratskii commented 2 years ago

Hi! I have the same error. When using a dot in the key - te always returns false. Version ^9.2.0-beta.36.

Kilbourne commented 2 years ago

Same problem here. Please support it ( maybe just an option to choose the key separator)

mkrajinov commented 2 years ago

Same for me.

cnguyen-de commented 2 years ago

same, had to remove $te check in the if before, was wondering why i18n didn't work after dependency update

Yummy-zZ commented 1 year ago

when my key is Chinese,it can't match.

dmcknight26 commented 1 year ago

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);
};
Eternal-Rise commented 1 year ago

Still not fixed :(

MindaugasR commented 1 year ago

@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)
}
jclaveau commented 1 year ago

@dmcknight26 @MindaugasR

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)
}
NamesMT commented 10 months ago

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

rna2i commented 3 months ago

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#271

We are facing the same issue.