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
524 stars 37 forks source link

Translation doesn't work, it shows a key instead of translated text. #103

Open drumsta opened 1 year ago

drumsta commented 1 year ago

Describe the bug

Translation doesn't work - e.g. in case of translation title={t("cards.documentation.title")} it shows cards.documentation.title on the web page. I guess the same issue is described in discusion #99, and it might be the case on Windows only.

To Reproduce

Steps to reproduce the behavior:

  1. Open the folder astro-i18next\examples\node in VS Code
  2. Run nmp install, and then npm run dev, point to http://localhost:3000/
  3. All translated messages are untranslated

Expected behavior

Translated messages should appear.

Screenshots

image

Context (please complete the following information):

By adding i18nextServer.debug: true the following is observabled in Terminal window, i.e. notice missing slashes in a path.

image

Possible fixes

drumsta commented 1 year ago

Adding the following configuration to the astro-i18next.config has helped:

  i18nextServer: {
    backend: {
      loadPath: './public/locales/{{lng}}/{{ns}}.json',
    },
  },

The code at line 78 works incorrectly at least on Windows:

fileURLToPath(config.publicDir) + "locales/{{lng}}/{{ns}}.json",

0x7357 commented 1 year ago

Same here.

Silbad commented 1 year ago

Hello, same with Win11

VicAv99 commented 1 year ago

Still seeing this issue when deploying to Vercel

mihanc commented 1 year ago

Same here

mihanc commented 1 year ago

Still seeing this issue when deploying to Vercel

Have you managed to solve this issue?

mihanc commented 1 year ago

Got rid of the fsBackend plugin and added the resources object, and now everything is working. But I hope it's a temporary solution and the issue will be fixed in the further releases... image

image

eidens-design commented 1 year ago

I am experiencing this issue with the version 1.0.0-beta.16, on MacOS.

josemarcilio commented 1 year ago

Got rid of the fsBackend plugin and added the resources object, and now everything is working. But I hope it's a temporary solution and the issue will be fixed in the further releases... image

image

Not worked for me, neither in local or vercel =/

josemarcilio commented 1 year ago

Still seeing this issue when deploying to Vercel

Fixed my deploy on Vercel by adding includeFiles in my adapter settings:

  adapter: vercel({
    includeFiles: ["./public/locales/en/translation.json", "./public/locales/pt/translation.json"],
  }),
sermagnus commented 1 year ago

I have the same problem with an SSR site: after several test, since I don't use the astro-i18next routing I was able to solve it using this configuration

import en from "./public/locales/en/translation.json" assert {type: "json"};
import it from "./public/locales/it/translation.json" assert {type: "json"};

export default {
    defaultLocale: "en",
    locales: ["en", "it"],
    fallbackLng : "en",
    i18nextServer: {
      debug: true,
      resources: {
        en: {
          translation: {
              ...en
          }
        },
        it: {
          translation: {
              ...it
          }
        }
      },
      initImmediate: false,
      preload: true,
      backend: null
    },
    i18nextServerPlugins: {
      fsBackend: null
    }
  };

In short, since I don't use routing I disabled i18nextServerPlugins and manually imported the translations

sermagnus commented 1 year ago

I confirm the bug reported by drumsta: in the build phase the backend.loadPath property of astro-i18next configuration, generate by resolve from pathe, use an absolute path for the local environment, which clearly doesn't work on the server once published.

https://github.com/yassinedoghri/astro-i18next/blob/beta/src/index.ts#L80

moritzlang commented 1 year ago

I have the same problem when deploying a SSR site to Vercel. Is there a better way to handle this than using the resources object?

enricogallesio commented 1 year ago

I had a similar issue here on Linux. After banging my head for a while I've noticed something suspect, and found out this issued was caused by the presence of a white space in the local path of the project... I guess that's my fault? ¯_(ツ)_/¯

I hope this can help

alanescarcha commented 7 months ago

[!IMPORTANT] To solve this simply add the following to the file astro-i18next.config.mjs:

import en from "./public/locales/en/translation.json" assert {type: "json"};
import es from "./public/locales/es/translation.json" assert {type: "json"}; // other example

export default {
    i18nextServer: {
        resources: {
            en: {
                translation: {
                    ...en
                }
            },
            es: { // other example
                translation: {
                    ...es
                }
            }
        },
    }
};

Adapt the code as you need it, don't forget variables like defaultLocale and locales

mimi566 commented 4 days ago

Still seeing this issue when deploying to Vercel

Well one of my friend just help me to fix this problem:

Note: You can try this AstroI18nextConfig when it's working on local dev (vercel dev) but not working after vercel deply:

import type { AstroI18nextConfig } from "astro-i18next"; import en from "./public/locales/en/translation.json" assert { type: "json" }; import es from "./public/locales/es/translation.json" assert { type: "json" }; import fr from "./public/locales/fr/translation.json" assert { type: "json" }; import id from "./public/locales/id/translation.json" assert { type: "json" }; import ru from "./public/locales/ru/translation.json" assert { type: "json" };

const config: AstroI18nextConfig = { defaultLocale: "en", locales: ["en", "fr", "es", "ru", "id"], i18nextServer: { resources: { en: { translation: { ...en, }, }, es: { translation: { ...es, }, }, ru: { translation: { ...ru, }, }, fr: { translation: { ...fr, }, }, id: { translation: { ...id, }, }, }, }, };

export default config;

thanks to my friend and deploy project is: scconverter