feat: init i18n with type hints (#18)

This commit is contained in:
RustamRu 2024-11-16 22:41:32 +03:00
parent 95d26e58a3
commit 8328ee8028
3 changed files with 34 additions and 0 deletions

2
package-lock.json generated
View File

@ -21,6 +21,7 @@
"i18next": "^23.16.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-i18next": "^15.1.1",
"react-icons": "^5.3.0",
"react-router-dom": "^6.27.0"
},
@ -8964,6 +8965,7 @@
"version": "15.1.1",
"resolved": "https://registry.npmjs.org/react-i18next/-/react-i18next-15.1.1.tgz",
"integrity": "sha512-R/Vg9wIli2P3FfeI8o1eNJUJue5LWpFsQePCHdQDmX0Co3zkr6kdT8gAseb/yGeWbNz1Txc4bKDQuZYsC0kQfw==",
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.25.0",
"html-parse-stringify": "^3.0.1"

View File

@ -29,6 +29,7 @@
"i18next": "^23.16.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-i18next": "^15.1.1",
"react-icons": "^5.3.0",
"react-router-dom": "^6.27.0"
},

31
src/models/i18next.d.ts vendored Normal file
View File

@ -0,0 +1,31 @@
import defaultLocale from '../../locales/ru.json';
type Split<S extends string, D extends string> =
S extends `${infer A}${D}${infer B}` ? [A, ...Split<B, D>] : [S];
type NestedObject<T extends string[]> =
T extends [infer Head, ...infer Tail] ?
Head extends string ?
{ [key in Head]: NestedObject<Tail extends string[] ? Tail : []> } : never :
string;
// Основная утилита для обработки union type
type CreateTree<T> =
UnionToIntersection<T extends infer U ?
U extends string ?
NestedObject<Split<U, '.'>> : never : never>;
// Утилита для объединения типов
type UnionToIntersection<U> =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
type LanguageResource = CreateTree<keyof typeof defaultLocale>;
declare module "i18next" {
interface CustomTypeOptions {
resources: {
'~': LanguageResource
};
}
}