@nice2dev/i18n
v1.4.64
Published
Shared internationalization infrastructure for Nice2Dev UI libraries
Maintainers
Readme
@nice2dev/i18n
Shared internationalization infrastructure for Nice2Dev UI libraries.
Features
- NiceI18nProvider — React context for translations
- 30 built-in languages, loaded one at a time (see Language coverage)
- Placeholders — texts carry
{name}-style placeholders the controls fill in - RTL support — Arabic, Hebrew with automatic dir attribute
- Browser detection —
detectBrowserLanguage()auto-detects user preference - Custom translations — plug in your own
t()function (react-i18next compatible)
Language coverage
| Coverage | Languages | | ------------------------------------------ | ------------------------------------------------------------------------- | | Complete — every text of every control | en, pl, de, fr, es, uk; it, pt (Brazilian), cs, ja, ko, ar (since 1.3.14) | | Partial — a few hundred common texts | zh | | Basic — a few dozen universal terms | nl, sv, no, da, fi, sk, hu, ro, bg, hi, bn, th, vi, id, sw, fil, ms |
A key a dictionary does not have shows the control's built-in English text (the default passed
to t(key, default)), not a key name. Your own overrides (or your own t) fill any gap.
Installation
npm install @nice2dev/i18nQuick Start
Option 1: Built-in Dictionaries (Simplest)
import { NiceI18nProvider } from '@nice2dev/i18n';
// or re-exported from @nice2dev/ui
function App() {
const [lang, setLang] = useState('en');
return (
<NiceI18nProvider lang={lang}>
<LanguageSelector value={lang} onChange={setLang} />
<MyApp />
</NiceI18nProvider>
);
}All Nice2Dev components automatically pick up translations from the provider.
The provider loads only the dictionary of lang, with import(), so your bundler gives each
language its own chunk. Until it arrives the controls show their built-in English; to start in
the language, load it before the first render:
import { NiceI18nProvider, loadNiceDictionary } from '@nice2dev/i18n';
await loadNiceDictionary('pl');
root.render(
<NiceI18nProvider lang="pl">
<App />
</NiceI18nProvider>,
);Or pass fallback to render something else meanwhile. Up to 1.1.x the provider carried all thirty
dictionaries; 1.1.10 added this loading as NiceI18nLazyProvider, which is now the same component.
If your bundler config groups modules by package (manualChunks), leave
@nice2dev/i18n/dist/dictionaries/ out of the group — see the
setup guide.
Option 2: Custom Translation Function
If you use react-i18next, i18next, or any other i18n library:
import { useTranslation } from 'react-i18next';
import { NiceI18nProvider } from '@nice2dev/i18n';
function App() {
const { t } = useTranslation();
return (
<NiceI18nProvider t={t}>
<MyApp />
</NiceI18nProvider>
);
}Option 3: No Provider (English Defaults)
All components work without a provider — they show English by default.
<NiceButton>Click me</NiceButton> {/* Works fine */}API Reference
NiceI18nProvider
| Prop | Type | Description |
| ----------- | -------------------------- | ----------------------------------------------------------------------------- |
| lang | NiceSupportedLang | Built-in language code (default: 'en'); its dictionary is loaded on first use |
| t | (key, default) => string | Custom translation function (takes priority over lang; nothing is loaded) |
| overrides | Record<string, string> | Extra translations merged on top of built-in dictionary |
| fallback | ReactNode | Rendered instead of children until the first dictionary has loaded |
| onError | (error) => void | Called when a dictionary fails to load |
| children | ReactNode | App content |
loadNiceDictionary
loadNiceDictionary(lang) loads one language's dictionary and caches it; concurrent calls share
one request. getLoadedNiceDictionary(lang) returns it once loaded, otherwise undefined.
useNiceTranslation
Hook for translating text in components:
import { useNiceTranslation } from '@nice2dev/i18n';
function MyComponent() {
const { t } = useNiceTranslation();
return <span>{t('save', 'Save')}</span>; // Returns localized "Save"
}@nice2dev/i18n/all — every dictionary at once
NICE_I18N_DICTIONARIES, createTranslator, NICE_I18N_VARIANTS / resolvePlVariant and the
translation tooling (getMissingTranslations, getTranslationStats, NICE_GLOSSARY, …) need all
thirty languages, so they live on their own entry (1.2.0). Use it in build scripts, tests or a
translation editor — not in app code that only shows one language.
import { createTranslator } from '@nice2dev/i18n/all';
const t = createTranslator('de');
console.log(t('save', 'Save')); // "Speichern"ICU MessageFormat
import { formatICU, plural, ordinal, select } from '@nice2dev/i18n';
// Pluralization
formatICU('{count, plural, one{# item} other{# items}}', { count: 5 });
// → "5 items"
// Ordinals
formatICU('{n, selectordinal, one{#st} two{#nd} few{#rd} other{#th}}', { n: 3 });
// → "3rd"
// Gender select
formatICU('{gender, select, male{He} female{She} other{They}} liked it', { gender: 'female' });
// → "She liked it"RTL Support
import { RTLProvider, useIsRTL, isRTLLanguage } from '@nice2dev/i18n';
// Wrap your app with RTL-aware container
<RTLProvider lang="ar">
<App /> {/* dir="rtl" automatically applied */}
</RTLProvider>;
// Check if current language is RTL
function MyComponent() {
const isRTL = useIsRTL();
return <div style={{ textAlign: isRTL ? 'right' : 'left' }}>...</div>;
}detectBrowserLanguage
import { detectBrowserLanguage } from '@nice2dev/i18n';
const userLang = detectBrowserLanguage(); // e.g. 'pl' if browser is PolishSupported Languages
| Code | Language | Native Name |
| ----- | ------------ | ----------- |
| en | English | English |
| pl | Polish | Polski |
| de | German | Deutsch |
| fr | French | Français |
| es | Spanish | Español |
| it | Italian | Italiano |
| pt | Portuguese | Português |
| nl | Dutch | Nederlands |
| sv | Swedish | Svenska |
| no | Norwegian | Norsk |
| da | Danish | Dansk |
| fi | Finnish | Suomi |
| cs | Czech | Čeština |
| sk | Slovak | Slovenčina |
| hu | Hungarian | Magyar |
| ro | Romanian | Română |
| bg | Bulgarian | Български |
| uk | Ukrainian | Українська |
| ja | Japanese | 日本語 |
| ko | Korean | 한국어 |
| zh | Chinese | 中文 |
| ar | Arabic (RTL) | العربية |
| hi | Hindi | हिन्दी |
| bn | Bengali | বাংলা |
| th | Thai | ไทย |
| vi | Vietnamese | Tiếng Việt |
| id | Indonesian | Indonesia |
| sw | Swahili | Kiswahili |
| fil | Filipino | Filipino |
| ms | Malay | Melayu |
Adding Custom Translations
Extend built-in dictionaries with the overrides prop:
<NiceI18nProvider lang="en" overrides={{ 'myapp.greeting': 'Hello!' }}>
...
</NiceI18nProvider>Or provide your own complete dictionary via t prop.
Translation Keys
All Nice2Dev components use namespaced keys like:
ok,cancel,save,close— common actionsnav.home,nav.menu— navigationdataGrid.sort,dataGrid.filter— grid-specificform.required,form.invalid— validation messages
See src/dictionaries/en.ts for the full list.
License
MIT
