/
useTranslation.ts
154 lines (140 loc) · 4.85 KB
/
useTranslation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import {useContext} from "react";
import {path} from "ramda";
import {Language, TemplateFunction, TranslationContext, TranslationMap} from "./context.js";
import {Translation, extend} from "./translation.js";
const replaceAll = (string: string, token: string, newToken: string) => {
if (token != newToken)
while (string.indexOf(token) > -1) {
string = string.replace(token, newToken);
}
return string;
};
export type TranslationProperties = {
[key: string]: string | number;
};
const getTranslation = (
translationMap: TranslationMap,
lang: Language,
key: string,
params: TranslationProperties,
): string | number | TemplateFunction | null => {
if (key == null) {
return null;
}
if (typeof params.count === "number") {
const suffix = getSuffix(lang, key, params);
const candidate = path(
`${key}${suffix}`.split(".").map(path => (/^\d+$/.test(path) ? +path : path)),
translationMap[lang],
) as string | null;
if (candidate != null) {
return candidate;
}
}
return path(
key.split(".").map(path => (/^\d+$/.test(path) ? +path : path)),
translationMap[lang],
) as string | null;
};
const applyTemplate = (str: string, params: TranslationProperties) => {
const variables = Object.keys(params);
if (variables.length === 0) {
return str;
}
return variables.reduce((ft: string, key: string) => {
return replaceAll(ft, `{{${key}}}`, params[key].toString());
}, str);
}
export const translate = (
translationMap: TranslationMap,
lang: Language,
key: string,
params: TranslationProperties = {},
fallbackLanguage: Language,
): string => {
const translation =
getTranslation(translationMap, lang, key, params) ??
getTranslation(translationMap, fallbackLanguage, key, params);
if (translation == null) {
return key;
}
if (typeof translation === "object") {
return translate(translationMap, lang, `${key}.default`, params, fallbackLanguage) ?? key;
}
if (typeof translation === "function") {
return translation(params);
}
if (typeof translation === "number"){
return translation.toString();
}
if (typeof translation === "string") {
return applyTemplate(translation, params);
}
return key;
};
const lastDigit = (n: number) => parseInt(n.toString().slice(-1));
const toLowestTheSame = (n: number) => {
if (n >= 2 && n <= 4) {
return 2;
} else if (n > 4) {
return 0;
}
return n;
};
const toRussianCasesRules = (n: number) => {
if (n > 20) {
return lastDigit(n);
} else if (n < 10) {
return n;
}
return 0;
};
const getSuffix = (language: Language, _key: string, params: TranslationProperties) => {
if (typeof params.count === "number") {
if (language === "en") {
return params.count === 1 ? "" : "_plural";
} else if (language === "ru") {
return `_${toLowestTheSame(toRussianCasesRules(params.count))}`;
}
}
return "";
};
export const generateTranslationFunction = (
translations: TranslationMap,
language: Language,
fallbackLanguage: Language,
) => {
return (key: string, params?: TranslationProperties, enforceLanguage?: Language) => {
return translate(translations, enforceLanguage ?? language ?? fallbackLanguage, key, params, fallbackLanguage);
};
};
export const generateDictFunction = (
translationMap: TranslationMap,
language: Language,
fallbackLanguage: Language,
) => {
const ramdaPath = path;
return <T>(
path: string | null,
mapper: (key: string, path: string) => T = key => key as unknown as T,
enforceLanguage?: Language,
): T[] => {
const finalLanguage = enforceLanguage ?? language ?? fallbackLanguage;
const finalPath = path == null ? [finalLanguage as string] : [finalLanguage as string].concat(path.split("."));
const subMap = ramdaPath(finalPath, translationMap) as TranslationProperties;
if (Object.prototype.toString.call(subMap) !== "[object Object]") return [];
return Object.keys(subMap)
.filter(key => key !== "default")
.map(key => mapper(key, path == null ? key : `${path}.${key}`));
};
};
export const useTranslation = (translation?: Translation | TranslationMap) => {
const settings = useContext(TranslationContext);
const {fallbackLanguage, translations} = settings;
const translationMap = translation == null ? translations : extend(translations, translation).translationMap;
return {
t: generateTranslationFunction(translationMap, settings.language, fallbackLanguage),
language: settings?.language ?? fallbackLanguage,
};
};
export type UseTranslationResponse = ReturnType<typeof useTranslation>;