Skip to main content

๐ŸŒ How to Translate JetShip in Your Own Language?

ยท 3 min read

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 in i18n-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.