Internationalization
Bunship uses next-intl for internationalization support
Bunship uses next-intl to implement internationalization features, with default support for English and Chinese, easily extensible to other languages.
File Structure
Bunship uses a multi-file system to manage translation content. All translation files are located in the i18n/messages directory, organized as follows:
Each language directory contains multiple JSON files, with each file corresponding to translations for specific features or pages.
Translation File Examples
Below are basic examples of JSON translation file structures:
Using Translations
In Client Components
To use translations in client components, use the useTranslations hook:
In Server Components
For server components, use the getTranslations function:
Language Switching
Bunship uses URL path prefixes to differentiate languages, for example:
- English:
/en/dashboard - Chinese:
/zh/dashboard
Example component for language switching:
Adding New Languages
To add support for a new language, follow these steps:
-
Create a new language directory in
i18n/messages, such asjafor Japanese -
Copy all JSON files from the
endirectory to the new language directory and translate the content -
Add the new language to the configuration in
lib/i18n.ts:
- Add the new language option to your language switcher component
URL Routing Strategy
Bunship uses path-based internationalization routing, with each URL path containing a language code prefix:
For example:
/en/dashboard- English dashboard/zh/dashboard- Chinese dashboard
Best Practices
- Maintain naming consistency: Use the same key structure across all language versions
- Avoid hardcoded text: Always use translation functions instead of hardcoded text
- Modularity: Split translation files by feature or page to avoid large files
- Reuse common translations: Place commonly used text in
common.jsonfor reuse - Mind the formatting: Different languages may have different date, currency, etc. formats; use next-intl formatting features
Dynamic Content
For dynamic content, you can use parameters:
Corresponding translation file:
For more advanced internationalization features and usage, please refer to the next-intl official documentation.