js-lingui: Missing "beforeChange"-event
I just want to start to dynamically load my translations. I thought I could simply hook into the “change” event and import my message-catalog from there. I was missing that this event is propagated after the locale is set. Therefor the messages are missing while setting the new locale in i18n and my plan failed miserably… 😉
Describe proposed solution
I would love to have an event which fires on i18n.activate(locale), but before the locale is internally actually set. This would enable dynamic message-loading inside the i18n-context without the need for custom methods or similar.
PS: Thanks for the work on linguijs - love it! ❤️
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (14 by maintainers)
Hello 👋 so I read through the whole thing and here are my observations:
First, it’s tempting to write a quick feature request for some issue that a developer encounters. However, if the feature request is not carefully written, and offers enough details on what problem it’s trying to solve and how, and why something is a problem in the first place, then it’s hard to find a common ground for discussion.
Here’s why I don’t think the proposal is a good way forward:
which is a variation from https://github.com/lingui/js-lingui/issues/1200#issuecomment-1020003814 but at the same time, it avoids the import because the OP will import messages from different places and therefore with different import paths.
it can be used like this:
It’s true that still means that the
const {messages} = ...part needs to be duplicated across the monorepo packages which the OP wanted to avoid, but how many times does it need to be duplicated? In another words, how many packages are in the monorepo? Maybe 5 if not less? It’s better to have a tiny bit of duplication in user land rather than introduce a new api to a library.the problem with this is that the
I18nProviderwill re-render both with activate() and load() calls which take place with a delay in between them because of the async import. Rather than re-rendering twice (the first re-render is useless because no new messages are present), we need to change the order towhich will result in one re-render only. Exactly as said in https://github.com/lingui/js-lingui/issues/1200#issuecomment-1021086930
load()andactivate()sequence is pretty straightforward, imperative way to do a locale change and doing this via some event listener is an unnecessary indirection.Thanks 🙂
I’m no maintainer of linguijs so I was just trying to help you in case you were stuck, that’s all 😃 And because I was also like @semoal quite sure you do not need this event. (You can go with an event oriented solution, but I’m pretty sure you can do without and with better and more readable code)
Feel free to post a PR with your solution and your use case, I’m very curious about this solution. If I’m wrong I would be the 1st to support it and use it because I think I have a similar use case (That I solved by doing the less copy / paste I could do, code quoted just above, and it’s fine by me)