Guides
Internationalization (i18n) Guide
Learn how to use Lingui.js for internationalization in OnlyRules
Internationalization (i18n) Guide
This project uses Lingui.js for internationalization, supporting English (en), Simplified Chinese (zh-CN), and Traditional Chinese (zh-HK).
Quick Start
Using Translations in Components
- Client Components - Use the
useLingui
hook from@lingui/react
:
'use client'
import { useLingui } from '@lingui/react'
export function MyComponent() {
const { i18n } = useLingui()
return (
<div>
<h1>{i18n._("hero.title")}</h1>
<p>{i18n._("hero.description")}</p>
</div>
)
}
- Dynamic Values - Use template literals with the i18n instance:
'use client'
import { useLingui } from '@lingui/react'
export function MyComponent() {
const { i18n } = useLingui()
const userName = "John"
// For simple interpolation
return (
<p>{i18n._("welcome.message", { name: userName })}</p>
)
}
Adding New Translations
- Add your translation keys to the
.json
files insrc/locales/[locale]/messages.json
:
{
"my.new.key": "My translated text"
}
- Run the compile command to generate the JavaScript files:
npm run lingui:compile
Language Switching
The language switcher is available in the navigation bar. It:
- Saves the user's preference in a cookie
- Auto-detects language from browser settings on first visit
- Supports manual switching between languages
File Structure
src/locales/
├── en/
│ ├── messages.json # English translations
│ └── messages.js # Compiled translations (auto-generated)
├── zh-CN/
│ ├── messages.json # Simplified Chinese translations
│ └── messages.js # Compiled translations (auto-generated)
└── zh-HK/
├── messages.json # Traditional Chinese translations
└── messages.js # Compiled translations (auto-generated)
Available Scripts
npm run lingui:extract
- Extract messages from source codenpm run lingui:compile
- Compile .json files to JavaScript
Best Practices
- Use descriptive IDs: Use dot notation for organization (e.g.,
nav.home
,auth.signIn
) - Keep translations updated: Run compile command after adding new translations
- Test all languages: Always test your UI in all supported languages
- Consistent naming: Keep translation keys consistent across all locales
Server Components
For server components, pass the locale from a parent server component:
// Server component wrapper
import { getLocale } from '@/lib/locale'
import { ClientComponent } from './client-component'
export function ServerComponentWrapper() {
const locale = getLocale()
return <ClientComponent locale={locale} />
}
Common Translation Keys
Here are some commonly used translation keys already available:
app.title
- Application titleapp.description
- Application descriptionnav.home
- Home navigationnav.dashboard
- Dashboard navigationnav.templates
- Templates navigationnav.tutorials
- Tutorials navigationhero.title
- Hero section titlehero.subtitle
- Hero section subtitlehero.getStarted
- Get started buttonhero.browseTemplates
- Browse templates buttonauth.signIn
- Sign inauth.signUp
- Sign upauth.signOut
- Sign out
Adding New Languages
To add a new language:
- Update
lingui.config.js
to include the new locale - Update
lib/i18n.ts
to add the locale code and display name - Create a new directory in
src/locales/
with the locale code - Add a
messages.json
file with translations - Run
npm run lingui:compile
to generate the JavaScript files