1

我正在使用next-i18next我的翻译。目录里面<rootDir>/public是我的翻译 *.json 文件。

"next": "^10.0.2",
"next-i18next": "^8.1.1",

运行我的生产包时,我的赛普拉斯测试失败,因为找不到完整的文本字符串。它只显示翻译键的值:t('someKey')

next-i18next.config.js 文件:

module.exports = {
  i18n: {
    defaultLocale: 'en',
    locales: ['en'],
  },
  localePath: path.resolve('./public/static/locales'),
};

next.config.js 文件:

const defaultConfig = {
  i18n,
  redirects: require('./next-redirects'),
}

_app.js:

function MyApp({ Component, pageProps }) {
   /// snipped
}

export default appWithTranslation(MyApp);

有错误的页面(不仅限于这一页)

export const SearchPage = () => {
  const router = useRouter();
  const { t } = useTranslation('search'); 

  // snipped //

  return (
    <>
      <Head>
        <title>{`${t('searchTitle')}`}</title>
        <meta
          property="og:title"
          content={`${t('searchTitle')}`}
          key="title"
        />
        <meta
          name="description"
          content={t('metaDescription')}
        />
      </Head>
      <ItemsContext.Provider
        value={{snipped}}>
         <Search />
      </ItemsContext.Provider>
    </>
  );
};

SearchPage.namespace = 'SearchPage';

export const getServerSideProps = async ({ locale }) => ({
  // exposes `_nextI18Next` as props which includes our namespaced files
  props: {
    ...await serverSideTranslations(locale, ['common', 'search']),
  }
});

export default SearchPage;

使用的命令:

"dev": "next dev",
"build": "next build",
"start": "next start",
"cypress.test": "npx cypress run",

运行 yarn dev 工作正常,如图所示: 在此处输入图像描述

运行yarn build && yarn start && yarn cypress.test将输出: 在此处输入图像描述

如果我尝试在我的本地机器上复制失败,yarn build翻译就可以了。

从关于静态文件服务的nextJS 文档:

注意:只有在构建时位于公共目录中的资产才会由 Next.js 提供服务。在运行时添加的文件将不可用。我们建议使用 AWS S3 等第三方服务来进行持久文件存储。

似乎 serverSideTranslations 函数可能被视为运行时 - 因为我的页面在与动态路径 URL 相关时不是预先构建的。

请在 serverSideTranslations 考虑运行时与构建时提供建议?

4

0 回答 0