This is a Next.js Template implementing Internationalization (i18n).
I wrote this because I found it hard to compose all the demos and docs together into what resembled a fully functioning content-centric website.
This template:
- relies on local MDX files. Put your long-form content in any locale. This might be a deal-breaker for you.
- doesn't choose one of the 5 i18n libraries Next shortlists. Instead, it creates its own translation API 😅. Replace it if you need something more robust.
- implements gentle TypeScript hinting to ensure consistent translation across locales. (see below)
- doesn't make any choices about your styling solution. Bikeshed away.
Current locales are defined within next.config.js
:
File: next.config.js
module.exports = {
i18n: {
locales: ["en", "es"],
defaultLocale: "en",
},
}
The content/
directory contains a TypeScript definition file ensuring you never miss mapping a new value.
File: content\translation.d.ts
export type Nav = {
home: string,
chapter1: string,
}
export type Translation = {
nav: Nav,
welcome: string,
}
- Add the locale to
next.config.js
- Add a new locale directory under
/content
- Add
translation.ts
under the new locale directory, implementing theTranslation
type. - Add any long-form MDX content you wish to author.
Node environment:
File: .nvmrc
v16.14.2
Install dependencies:
npm ci
Then, run the development server:
npm run dev
Option one: One-click deploy to Vercel
Option two: Manual clone
- Clone this repo:
git clone [email protected]:bmuenzenmeyer/next-i18n-starter.git
- Navigate to the directory and run
npm ci
- Run
npm run dev
I composed some resources to get this far. Thank you to:
- ❤️ Next Netlify Starter - from which these docs are derived
- 🌐 Next i18n-routing example - for the LocaleSwitcher
- 📦 Next MDX Remote via Albert Barsegyan's demo
- 🔺 Vercel for easy and free hosting supporting Server-Side Rendering
- nextjs-i18n-static-page-starter - Consider this if you want Tailwind and i18next baked into your experience.