NipGeihou's blog NipGeihou's blog
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档

NipGeihou

我见青山多妩媚,料青山见我应如是
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档
  • nodejs

  • css

  • 最佳实践

    • Next.js整合i18n(next-i18next)
      • 背景
      • 开始
        • 安装
        • 翻译内容
        • 项目设置
        • 翻译函数
        • appWithTranslation
        • serverSideTranslations
        • useTranslation
      • 参考
  • TypeScript

  • ajax

  • JavaScript

  • 前端工程化

  • React

  • nextjs

  • Flutter

  • 笔记

  • 前端
  • 最佳实践
NipGeihou
2024-08-10
目录

Next.js整合i18n(next-i18next)

# 背景

  • 需要实现一个不同地区用户访问时返回的页面为当地语言的,而且需要是在服务端渲染的,以达到更好的 SEO
  • 同时由于路由已使用地区代码来区分不同的地区

# 开始

# 安装

yarn add next-i18next react-i18next i18next

# 翻译内容

.
└── public
    └── locales
        ├── en
        |   └── common.json
        └── de
            └── common.json

内容参考:next-i18next/examples/simple at master · i18next/next-i18next (opens new window)

只要按照以上目录结构存放翻译配置,能自动读取

# 项目设置

next-i18next.config.js

/** @type {import('next-i18next').UserConfig} */
module.exports = {
  i18n: {
    defaultLocale: 'en',
    locales: ['en', 'de'],
  },
}

next.config.js

const { i18n } = require('./next-i18next.config')

module.exports = {
  i18n,
}

# 翻译函数

# appWithTranslation

在项目根目录创建 pages/_app.tsx 文件

import { appWithTranslation } from 'next-i18next'

const MyApp = ({ Component, pageProps }) => (
  <Component {...pageProps} />
)

export default appWithTranslation(MyApp)

# serverSideTranslations

# useTranslation

# 参考

  • i18next/next-i18next: The easiest way to translate your NextJs apps. (opens new window)
上次更新: 2024/08/28, 15:20:29
SASS
TypeScript

← SASS TypeScript→

最近更新
01
Docker Swarm
04-18
02
安全隧道 - gost
04-17
03
Solana最佳实践
04-16
更多文章>
Theme by Vdoing | Copyright © 2018-2025 NipGeihou | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式