| name | Internationalization |
| description | Multi-language support with i18n-js |
Internationalization (i18n)
Basic Usage
import { i18n } from '../services/i18nService';
<Text>{i18n.t('library.title')}</Text>
<Text>{i18n.t('reader.pageCount', { current: 5, total: 20 })}</Text>
<Text>{i18n.t('library.mangaCount', { count: items.length })}</Text>
Locale Files
Location: src/locales/
| File | Language |
|---|
en.json | English (default) |
vi.json | Vietnamese |
ja.json | Japanese |
zh.json | Chinese |
ko.json | Korean |
es.json | Spanish |
pt.json | Portuguese |
fr.json | French |
de.json | German |
ru.json | Russian |
id.json | Indonesian |
th.json | Thai |
ar.json | Arabic |
ms.json | Malay |
fil.json | Filipino |
tr.json | Turkish |
it.json | Italian |
Translation File Format
{
"common": {
"loading": "Loading...",
"error": "Error",
"retry": "Retry",
"cancel": "Cancel",
"save": "Save",
"delete": "Delete"
},
"library": {
"title": "Library",
"empty": "Your library is empty",
"mangaCount": {
"one": "{{count}} manga",
"other": "{{count}} manga"
}
},
"reader": {
"pageCount": "Page {{current}} of {{total}}"
}
}
i18n Service Setup
import { I18n } from 'i18n-js';
import * as Localization from 'expo-localization';
import en from '../locales/en.json';
import vi from '../locales/vi.json';
import ja from '../locales/ja.json';
export const i18n = new I18n({
en, vi, ja,
});
i18n.defaultLocale = 'en';
i18n.enableFallback = true;
i18n.locale = Localization.locale.split('-')[0];
Language Settings
const currentLang = i18n.locale;
export function setLanguage(langCode: string) {
i18n.locale = langCode;
AsyncStorage.setItem('@language', langCode);
}
export async function loadLanguagePreference() {
const saved = await AsyncStorage.getItem('@language');
if (saved) {
i18n.locale = saved;
}
}
Adding New Translation
- Create
src/locales/{langCode}.json
- Copy structure from
en.json
- Translate all strings
- Import in
i18nService.ts:
import newLang from '../locales/newLang.json';
export const i18n = new I18n({
en, vi, ja, newLang,
});
- Add to
app.json for iOS:
"CFBundleLocalizations": ["en", "vi", "ja", "newLang"]
RTL Support
import { I18nManager } from 'react-native';
const isRTL = I18nManager.isRTL;
I18nManager.forceRTL(true);
Date/Time Formatting
const formatted = new Date().toLocaleDateString(i18n.locale);
const timeAgo = i18n.t('common.timeAgo', { time: '2 hours' });