Open florian-lefebvre opened 1 year ago
This may be related to my issue, which only happens on local. It works fine on prod tho for some reason, so not sure. My default language file works fine on local:
changeLanguage("en");
console.log(i18next.language); //en
export async function getStaticPaths() {
console.log(i18next.language); //en
return await getAllPages({ language: i18next.language });
}
But when I translate to Spanish, language did not change inside getStaticPaths:
changeLanguage("en");
console.log(i18next.language); //es
export async function getStaticPaths() {
console.log(i18next.language); //en
return await getAllPages({ language: i18next.language });
}
If I attempt to add a changeLanguage inside of getStaticPaths, it gets removed in the default language file after running generate.
OS: Mac Ventura 13.2 astro-i18next version: 1.0.0-beta.17 astro version: 2.0.6
Thanks @florian-lefebvre for your workaround
Here's a more general script if anyone need
import fsp from "fs/promises";
import path from "path";
// Change this
const locales = ["it"];
const defaultLocale = "en";
const paths = ["[...blog]/[...page]"];
async function main() {
const __dirname = path.resolve();
async function fix(filePath, locale = defaultLocale) {
const regex = /getStaticPaths\([^)]*\)\s*{/;
let data = await fsp.readFile(filePath, { encoding: "utf-8" });
const match = data.match(regex);
const changeLanguageString = `changeLanguage('${locale}')`;
if (match) {
const index = match.index + match[0].length;
data = data.slice(0, index) + changeLanguageString + data.slice(index);
await fsp.writeFile(filePath, data, { encoding: "utf-8" });
}
}
for (const filePath of paths) {
for (const locale of locales) {
await fix(path.join(__dirname, `./src/pages/${locale}/${filePath}.astro`), locale);
}
await fix(path.join(__dirname, `./src/pages/${filePath}.astro`));
}
}
main();
Describe the bug
Running
astro-i18next generate
adds thechangeLanguage('locale')
call to pages, but not togetStaticPaths
Context (please complete the following information):
astro-i18next
version: 1.0.0-beta.14astro
version: ^1.6.13Workaround
I created a script: