-
Notifications
You must be signed in to change notification settings - Fork 4
/
i18n.ts
74 lines (64 loc) · 2.04 KB
/
i18n.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
import i18n from 'i18next'
import Backend from 'i18next-http-backend'
import LanguageDetector from 'i18next-browser-languagedetector'
import {initReactI18next} from 'react-i18next'
const fallbackLanguage = 'en'
export const projectToken = ""; // YOUR PROJECT TOKEN
export const apiKey = ""; // YOUR API KEY
export const cdnBaseUrl = "https://cdn.simplelocalize.io";
export const environment = "_latest"; // or "_production"
const loadPath = `${cdnBaseUrl}/${projectToken}/${environment}/{{lng}}`;
const endpoint = `https://api.simplelocalize.io/api/v1/translations`;
const missingKeysPushInterval = 10_000; // 10 seconds
let missingKeysRequests: any[] = [];
const missingKeyHandler = (
languages: readonly string[],
namespace: string,
key: string,
fallbackValue: string) => {
missingKeysRequests.push({
key,
//namespace: namespace, // uncomment if you use namespaces
language: fallbackLanguage,
text: fallbackValue
});
};
const pushMissingKeys = () => {
if (missingKeysRequests.length > 0) {
console.log(`[SimpleLocalize] Pushing missing keys: ${missingKeysRequests.length}`);
const requestBody = {
content: missingKeysRequests
}
fetch(endpoint, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'X-SimpleLocalize-Token': apiKey
},
body: JSON.stringify(requestBody),
})
missingKeysRequests = [];
}
}
// @refresh reset
setInterval(() => pushMissingKeys(), missingKeysPushInterval)
i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
fallbackLng: fallbackLanguage,
backend: {
loadPath: loadPath,
},
detection: {
order: ['querystring', 'cookie'],
lookupQuerystring: 'hl',
lookupCookie: 'language',
caches: ['cookie']
},
saveMissing: true,
missingKeyHandler
})
export default i18n;