transloco
transloco copied to clipboard
Reload translation dictionaries in runtime
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?
I have the same problem, didn't find a way to reload translation already loaded
Am I missing something ?
@SimonCorellia I have not solved this problem - you can refresh page if you want to load new dictionaries.
Yes, I did that too but it's not the best solution IMO
@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 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
@SimonCorellia Why isn't setTransaltion working for you? what's the flow?
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
@shaharkazaz @SimonCorellia looks like setTranslation works like charm! Thanks for advice, we will use it.
@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 You are welcome to open a PR for it 🙂 should be pretty easy to implement.
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 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 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.