This is a Next.js project bootstrapped with create-next-app.
Translations are placed in /public/locales/{lang}/{ns}.json
{ns}- namespace, allows you to split translation keys into multiple files{lang}- language
In this example there are two namespaces: common and home and 4 locales: en, es, fr_FR, pl.
.
├── en
│ ├── common.json
│ └── home.json
├── es
│ ├── common.json
│ └── home.json
├── pl
│ ├── common.json
│ └── home.json
└── fr_FR
├── common.json
└── home.jsonInstall i18next for NextJS
npm install --save next-i18nextCreate a configuration file in project root.
// 📦 file: ./next-i18next.config.js
module.exports = {
i18n: {
defaultLocale: 'en',
locales: ['en', 'es', 'pl', 'fr_FR'],
},
};Import i18next configuration file into next.config.js
// 📦 file: ./next.config.js
const {i18n} = require("./next-i18next.config");
const nextConfig = {
reactStrictMode: true,
i18n
}
module.exports = nextConfig💿 Install SimpleLocalize CLI
curl -s https://get.simplelocalize.io/2.7/install | bash🧷 Create configuration file
# 📦 file: ./simplelocalize.yml
uploadFormat: single-language-json
uploadLanguageKey: en
uploadPath: ./public/locales/en/{ns}.json
uploadOptions:
- REPLACE_TRANSLATION_IF_FOUND
downloadFormat: single-language-json
downloadLanguageKeys: ['pl', 'fr', 'es']
downloadPath: ./public/locales/{lang}/{ns}.json./public/locales directory
simplelocalize download./public/locales directory
simplelocalize uploadYou can automate process of adding translation keys from project to SimpleLocalize.
Example usage can be found in pages/index.tsx.
//translations from common.json
const {t} = useTranslation('common');
console.log(t('LEARN_MORE')) // output: Learn more
//translations from home.json
const {t: homeT} = useTranslation('home');
console.log(homeT('HELLO_WORLD')) // output: Hello worldFirst, run the development server:
npm run devOpen http://localhost:3000 with your browser to see the result.
You can start editing the page by modifying pages/index.tsx. The page auto-updates as you edit the file.
