๐ How to Translate JetShip in Your Own Language?
The JetShip Next.js Starter Kit comes with built-in internationalization (i18n) support using the powerful i18next library. This guide will walk you through the process of translating the starter kit into your preferred language.
๐๏ธ Understanding the Structureโ
The internationalization setup in JetShip follows a clean and organized structure:
apps/web/src/data/locales/
โโโ en/
โ โโโ translation.json
โโโ fr/
โ โโโ translation.json
โโโ de/
โโโ translation.json
The starter kit comes with three default languages: English (en), French (fr), and German (de). Each language has its own directory containing translation files organized by namespaces.
๐ Namespaces in i18nextโ
Namespaces are a way to organize your translations into logical groups. The starter kit uses the following namespace structure:
-
translation
(default namespace): Contains general UI translations -
You can add more namespaces for specific features or sections of your app
-
The namespace configuration is defined in
i18n-settings.ts
:export const defaultI18nNamespaces = ['translation']
๐ Language Persistenceโ
The starter kit uses cookies to persist the user's language preference. This means:
- The selected language is saved in the browser
- When the user returns to your site, their language preference is automatically restored
- The language preference works across page refreshes and browser session
๐ ๏ธ Steps to Add Your Languageโ
1. ๐๏ธ Create a New Language Directoryโ
- Navigate to
apps/web/src/data/locales/
- Create a new directory with your language code (e.g.,
es
for Spanish,it
for Italian) - Create a
translation.json
file inside this directory
2. โ๏ธ Add Translation Keysโ
-
Copy the content from
en/translation.json
-
Translate each value while keeping the keys unchanged
-
Example
{
"welcome": "Welcome" // English
}
// becomes
{
"welcome": "Bienvenido" // Spanish
}
3. ๐ง Register Your Languageโ
- Open
apps/web/src/lib/i18n/i18n-settings.ts
- Add your language code to the
languages
array - The first language in the array serves as the fallback language
4. ๐ Adding New Namespaces (Optional)โ
-
Create a new JSON file in each language directory with your namespace name
locales/
โโโ en/
โ โโโ translation.json
โ โโโ dashboard.json // New namespace
โโโ fr/
โ โโโ translation.json
โ โโโ dashboard.json -
Add the namespace to
defaultI18nNamespaces
ini18n-settings.ts
๐ Best Practicesโ
1. ๐งฉ Maintain Key Structureโ
- Keep the same key structure across all language files
- Use descriptive keys that reflect the content
- Group related translations using nested objects
2. โ๏ธ Handle Dynamic Contentโ
- Use placeholders for dynamic content:
{{variable}}
- Keep the same placeholder names across all translations
3. ๐งช Testing Translationsโ
- Test your translations in development mode
- Check for missing translations
- Verify that dynamic content works correctly
๐ฅ๏ธ Using Translations in Componentsโ
Using the Trans
component to translate:
import Trans from '@repo/i18n/src/Trans'
const ComponentPage = () => {
return (
<div>
<Trans i18nKey='welcome' />
</div>
)
}
export default ComponentPage
Using the useTranslation
Hook:
import { useTranslation } from 'react-i18next'
const ComponentPage = () => {
const { t } = useTranslation()
return <div>{t('hello')}</div>
}
export default ComponentPage
๐ Language Switchingโ
The starter kit includes a language switcher component that:
- Automatically detects the user's preferred language
- Allows manual language selection
- Persists the selection in cookies
- Provides immediate feedback when language is changed
โ Conclusionโ
Translating the JetShip Next.js Starter Kit is straightforward thanks to its well-organized i18n implementation. By following these steps, you can make your application accessible to users in multiple languages, expanding your global reach.