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

te function returns false although fallback exists #1765

Open
4 tasks done
valoricDe opened this issue Mar 13, 2024 · 5 comments
Open
4 tasks done

te function returns false although fallback exists #1765

valoricDe opened this issue Mar 13, 2024 · 5 comments
Labels
Status: Proposal Request for comments

Comments

@valoricDe
Copy link

Reporting a bug?

te function returns false for de-AT locale although t function is able to fallback to language de.

Expected behavior

I think it is problematic that the behaviour of t and te is different.
I propose:
if you provide no locale to te function it should be true if t function resolves to a fallback (eg. de-AT -> de)
if you provide a locale it should only return true if the message exists in the given locale ignoring fallbacks

Reproduction

https://stackblitz.com/edit/vitejs-vite-tr2dcd?file=src%2FApp.vue

System Info

System:
    OS: macOS 14.2
    CPU: (8) arm64 Apple M1 Pro
    Memory: 95.63 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v20.9.0/bin/yarn
    npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
  Browsers:
    Chrome: 122.0.6261.112
    Edge: 113.0.1774.35
    Safari: 17.2

Screenshot

Bildschirmfoto 2024-03-13 um 12 09 34

Additional context

No response

Validations

@valoricDe valoricDe added the Status: Review Needed Request for review comments label Mar 13, 2024
@kazupon kazupon added Status: Proposal Request for comments and removed Status: Review Needed Request for review comments labels Mar 14, 2024
@kazupon
Copy link
Member

kazupon commented Mar 14, 2024

Thank you for your feeback!

As the reason provide te, simply because we want to detect if a message exists for the current local, or for the locale specified in the parameters.

The t function has a fallback cost.
To avoid that cost, and to avoid missing translations, this API is provided.

@valoricDe
Copy link
Author

I think we agree that there are different UseCases.
In our case by using te we want to know if a translation exists and if there is a translation fallback. We are fine with having the additional cost (as it is inevitable in our case). If the key not exists we try another key.

Our own fallback translationExists function looks like this now:

function translateOrEmpty(i18n, key) {
  const translation = i18n.t(key);
  if (translation === key) return '';
  return translation;
}

But it raises an error for missing transations in our test cases.

We could also create our own fallback schema for te but it seems weird recreating the logik of t.

What are your thoughts on this?

@BobbieGoede
Copy link
Member

Expected behavior
I think it is problematic that the behaviour of t and te is different.

Would something like this be what you're looking for?

function hasTranslationOrFallback(i18n, key) {
  return i18n.te(key) || i18n.te(key, i18n.fallbackLocale.value)
}

@valoricDe
Copy link
Author

valoricDe commented Mar 18, 2024

As far as I know you can define multiple fallback levels (de-DE-BR -> de-AT -> de -> en). So it would need to be iterative.
Edit: https://vue-i18n.intlify.dev/guide/essentials/fallback.html#explicit-fallback-with-decision-maps

@valoricDe
Copy link
Author

Thinking about it. I actually don't care about the te function. Because in my eyes it is more efficient to directly try to translate and check if I got the key returned than first checking if it exists and then translate it (-> 2x resolving).

So I silenced the warnings I get in the jest tests. If you agree we can close this ticket

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Proposal Request for comments
Projects
None yet
Development

No branches or pull requests

3 participants