yassinedoghri / astro-i18next

An astro integration of i18next + some utility components to help you translate your astro websites!
https://astro-i18next.yassinedoghri.com
MIT License
514 stars 36 forks source link

Localized pages generation and static paths #93

Open florian-lefebvre opened 1 year ago

florian-lefebvre commented 1 year ago

Describe the bug

Running astro-i18next generate adds the changeLanguage('locale') call to pages, but not to getStaticPaths

Context (please complete the following information):

Workaround

I created a script:

import fsp from 'fs/promises'
import path from 'path'

async function main() {
    const __dirname = path.resolve()
    async function fix(filePath) {
        let data = await fsp.readFile(filePath, { encoding: 'utf-8' })
        data = data.replaceAll(
            'getStaticPaths() {',
            "getStaticPaths() {changeLanguage('fr')"
        )
        await fsp.writeFile(filePath, data, { encoding: 'utf-8' })
    }
    for (const filePath of ['projets/[slug]', 'blog/[slug]']) {
        await fix(path.join(__dirname, `./src/pages/fr/${filePath}.astro`))
    }
}

main()
zogar1993 commented 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

zanhk commented 1 year ago

Thanks @florian-lefebvre for your workaround

zanhk commented 1 year ago

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();