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

Can't get i18next-browser-languageDetector plugin to work #37

Closed
dallyh opened this issue Sep 8, 2022 · 10 comments
Closed

Can't get i18next-browser-languageDetector plugin to work #37

dallyh opened this issue Sep 8, 2022 · 10 comments
Labels
bug Something isn't working

Comments

@dallyh
Copy link
Contributor

dallyh commented Sep 8, 2022

For the past few days I've been trying to get this plugin to work: https://github.com/i18next/i18next-browser-languageDetector.

Everything I do fails, and the plugin doesn't actually do anything. I dug through it's source code and it uses simple methods to detect the language in the browser, for example by path (http://site.com/LANGUAGE): https://github.com/i18next/i18next-browser-languageDetector/blob/master/src/browserLookups/path.js, by navigator.language: https://github.com/i18next/i18next-browser-languageDetector/blob/master/src/browserLookups/navigator.js and such.

I have confirmed that the plugin actually loads, but simply doesn't do anything and I got stuck.

To my understanding i18next in astro-i18next gets loaded with page-ssr which from the Astro API page documentation says:

Imported as a separate module in the frontmatter of every Astro page component. Because this stage imports your script, the Astro global is not available and your script will only be run once when the import is first evaluated.

Does this actually mean, that all of the window objects are unavailable for the plugin , and that is why it doesn't work, or am I missing something?

Configured repo with this plugin is here.
From the i18next documentation, changeLanguage gets called on Layout.astro without lng:

Calling changeLanguage without lng uses the language detector to choose the language to set.

This may be not an issue with this plugin, but rather my misunderstading of it or of Astro.

Thanks!

@dallyh dallyh changed the title Can't get / i18next-browser-languageDetector to work Can't get i18next-browser-languageDetector to work Sep 8, 2022
@dallyh dallyh changed the title Can't get i18next-browser-languageDetector to work Can't get i18next-browser-languageDetector plugin to work Sep 8, 2022
@yassinedoghri yassinedoghri added the bug Something isn't working label Sep 10, 2022
@yassinedoghri
Copy link
Owner

Thanks for the issue!

I think one would expect i18next plugins to just work when using astro-i18next, so I've labeled this as a bug.

Though, I think that the best solution is to change the behavior of page-ssr directly on Astro as astro-i18next relies heavily on that to work properly.

That being said, I've written an rfc for this: withastro/roadmap#293

@dallyh
Copy link
Contributor Author

dallyh commented Sep 10, 2022

That's great! RFC looks good to me. Anyways is there maybe a workaround over this, besides implementing the detection and or the whole i18next myself? I currently don't have any ideas.

@yassinedoghri
Copy link
Owner

That's great! RFC looks good to me. Anyways is there maybe a workaround over this, besides implementing the detection and or the whole i18next myself? I currently don't have any ideas.

That's a good question! Haven't had time to think about it in details. So, thinking out loud...

  1. Language Detection:
  • SSG: must be done via a client side script
  • SSR: can be done via a client side or (preferably?) a server side script
  1. After detection:

For starters, astro-i18next loads i18next on page-ssr (server side). And the browser language detector plugin you're including is meant to be run client side.

Maybe i18next needs to be loaded both in server side with page-ssr and client side during page stage.

There are other issues beyond this, the t function is resolved during build time for SSG but it sometimes needs to be resolved on client side to format date times for example.

As for astro-i18next utilities that can be added: a function that detects language (to be used client side) and/or a component that detects + shows a banner to switch offers to switch?

For reference:

@L1lith
Copy link

L1lith commented Sep 11, 2022

It would be super cool if you could have some kind of setup where there's a compute instance that gets spun up for every region of the world automatically that would be translate the site to the most popular language in that region automatically, then that response could be cached on a CDN for performance/cost reasons.
Related: Caching Lambda responses with Cloudflare

yassinedoghri added a commit that referenced this issue Oct 30, 2022
…side

- update config naming
- abstract i18next config + make it overridable for server and client
  configs
- fix route translations to discard page extensions
- update README to account for new route features and easier setup
- add i18next-fs-backend, i18next-http-backend and
  i18next-browser-languagedetector packages to abstract locales
  detection and loading
- automatically require react-i18next when @astrojs/react integration
  is installed

BREAKING CHANGE:
- defaultLanguage is now defaultLocale
- supportedLanguages is now locales
- i18next config is now split into two configs: `i18nextServer`
and `i18nextClient`

fixes #57, closes #46, #37
yassinedoghri added a commit that referenced this issue Nov 6, 2022
…side

- update config naming
- abstract i18next config + make it overridable for server and client
  configs
- fix route translations to discard page extensions
- update README to account for new route features and easier setup
- add i18next-fs-backend, i18next-http-backend and
  i18next-browser-languagedetector packages to abstract locales
  detection and loading
- automatically require react-i18next when @astrojs/react integration
  is installed

BREAKING CHANGE:
- defaultLanguage is now defaultLocale
- supportedLanguages is now locales
- i18next config is now split into two configs: `i18nextServer`
and `i18nextClient`

fixes #57, closes #46, #37
yassinedoghri added a commit that referenced this issue Nov 6, 2022
…side

- update config naming
- abstract i18next config + make it overridable for server and client
  configs
- fix route translations to discard page extensions
- update README to account for new route features and easier setup
- add i18next-fs-backend, i18next-http-backend and
  i18next-browser-languagedetector packages to abstract locales
  detection and loading
- automatically require react-i18next when @astrojs/react integration
  is installed

BREAKING CHANGE:
- defaultLanguage is now defaultLocale
- supportedLanguages is now locales
- i18next config is now split into two configs: `i18nextServer`
and `i18nextClient`

fixes #57, closes #46, #37
yassinedoghri pushed a commit that referenced this issue Nov 6, 2022
# [1.0.0-beta.13](v1.0.0-beta.12...v1.0.0-beta.13) (2022-11-06)

### Bug Fixes

* add isFileHidden function + tests to prevent missing hidden files ([7dcd0aa](7dcd0aa))
* **generate:** replace isLocale check with user defined locales to prevent nested folders generation ([a598e2e](a598e2e)), closes [#56](#56)
* **i18next-server:** load locale files synchronously ([e7892e2](e7892e2))
* update types import to relative ([#58](#58)) ([44a5422](44a5422))

### Features

* add option to show the default locale in the url ([#51](#51)) ([ea939db](ea939db)), closes [#54](#54)
* add support for route translations ([db5200b](db5200b)), closes [#50](#50) [#29](#29)
* allow implicit key for <Trans> when omitting i18nKey prop ([ff14354](ff14354)), closes [#53](#53)
* simplified API + instanciate i18next both in server and client side ([ed44510](ed44510)), closes [#57](#57) [#46](#46) [#37](#37)

### BREAKING CHANGES

* - defaultLanguage is now defaultLocale
- supportedLanguages is now locales
- i18next config is now split into two configs: `i18nextServer`
and `i18nextClient`
@yassinedoghri
Copy link
Owner

yassinedoghri commented Nov 6, 2022

I think that after beta.13 release, it's safe to say that this can be closed! 🎉

astro-i18next now loads 2 separate i18next configs:

@L1lith
Copy link

L1lith commented Nov 6, 2022

I think that after beta.13 release, it's safe to say that this can be closed! 🎉

astro-i18next now loads 2 separate i18next configs:

* **server-side** with [i18next-fs-backend](https://github.com/i18next/i18next-fs-backend) by default

* **client-side** with [i18next-http-backend](https://github.com/i18next/i18next-http-backend) and [i18next-browser-languagedetector](https://github.com/i18next/i18next-browser-languageDetector) by default

🎉 Seriously impressive work

@dallyh
Copy link
Contributor Author

dallyh commented Nov 7, 2022

I think that after beta.13 release, it's safe to say that this can be closed! 🎉

astro-i18next now loads 2 separate i18next configs:

Truly amazing work! But I have to admit I got a little bit lost here. Now astro-i18next should be loaded server side and also client side by default. That basically means that when I'm running the dev server, i should see the debug logs both in browser console and also in the terminal from Vite server (of course having both debug options enabled).

I quickly tried adding this to my config file, but the detection does not seem to work client-side (for example navigating with query string ?lng=cs), and also there are no client side browser console logs.
i18nextServer: { debug: true }, i18nextClient: { debug: true, detection: { order: ['querystring', 'navigator', 'path', 'subdomain'], } }

Am I doing something wrong, or I just did not get the concept of this?

Thanks a lot!

EDIT: Allright figured out the first part. When building for SSG then i18next is run and all the strings are translated on runtime. However I would please still like to know, why there's no debug client side.

@yassinedoghri
Copy link
Owner

However I would please still like to know, why there's no debug client side.

Hey @dallyh, have you managed to see the console logs?

I could see them when trying out React. Maybe there needs to be a client-side component for logs to show up?
If you still get the problem, could you please create a new issue? I'll try to tackle it whenever I get the chance.

@GoldenSoju
Copy link

I am still a bit lost here, as I am working on an SSG project but I cannot get the language detection to work.
(No other framework, just Astro)
I use version v1.0.0-beta.17, and I understood that the language detection should work out of the box.
Or do I have to add something to the configuration file?

At the moment the locale always changes to the set defaultLocale.

Any help is much appreciated.

@danieldaeschle
Copy link

I have the same problem! Not working on client side!

AliLee0923 pushed a commit to AliLee0923/astro-i18N that referenced this issue Dec 2, 2023
# [1.0.0-beta.13](yassinedoghri/astro-i18next@v1.0.0-beta.12...v1.0.0-beta.13) (2022-11-06)

### Bug Fixes

* add isFileHidden function + tests to prevent missing hidden files ([7dcd0aa](yassinedoghri/astro-i18next@7dcd0aa))
* **generate:** replace isLocale check with user defined locales to prevent nested folders generation ([a598e2e](yassinedoghri/astro-i18next@a598e2e)), closes [#56](yassinedoghri/astro-i18next#56)
* **i18next-server:** load locale files synchronously ([e7892e2](yassinedoghri/astro-i18next@e7892e2))
* update types import to relative ([#58](yassinedoghri/astro-i18next#58)) ([44a5422](yassinedoghri/astro-i18next@44a5422))

### Features

* add option to show the default locale in the url ([#51](yassinedoghri/astro-i18next#51)) ([ea939db](yassinedoghri/astro-i18next@ea939db)), closes [#54](yassinedoghri/astro-i18next#54)
* add support for route translations ([db5200b](yassinedoghri/astro-i18next@db5200b)), closes [#50](yassinedoghri/astro-i18next#50) [#29](yassinedoghri/astro-i18next#29)
* allow implicit key for <Trans> when omitting i18nKey prop ([ff14354](yassinedoghri/astro-i18next@ff14354)), closes [#53](yassinedoghri/astro-i18next#53)
* simplified API + instanciate i18next both in server and client side ([ed44510](yassinedoghri/astro-i18next@ed44510)), closes [#57](yassinedoghri/astro-i18next#57) [#46](yassinedoghri/astro-i18next#46) [#37](yassinedoghri/astro-i18next#37)

### BREAKING CHANGES

* - defaultLanguage is now defaultLocale
- supportedLanguages is now locales
- i18next config is now split into two configs: `i18nextServer`
and `i18nextClient`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants