transloco icon indicating copy to clipboard operation
transloco copied to clipboard

Reload translation dictionaries in runtime

Open Dedrus opened this issue 3 years ago • 17 comments

Hello! We have idea in our electron application. We implemented localization with online dictionaries for that. We want to implement periodical polling and reload translation dictionaries silently during runtime, so if we release some localization fixes user app will silently reload translation and user will not be required to reload/refresh electron application.

It would be great to have some method to force reload of translation dictionaries. Right now they are cached and looks like there are no ways to reload them in runtime.

Or there is already some way to reload translation dictionaries?

Dedrus avatar Jul 05 '21 10:07 Dedrus

I have the same problem, didn't find a way to reload translation already loaded

Am I missing something ?

simon-knu avatar Nov 10 '21 22:11 simon-knu

@SimonCorellia I have not solved this problem - you can refresh page if you want to load new dictionaries.

Dedrus avatar Nov 11 '21 13:11 Dedrus

Yes, I did that too but it's not the best solution IMO

simon-knu avatar Nov 11 '21 13:11 simon-knu

@Dedrus @SimonCorellia as a workaround you can load the translation and use the setTranslation.

Maybe we can expose a function that clears the cache, and after calling it you can load whatever you need to reload.

shaharkazaz avatar Nov 14 '21 09:11 shaharkazaz

@shaharkazaz Hi

As a workaround, I use the this.translocoService.ngOnDestroy() to clear the cache

It's not the best solution because it destroys a subscription but it's the only way I found to clear it and to cover all my cases

Could you provide a function to clear the cache ? Or I could provide a pull request for that...

Let me know

simon-knu avatar Nov 22 '21 10:11 simon-knu

@SimonCorellia Why isn't setTransaltion working for you? what's the flow?

shaharkazaz avatar Nov 22 '21 10:11 shaharkazaz

Client requirement....., users can't access the app until there is translations loaded, and when we change the language, we need to reset the app, reload translations from the backend and then, access the app

simon-knu avatar Nov 22 '21 11:11 simon-knu

@shaharkazaz @SimonCorellia looks like setTranslation works like charm! Thanks for advice, we will use it.

Dedrus avatar Dec 24 '21 09:12 Dedrus

@shaharkazaz I think a clear cache function in Transloco service would be great. Is it hard to design/implements? UseCase: when user switch language manually, it would be nice if we could reload the translations from backend in case an admin changed something in between

f-aubert avatar Apr 15 '23 10:04 f-aubert

@f-aubert You are welcome to open a PR for it 🙂 should be pretty easy to implement.

shaharkazaz avatar Apr 22 '23 18:04 shaharkazaz

If I expose the following deleteCache:

  deleteCache(...paths: string[]) {
    if (!paths.length) {
      this.cache.clear();
    } else {
      for (const path of paths) {
        this.cache.delete(path);
      }
    }
  }

How would you use it? What will trigger the reload? @simon-knu How are you using the ngOnDestroy? skipping the cache altogether as suggested might cause multiple loads.

shaharkazaz avatar Apr 19 '24 15:04 shaharkazaz

@shaharkazaz Yes, I would use it like that

The reload would be triggered by an user action

I'm using the ngOnDestroy before calling the "translocoService.load" method

I have to do that because I need to have the latest translations. I can't trust the cached value, the translations can be updated at runtime (on the server) and it's critical for the app to have the correct translations

simon-knu avatar Apr 22 '24 08:04 simon-knu

@simon-knu let me clarify my question, all the directives need to get the updated values, what causes the stream to push the new value?

They are registering onInit to the previous cached load stream. That's why I'm surprised that just calling the onDestroy before the load works.

Can you share a reproduction of what you are doing?

Another option I thought of is adding an override option to the load method.

shaharkazaz avatar Apr 22 '24 08:04 shaharkazaz