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

Translate in .ts file -> Cannot perform operation, astro-i18n is not initialized. #68

Open
Nkay opened this issue Nov 2, 2023 · 7 comments
Assignees

Comments

@Nkay
Copy link

Nkay commented Nov 2, 2023

Describe the bug
I cannot load a translation from a .ts file - something the previous i18n implementation allowed.
Do I have to change this behaviour? Suggestions on how to do it?

I tried to find the minimum attack surface for my project - it is attached.

To Reproduce
Steps to reproduce the behavior:

  1. Clone Repo
  2. Start Astro
  3. Visit Page

Expected behavior
It should work ;)

Mandatory reproduction repository
https://github.com/Nkay/i18n-bug-reproduce-2

@Alexandre-Fernandez
Copy link
Owner

your repo is private

@Nkay
Copy link
Author

Nkay commented Nov 3, 2023

oh yes, sorry - corrected.

@Alexandre-Fernandez
Copy link
Owner

This is happening because the typescript files runs before the middleware.
But even if the translations were loaded you would still have a problem because the current route is set in the middleware.
Without the information on the current route you could not access page translations.
When using astroI18n outside a template wrap it in a getter, you can retrieve the value once it's properly intialized :

// old
t("my_key")
// new
() => ("my_key")

I will try to document this and add methods to be able to initialize astroI18n outside the middleware.
I'll leave this open until then.

@Alexandre-Fernandez
Copy link
Owner

Alexandre-Fernandez commented Nov 3, 2023

I investigated a bit, the TS modules run before everything because of vite which apparently initializes every module before the file that imports it runs.
This is really annoying, I also want astro-i18n to be used seamlessly in TS modules but this will need some rework to the core functionality.
Thanks for pointing out this issue, I'm thinking about a good way of solving it. For the moment you can wrap the calls inside getters (delaying the actual call until the middleware has run).

@Nkay
Copy link
Author

Nkay commented Nov 4, 2023

Hi Alexandre,

I changed the occurences (they were mostly in a link element, in the nav and the footer) and so it was only about an hour of work to change it from link.asd -> t(link.asd), and now it fully works again.
Thanks for taking the time and having a look into it!

@perinazzoo
Copy link

I have a related problem here, the only difference is that we are using server side render as default output, any updates here @Alexandre-Fernandez ? Can you provide a workaround until you give us a permanent solution?

@Alexandre-Fernandez
Copy link
Owner

Alexandre-Fernandez commented Nov 14, 2023

@perinazzoo If you are using the translations in the template the workaround is using getters instead. Otherwise as of now there is no workaround, as explained before, even if the translations were loaded you wouldn't be able to access the current route's translations because astro-i18n cannot detect it since Astro loads all modules before the request comes in.

This is not a simple fix, first I have to make sure that the library is initialized before the module runs, then I need to add a way to fetch a translation from any group/page (since the current route will be unknown in the module) and I will probably also need to make every call to the t function return a getter instead.
I didn't have time to think about the exact implementation but it would probably be something along those lines.

This issue is on my priority list for sure and I will fix it as soon as I can, however I'm very busy at the moment and I can't afford to work on this, if this is a deal-breaker I'm sorry but I can't help you at the moment.

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

3 participants