-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathi18n.js
34 lines (32 loc) · 1.21 KB
/
i18n.js
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
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import HttpApi from "i18next-http-backend";
i18n.use(HttpApi) // Load translations via HTTP (use with i18next-http-backend)
.use(LanguageDetector) // Detect user language
.use(initReactI18next) // Pass the i18n instance to react-i18next.
.init({
lng: localStorage.getItem("ispeaker")
? JSON.parse(localStorage.getItem("ispeaker")).language
: "en",
fallbackLng: {
"en-US": ["en"],
"en-GB": ["en"],
"zh-CN": ["zh"],
default: ["en"],
},
load: "languageOnly",
debug: true, // Enable debug messages
backend: {
loadPath: `${import.meta.env.BASE_URL}locales/{{lng}}.json`, // Translation files path
},
interpolation: {
escapeValue: false, // React already escapes values
},
})
.then(() => {
// Ensure the `lang` attribute on `html` is updated immediately
const currentLang = i18n.language || "en";
document.documentElement.setAttribute("lang", currentLang);
});
export default i18n;