astro-i18next Tfunction 显示按键而不是翻译

问题描述 投票:0回答:3

我使用

t()
功能来翻译文本。 该函数的作用就像 astros
/public
文件夹中没有语言环境一样。

My file structure

我的

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()
功能shows the passed key代替翻译。 我运行了
npx astro-i18next generate
,但什么也没做

i18next astrojs
3个回答
0
投票

我也有类似的问题;我通过配置更改和降级修复了它。 (由于它仍处于测试阶段,请密切关注)

注意:“astro-i18next”的当前版本是“1.0.0-beta.17”。

  1. 将以下内容添加到您的
    astro-i18next.config.*
    baseLanguage: "en"
  2. 将您的版本降级到
    1.0.0-beta.13
    ,在版本10到17之间,只有这个对我有用。
  3. 好但不是必需的:将其添加到您的
    package.json
    脚本中:
    "i18n": "npx astro-i18next generate"
  4. 运行此命令应该会成功:
    pnpm i && pnpm run i18n && pnpm run build

考虑到此修复,我期待类似的问题在稳定版本中得到解决;不过,目前这应该能让你继续前进。


0
投票

我用

npm update
修复了它。

由于某种原因,我的应用程序的依赖项没有更新。


0
投票

正如鼓手在本期中评论的那样:

将以下配置添加到astro-i18next.config

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

这对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.