@dmhd6219/i18next-slavic
v0.1.0
Published
Typed Slavic-case (declension) helper hook for react-i18next, plus a CLI that generates resource types from your locale JSON.
Maintainers
Readme
i18next-slavic
Typed Slavic-case (declension) helper for react-i18next,
plus a small CLI that generates fully-typed resource definitions from your locale JSON.
Slavic languages (Russian, Ukrainian, Polish, …) inflect nouns across six
grammatical cases. i18next-slavic maps each case to an i18next
context, so you keep
one translation key and let i18next pick the right form — with full TypeScript
autocomplete on your keys.
Install
npm install @dmhd6219/i18next-slavic
# peers (you almost certainly already have these):
npm install i18next react-i18next reactThe hook
import { useSlavicTranslation } from '@dmhd6219/i18next-slavic';
function Greeting() {
const { tCase, t } = useSlavicTranslation('common');
return (
<p>
{/* "Добро пожаловать в Москву" — resolved from `city_accusative` */}
Добро пожаловать в {tCase('city', 'accusative')}
</p>
);
}useSlavicTranslation is a thin wrapper over useTranslation and returns the
usual { t, i18n, ready } plus:
tCase(
key: TranslationKey, // typed from your own resources
wordCase: SlavicCase, // 'nominative' | 'genitive' | 'dative' | …
options?: TOptions,
defaultValue?: string,
): stringSlavicCase covers all six cases:
type SlavicCase =
| 'nominative'
| 'genitive'
| 'dative'
| 'accusative'
| 'instrumental'
| 'prepositional';Locale JSON
Provide one base key plus a context-suffixed variant per case:
{
"city": "Москва",
"city_genitive": "Москвы",
"city_dative": "Москве",
"city_accusative": "Москву",
"city_instrumental": "Москвой",
"city_prepositional": "Москве"
}The CLI
The bundled i18next-slavic command scans your locale JSON and writes a typed
Resources interface plus the i18next module augmentation that powers
autocomplete in the hook.
npx @dmhd6219/i18next-slavic --locales public/locales/ru --out src/@types/resources.d.tsAfter installing, the local bin is exposed as
i18next-slavic, so inside an npm script you can call it directly (see below).
| Option | Alias | Default | Description |
| ------ | ----- | ------- | ----------- |
| --locales <dir> | -l | public/locales/ru | Locale folder to read |
| --out <file> | -o | src/@types/resources.d.ts | Output .d.ts file |
| --default-ns <ns> | -n | common | defaultNS written into i18next.d.ts |
| --cwd <dir> | -c | auto-detected | Project root |
| --help | -h | | Show help |
It writes two files:
<out>— theResourcesinterface, regenerated on every run.<out-dir>/i18next.d.ts— the i18next augmentation, created once and then left untouched so your manual tweaks survive.
Add it to your scripts so types stay in sync:
{
"scripts": {
"i18n:types": "i18next-slavic"
}
}Context/plural variants (keys containing
_) are intentionally excluded from the generated key union — you reference the base key and pass the case totCase.
License
MIT
