tolgee / tolgee-js

Tolgee JavaScript libraries monorepo
https://tolgee.io
MIT License
231 stars 28 forks source link

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 closed 1 year ago

tpoisseau commented 1 year ago

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,

stepan662 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Thank you