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

Bug with t() and <T /> when set language option (translate in context language instead parameter language if parameter language is not cached yet) #3230

Closed
tpoisseau opened this issue Aug 9, 2023 · 4 comments

Comments

@tpoisseau
Copy link
Contributor

tpoisseau commented Aug 9, 2023

Hello,

You'll find a complete reproduce bug example here :
https://stackblitz.com/edit/gpbse5j-tolgee-language-param-wycsuw?file=src%2FApp.tsx

When you setup language option in t() or <T /> and this language is not cached by tolgee yet, it display the translation in language from tolgee context.

My workaround is a hook calling tolgee.loadRecord in components where I need to display some translations from an entity language parameter. but it's quite annoying.

export function useTolgeeCacheLoad(
  language: string | undefined,
  namespace?: string,
) {
  const tolgee = useTolgee();

  useEffect(() => {
    if (!language) return;
    tolgee.loadRecord({ language, namespace }).catch(reportError);
  }, [tolgee, language, namespace]);
}

SDK should take care of language param and call loadRecord if needed (as explained in doc)

language: string - override current Tolgee language. This way you can switch to different language for separate translation. Load the language manually with tolgee.loadRecord.

https://tolgee.io/js-sdk/api/core_package/tolgee#t

Best regards,

@tpoisseau tpoisseau changed the title Bug with t() and <T /> when set language option Bug with t() and <T /> when set language option (translate in context language instead parameter language if parameter language is not cached yet) Aug 9, 2023
@stepan662
Copy link
Collaborator

Hey, I think this is not a bug. But I guess it's a bit confusing.

Tolgee doesn't load language automatically when you call t with the language property. It is stated in docs, that you have to load the language yourself. It will only override the current language set in tolgee and look at the cache.

Since the fallback language is set to en if the translation is not present it will fallback to english. So if the French is not loaded, it will display english.

@tpoisseau
Copy link
Contributor Author

Hello,

So I think the wording of the documentation is unclear. My understanding of doc was tolgee will loadRecord if needed. And I thought it's a handy approach.

As a user of tolgee api, I think I should not have to worry about the cache (and advanced features) of tolgee, when I use the basic api to translate. So my suggestion to you is to take care of this usage so auto loadRecord if needed.

If for you the API is good like this, your product your rules and no problem, but please at least improve the wording of documentation. maybe something like

You have to load the language translations manually with tolgee.loadRecord.

@stepan662
Copy link
Collaborator

The thing with auto loading is that it's not ready right away, so you would still had to solve the loading state in your app. Otherwise for a brief moment tolgee would display invalid translations anyway (english in case of fallback or just key names). So this is kinda design decision to force you to solve this yourself.

I was considering an edge case, because usually you have whole your app in one language and only rarely you need simultaneously display multiple languages, so it was considered ok, to keep it a bit more "manual".

I'll try to improve the docs about this.

@tpoisseau
Copy link
Contributor Author

Thank you

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

No branches or pull requests

2 participants