@morphixai/i18n
v0.1.0
Published
Framework-neutral i18n core with React bindings — nested message merging, runtime locale switching.
Downloads
23
Maintainers
Readme
@morphixai/i18n
Framework-neutral i18n core with an optional React binding. Messages are grouped into modules, merged across a fallback chain, and resolved through a catalog that interpolates at call time. Adapters convert the same modules into next-intl or i18next shapes, so this can sit under an existing stack rather than replace it.
Install
npm install @morphixai/i18nQuick start
import { defineMessages, createCatalog } from '@morphixai/i18n';
const nav = defineMessages('nav', {
'en-US': { home: 'Home', greeting: 'Hi {name}' },
'zh-CN': { home: '首页', greeting: '你好 {name}' },
});
const catalog = createCatalog({
locale: 'zh-CN',
modules: [nav],
fallbackLocales: ['en-US'], // default
});
catalog.t('nav.home'); // 首页
catalog.t('nav.greeting', { name: 'Ada' }); // 你好 AdaA key missing in the active locale falls through the chain to en-US, so a
partially translated locale degrades to English instead of showing the raw key.
Overrides
Per-module overrides are deep-merged over the module's own messages — useful for white-labelling without forking the catalog:
createCatalog({
locale: 'en-US',
modules: [nav],
overrides: { nav: { home: 'Dashboard' } },
});React
import { MorphixI18nProvider, useT } from '@morphixai/i18n';
function App() {
return (
<MorphixI18nProvider locale="zh-CN" modules={[nav]}>
<Nav />
</MorphixI18nProvider>
);
}
function Nav() {
const t = useT('nav'); // module-scoped — keys are relative
return <a>{t('greeting', { name: 'Ada' })}</a>;
}Providers nest: an inner MorphixI18nProvider inherits the parent's locale
unless it sets its own, so a subtree can be pinned to one language.
Adapters
import { toNextIntlMessages, toI18nextResources } from '@morphixai/i18n';
toNextIntlMessages(catalog); // → next-intl `messages`
toI18nextResources([nav]); // → i18next `resources`Subpath exports
| Entry | Contents |
|---|---|
| @morphixai/i18n | everything below, re-exported |
| @morphixai/i18n/core | createCatalog, defineMessages, deepMerge, interpolate |
| @morphixai/i18n/react | MorphixI18nProvider, useT, useCatalog |
| @morphixai/i18n/messages | built-in common / auth message modules |
License
MIT
