fix: language detection

This commit is contained in:
cihatata
2025-05-05 23:55:21 +03:00
parent 13eae5dc76
commit e58c47c205
8 changed files with 20 additions and 35 deletions

View File

@@ -1,43 +1,18 @@
import { useEffect } from "react";
import { useSelector } from "react-redux";
import { useDispatch } from "react-redux";
import "react-toastify/dist/ReactToastify.css";
import { ToastContainer } from "react-toastify";
import { ThemeProvider } from "@emotion/react";
import lightTheme from "./Utils/Theme/lightTheme";
import darkTheme from "./Utils/Theme/darkTheme";
import { CssBaseline, GlobalStyles } from "@mui/material";
import { getAppSettings } from "./Features/Settings/settingsSlice";
import { logger } from "./Utils/Logger"; // Import the logger
import { networkService } from "./main";
import { Routes } from "./Routes";
import WalletProvider from "./Components/WalletProvider";
import { useTranslation } from "react-i18next";
import { setLanguage } from "./Features/UI/uiSlice";
function App() {
const mode = useSelector((state) => state.ui.mode);
const { authToken } = useSelector((state) => state.auth);
const dispatch = useDispatch();
const { i18n } = useTranslation();
useEffect(() => {
if (authToken) {
dispatch(getAppSettings({ authToken })).then((action) => {
if (action.payload && action.payload.success) {
const { language } = action.payload.data;
const availableLanguages = Object.keys(i18n.options.resources || {});
if (language && availableLanguages.includes(language)) {
dispatch(setLanguage(language));
i18n.changeLanguage(language);
} else {
dispatch(setLanguage(availableLanguages[0]));
i18n.changeLanguage(availableLanguages[0]);
}
}
});
}
}, [dispatch, authToken, i18n]);
// Cleanup
useEffect(() => {

View File

@@ -23,7 +23,7 @@ const initialState = {
greeting: { index: 0, lastUpdate: null },
timezone: "America/Toronto",
distributedUptimeEnabled: false,
language: "gb",
language: localStorage.getItem("app_language") || "gb",
starPromptOpen: true,
};

View File

@@ -16,6 +16,7 @@ import PasswordStep from "./Components/PasswordStep";
import ThemeSwitch from "../../../Components/ThemeSwitch";
import ForgotPasswordLabel from "./Components/ForgotPasswordLabel";
import LanguageSelector from "../../../Components/LanguageSelector";
import { useTranslation } from "react-i18next";
const DEMO = import.meta.env.VITE_APP_DEMO;
@@ -27,6 +28,7 @@ const Login = () => {
const dispatch = useDispatch();
const navigate = useNavigate();
const theme = useTheme();
const { t } = useTranslation();
const authState = useSelector((state) => state.auth);
const { authToken } = authState;
@@ -63,13 +65,17 @@ const Login = () => {
const handleChange = (event) => {
const { value, id } = event.target;
const name = idMap[id];
const lowerCasedValue = name === idMap["login-email-input"]? value?.toLowerCase()||value : value
const lowerCasedValue =
name === idMap["login-email-input"] ? value?.toLowerCase() || value : value;
setForm((prev) => ({
...prev,
[name]: lowerCasedValue,
}));
const { error } = credentials.validate({ [name]: lowerCasedValue }, { abortEarly: false });
const { error } = credentials.validate(
{ [name]: lowerCasedValue },
{ abortEarly: false }
);
setErrors((prev) => {
const prevErrors = { ...prev };
@@ -114,7 +120,7 @@ const Login = () => {
if (action.payload.success) {
navigate("/uptime");
createToast({
body: "Welcome back! You're successfully logged in.",
body: t("welcomeBack"),
});
} else {
if (action.payload) {

View File

@@ -32,7 +32,7 @@ class NetworkService {
config.headers = {
Authorization: `Bearer ${authToken}`,
"Accept-Language": currentLanguage,
"Accept-Language": currentLanguage === "gb" ? "en" : currentLanguage,
...config.headers,
};
@@ -920,7 +920,7 @@ class NetworkService {
onOpen?.();
};
this.eventSource.addEventListener("open", (e) => {});
this.eventSource.addEventListener("open", (e) => { });
this.eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);

View File

@@ -16,7 +16,7 @@ Object.keys(translations).forEach((path) => {
});
const savedLanguage = store.getState()?.ui?.language;
const initialLanguage = savedLanguage || primaryLanguage;
const initialLanguage = savedLanguage;
i18n.use(initReactI18next).init({
resources,
@@ -32,6 +32,7 @@ i18n.use(initReactI18next).init({
i18n.on("languageChanged", (lng) => {
store.dispatch(setLanguage(lng));
window.localStorage.setItem("app_language", lng);
});
export default i18n;

View File

@@ -18,6 +18,7 @@
"createAPassword": "Create a password",
"authRegisterAlreadyHaveAccount": "Already have an account?",
"commonAppName": "Checkmate",
"welcomeBack": "Welcome back! You're successfully logged in.",
"authLoginEnterEmail": "Enter your email",
"authRegisterTitle": "Create an account",
"authRegisterStepOneTitle": "Create your account",
@@ -396,4 +397,4 @@
"DeleteDescriptionText": "Note that deleting your account will remove all data from the server. This is permanent and non-recoverable.",
"DeleteAccountWarning": "If you delete your account, you will no longer be able to sign in, and all of your data will be deleted. Deleting your account is permanent and non-recoverable action.",
"DeleteWarningTitle": "Really delete this account?"
}
}

View File

@@ -18,6 +18,7 @@
"createAPassword": "Создайте пароль",
"authRegisterAlreadyHaveAccount": "Уже есть аккаунт?",
"commonAppName": "Checkmate",
"welcomeBack": "Добро пожаловать обратно! Вы успешно вошли в систему.",
"authLoginEnterEmail": "Введите свой email",
"authRegisterTitle": "Создать аккаунт",
"authRegisterStepOneTitle": "Создайте свой аккаут",
@@ -364,4 +365,4 @@
"pageSpeedWarning": "Предупреждение: Вы не добавили ключ API Google PageSpeed. Без него монитор PageSpeed не будет работать.",
"pageSpeedLearnMoreLink": "Нажмите здесь, чтобы узнать",
"pageSpeedAddApiKey": "как добавить ваш ключ API."
}
}

View File

@@ -18,6 +18,7 @@
"createAPassword": "Bir parola oluşturun",
"authRegisterAlreadyHaveAccount": "Zaten hesabınız var mı?",
"commonAppName": "Checkmate",
"welcomeBack": "Tekrar hoş geldiniz! Başarıyla giriş yaptınız.",
"authLoginEnterEmail": "E-posta adresinizi girin",
"authRegisterTitle": "Hesap oluştur",
"authRegisterStepOneTitle": "Hesabınızı oluşturun",
@@ -396,4 +397,4 @@
"DeleteDescriptionText": "",
"DeleteAccountWarning": "",
"DeleteWarningTitle": ""
}
}