首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >next-i18next未能从公用文件夹加载区域设置

next-i18next未能从公用文件夹加载区域设置
EN

Stack Overflow用户
提问于 2022-10-05 12:42:08
回答 1查看 69关注 0票数 0

我迁移到next.js,并开始使用next-i18next。我想知道,如何从公用文件夹中生成next-i18next加载区域设置?只有当我将locales文件夹放置在根目录而不是公共目录时,它才能工作。

i18n.json;

代码语言:javascript
复制
{
    "locales": ["en", "fr", "ar"],
    "defaultLocale": "en",
    "pages": {
      "*": ["common"]
    }
}

next.config.js;

代码语言:javascript
复制
// next.config.js
const nextTranslate = require("next-translate");

module.exports = {
  ...nextTranslate(),
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-05 12:50:43

创建i18n.js文件根,在内部添加以下代码: const = require(' path ');

代码语言:javascript
复制
module.exports = {
  i18n: {
    locales: ['en', 'ru', 'tm'],
    defaultLocale: 'en',
    localeDetection: false,
    localePath: path.resolve('./public/locales'),
  },
};

并添加配置next.config.js文件如下:

代码语言:javascript
复制
const { i18n } = require('./i18n');

const nextConfig = {
  reactStrictMode: true,
  i18n,
};

module.exports = nextConfig;

在每一页之后添加转换代码。示例: index.file

代码语言:javascript
复制
import React, { useContext, useEffect } from 'react';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { useTranslation } from 'next-i18next';
import Router from 'next/router';
import { Title } from '../components';
import i18n from '../i18n';

const Home = () => {
  const { t } = useTranslation('');

  useEffect(() => {
    if (!user) {
      Router.push('/login');
    }
  });
  return (
    <>
      <Title title={`${t('page.home')} | Trillo`} />
      <div className="min-h-screen">Home</div>
    </>
  );
};

export const getServerSideProps = async ({ locale }) => (
  { props: {
    ...(await serverSideTranslations(
      locale,
      ['common'],
      i18n,
    )),
  } }
);

export default Home;

在这样的公用文件夹中创建这些文件之后。看看这个见此2

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73960523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档