@bardioc/i18n-core
v0.1.1
Published
Framework-agnostic i18n core for React: provider, hooks and translation utilities with pluggable storage sync
Readme
@bardioc/i18n-core
Reusable i18n core for React: a provider, hooks and translation utilities with pluggable loading and update sync. Ships no translations and has no app-specific dependencies — consumers supply their own messages.
Features
I18nProviderwithuseI18n/useTranslationhooks- Nested message objects with dot-notation lookup (
t('common.buttons.save')) - Interpolation (
{name}), plurals viaIntl.PluralRules, nested references ($t(key)) - Pluggable custom-translation loading (
loadTranslations) and live update subscription (subscribeToUpdates) — bring your own storage (IndexedDB, HTTP, service worker, ...) - Framework-agnostic types, self-contained (no external type dependencies)
Usage
import { I18nProvider, useI18n, type I18nConfig } from '@bardioc/i18n-core';
const config: I18nConfig = {
defaultLocale: 'en',
supportedLocales: ['en', 'de'],
defaultMessages: {
en: { common: { save: 'Save' } },
de: { common: { save: 'Speichern' } },
},
};
function App() {
return (
<I18nProvider
config={config}
loadTranslations={async locale => fetchOverrides(locale)}
subscribeToUpdates={(locale, onUpdate) => subscribeSomewhere(locale, onUpdate)}
>
<MyComponent />
</I18nProvider>
);
}
function MyComponent() {
const { t, setLocale } = useI18n();
return <button onClick={() => setLocale('de')}>{t('common.save')}</button>;
}Custom messages returned by loadTranslations are deep-merged over defaultMessages, so overrides can be partial.
Utilities
mergeTranslations, getNestedValue, setNestedValue, interpolate, getPluralSuffix, resolveNested, flattenTranslations, unflattenTranslations, createTranslationId, parseTranslationId — exported for building storage/sync layers on top.
