我使用
t()
功能来翻译文本。
该函数的作用就像 astros /public
文件夹中没有语言环境一样。
我的
translation.json
文件为 en:
{
"index": {
"testHeader": "Test Header"
}
}
这是我的索引页代码:
---
import Layout from "../layouts/Layout.astro";
import { t, changeLanguage } from "i18next";
changeLanguage("en");
---
<Layout>
<h1>{t("index.testHeader")}</h1>
</Layout>
我的
astro-i18next.config.mts
:
/** @type {import('astro-i18next').AstroI18nextConfig} */
export default {
defaultLocale: "en",
locales: ["en", "cs"],
};
我的
astro.config.mjs
:
import { defineConfig } from 'astro/config';
import astroI18next from "astro-i18next";
import tailwind from '@astrojs/tailwind';
// https://astro.build/config
import react from "@astrojs/react";
// https://astro.build/config
export default defineConfig({
integrations: [astroI18next(), react(), tailwind({
config: './tailwind.config.cjs',
})]
});
t()
功能代替翻译。
我运行了npx astro-i18next generate
,但什么也没做
我也有类似的问题;我通过配置更改和降级修复了它。 (由于它仍处于测试阶段,请密切关注)
注意:“astro-i18next”的当前版本是“1.0.0-beta.17”。
astro-i18next.config.*
:baseLanguage: "en"
1.0.0-beta.13
,在版本10到17之间,只有这个对我有用。package.json
脚本中:"i18n": "npx astro-i18next generate"
pnpm i && pnpm run i18n && pnpm run build
考虑到此修复,我期待类似的问题在稳定版本中得到解决;不过,目前这应该能让你继续前进。
我用
npm update
修复了它。
由于某种原因,我的应用程序的依赖项没有更新。
正如鼓手在本期中评论的那样:
将以下配置添加到astro-i18next.config:
i18nextServer: {
backend: {
loadPath: './public/locales/{{lng}}/{{ns}}.json',
},
},
这对我有用。