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

The plugin thinks the locale is set to undefined for localizePath() #131

Closed
itsmatteomanf opened this issue Mar 1, 2023 · 11 comments
Closed
Labels
bug Something isn't working released on @beta

Comments

@itsmatteomanf
Copy link

itsmatteomanf commented Mar 1, 2023

Describe the bug

Starting from the latest, v1.0.0-beta.18 release, all instances of localizePath() (at least, do not use other variations, but most depend on this) assume that the current locale is undefined, unless I manually add it in the function call, after the URL.

localizePath("/search") fails, localizePath("/search", "es") works.

To Reproduce

  1. check any repo that uses localizePath() with a locale in the locales key in the plugin's config file (in my case this is the default locale, too), as it should be configured per the instructions (including the i18next.changeLanguage() at the top of the Astro file).
  2. verify in the console the error, "WARNING(astro-i18next): "undefined" locale is not supported, add it to the locales in your astro config.".
  3. all instances of localizePath() will return wrongly formatted URLs, missing the language part completely).

Expected behavior

It should get the current locale, which is correctly set (I checked, i18next.language returns the correct one) and use it if not set manually as it did previously.

Context (please complete the following information):

Possible fixes

The issue seems to be related to the check here, which fails, haven't yet figured out where it went wrong, but it's definitely in the commits between v1.0.0-beta.17 and v1.0.0-beta.18.

`WARNING(astro-i18next): "${locale}" locale is not supported, add it to the locales in your astro config.`

@loadbrain
Copy link

Same behaviour on my project, same context as mentioned above.

@Akasiek
Copy link

Akasiek commented Mar 2, 2023

Same in my project. I fixed it by using i18next.langauge property in the second argument of localizePath() function, like so:

import i18next from "i18next";
import { localizePath } from "astro-i18next";

// ...

localizePath(link, i18next.language)

@ferrarafer
Copy link

Same behavior on my project, same context as mentioned above.

@itsmatteomanf
Copy link
Author

itsmatteomanf commented Mar 3, 2023

Same in my project. I fixed it by using i18next.langauge property in the second argument of localizePath() function, like so:

import i18next from "i18next";
import { localizePath } from "astro-i18next";

// ...

localizePath(link, i18next.language)

I knew it was possible to fix this @Akasiek, but I have tens and tens of instances scattered across tens of files.
Can I do it? Sure.
Do I want to manually inject all of this? Nope, but I soon will have to if we don't get a reply/fix.

@yassinedoghri yassinedoghri added the bug Something isn't working label Mar 4, 2023
@yassinedoghri
Copy link
Owner

I'm guessing it's because I've set i18next as a peer dependency in beta.18. I'll look into it.

@itsmatteomanf
Copy link
Author

@yassinedoghri I had to manually install it, after the update. It is installed, though.

yassinedoghri added a commit that referenced this issue Mar 5, 2023
Having i18next set as peer dependency prevents astro-i18next from accessing the same i18next
context. Keeping it as a direct dependency for now.

fixes #131
yassinedoghri added a commit that referenced this issue Mar 5, 2023
Having i18next set as peer dependency prevents astro-i18next from accessing the same i18next
context. Keeping it as a direct dependency for now.

fixes #131
yassinedoghri added a commit that referenced this issue Mar 5, 2023
Having i18next set as peer dependency prevents astro-i18next from accessing the same i18next
context. Keeping it as a direct dependency for now.

fixes #131
yassinedoghri pushed a commit that referenced this issue Mar 5, 2023
# [1.0.0-beta.19](v1.0.0-beta.18...v1.0.0-beta.19) (2023-03-05)

### Bug Fixes

* add levels to relative path in script tag ([1203d42](1203d42)), closes [#129](#129)
* use unjs/pathe to resolve generated localized files paths across OS ([da80a8d](da80a8d)), closes [#135](#135)

### Reverts

* **i18next-peer:** reset i18next as package dependency ([7906e19](7906e19)), closes [#131](#131)
@github-actions
Copy link

github-actions bot commented Mar 5, 2023

🎉 This issue has been resolved in version 1.0.0-beta.19 🎉

The release is available on:

Your semantic-release bot 📦🚀

@yassinedoghri
Copy link
Owner

Reverted to having i18next packaged with astro-i18next to have this fixed. Apparently the i18next instance is not shared with astro-i18next otherwise, thus the undefined.

@theprobugmaker
Copy link

theprobugmaker commented Sep 2, 2023

@yassinedoghri For some reason I'm still getting this problem:

[...page].astro:

---
import i18next, { changeLanguage } from "i18next";
import { localizePath } from "astro-i18next";
import { getCollection } from "astro:content";
import Layout from "../../layouts/Layout.astro";

export async function getStaticPaths({ paginate }) {
  changeLanguage("en");

  const posts = await getCollection("blog", ({ id }) =>
    id.startsWith(i18next.language)
  );

  return paginate(posts, {
    pageSize: 1,
  });
}

const { page } = Astro.props;

const example = localizePath(`blog/${page.data[0].slug.split("/").slice(1).join("/")}`)

console.log(example);
---

WARNING(astro-i18next): "undefined" locale is not supported, add it to the locales in your astro config.

  "dependencies": {
    "@astrojs/mdx": "^1.0.0",
    "astro": "^3.0.7",
    "astro-i18next": "1.0.0-beta.21",
    "i18next": "^23.4.6",
    "i18next-fs-backend": "^2.1.5"
  }

@theprobugmaker
Copy link

I just noticed that it happens because of the i18next version 23+

AliLee0923 pushed a commit to AliLee0923/astro-i18N that referenced this issue Dec 2, 2023
# [1.0.0-beta.19](yassinedoghri/astro-i18next@v1.0.0-beta.18...v1.0.0-beta.19) (2023-03-05)

### Bug Fixes

* add levels to relative path in script tag ([1203d42](yassinedoghri/astro-i18next@1203d42)), closes [#129](yassinedoghri/astro-i18next#129)
* use unjs/pathe to resolve generated localized files paths across OS ([da80a8d](yassinedoghri/astro-i18next@da80a8d)), closes [#135](yassinedoghri/astro-i18next#135)

### Reverts

* **i18next-peer:** reset i18next as package dependency ([7906e19](yassinedoghri/astro-i18next@7906e19)), closes [#131](yassinedoghri/astro-i18next#131)
@amosbastian
Copy link

I am getting this issue too

"astro-i18next": "1.0.0-beta.21",
"i18next": "^23.7.11",
"i18next-fs-backend": "^2.3.1",
"i18next-http-backend": "^2.4.2",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released on @beta
Projects
None yet
Development

No branches or pull requests

7 participants