Skip to content

Commit

Permalink
Remember language selection
Browse files Browse the repository at this point in the history
  • Loading branch information
yell0wsuit committed Dec 22, 2024
1 parent 5ea2daf commit f50eb01
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 23 deletions.
4 changes: 3 additions & 1 deletion i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ 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: "en",
lng: localStorage.getItem("ispeaker")
? JSON.parse(localStorage.getItem("ispeaker")).language
: "en",
fallbackLng: {
"en-US": ["en"],
"en-GB": ["en"],
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": "yell0wsuit",
"description": "An English-learning interactive tool written in React, designed to help learners practice speaking and listening",
"private": true,
"version": "3.0.6",
"version": "3.0.7",
"type": "module",
"main": "main.cjs",
"license": "Apache-2.0",
Expand Down
2 changes: 2 additions & 0 deletions src/components/general/AccentDropdown.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import AccentLocalStorage from "../../utils/AccentLocalStorage";
import { sonnerSuccessToast } from "../../utils/sonnerCustomToast";

// Emoji SVGs import
import UKFlagEmoji from "../../emojiSvg/emoji_u1f1ec_1f1e7.svg";
Expand All @@ -24,6 +25,7 @@ const AccentDropdown = ({ onAccentChange }) => {
const handleAccentChange = (value) => {
setSelectedAccent(value);
onAccentChange(value);
sonnerSuccessToast(t("settingPage.changeSaved"));
};

return (
Expand Down
7 changes: 7 additions & 0 deletions src/components/setting_page/LanguageSwitcher.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useTranslation } from "react-i18next";
import { LuExternalLink } from "react-icons/lu";
import { isElectron } from "../../utils/isElectron";
import { sonnerSuccessToast } from "../../utils/sonnerCustomToast";

// Utility function to open external links
const openExternal = (url) => {
Expand Down Expand Up @@ -31,6 +32,12 @@ const LanguageSwitcher = () => {
const handleLanguageChange = (lng) => {
i18n.changeLanguage(lng);
document.documentElement.setAttribute("lang", lng); // Update HTML lang attribute

const ispeakerSettings = JSON.parse(localStorage.getItem("ispeaker")) || {};
ispeakerSettings.language = lng;
localStorage.setItem("ispeaker", JSON.stringify(ispeakerSettings));

sonnerSuccessToast(t("settingPage.changeSaved"));
};

const currentLanguage =
Expand Down
4 changes: 2 additions & 2 deletions src/components/setting_page/ResetSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslation } from "react-i18next";
import { isElectron } from "../../utils/isElectron";
import { sonnerSuccessToast } from "../../utils/sonnerCustomToast";

const ResetSettings = ({ onReset }) => {
const ResetSettings = () => {
const { t } = useTranslation();

const [isResettingLocalStorage, setIsResettingLocalStorage] = useState(false);
Expand Down Expand Up @@ -44,7 +44,7 @@ const ResetSettings = ({ onReset }) => {
localStorage.clear();
setIsResettingLocalStorage(false);
localStorageModal.current?.close();
onReset();
window.location.reload();
sonnerSuccessToast(t("settingPage.changeSaved"));
};
deleteRequest.onerror = () => setIsResettingLocalStorage(false);
Expand Down
13 changes: 2 additions & 11 deletions src/components/setting_page/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import Container from "../../ui/Container";
import { isElectron } from "../../utils/isElectron";
import { useTheme } from "../../utils/ThemeContext/useTheme";
import TopNavBar from "../general/TopNavBar";
import AppearanceSettings from "./Appearance";
import AppInfo from "./AppInfo";
Expand All @@ -15,21 +14,13 @@ import VideoDownloadSubPage from "./VideoDownloadSubPage";

const SettingsPage = () => {
const { t } = useTranslation();
const { resetTheme } = useTheme();

useEffect(() => {
document.title = `${t("navigation.settings")} | iSpeakerReact v${__APP_VERSION__}`;
}, [t]);

const [resetFlag, setResetFlag] = useState(false); // Boolean flag to force remount
const [currentPage, setCurrentPage] = useState("settings");

const handleReset = () => {
// Change the key to remount the CachingSettings component
setResetFlag((prevFlag) => !prevFlag);
resetTheme();
};

const handleVideoDownloadMenuPage = () => {
setCurrentPage("video-download");
};
Expand All @@ -43,7 +34,7 @@ const SettingsPage = () => {
<TopNavBar />
<Container>
<div className="my-8 flex justify-center">
<div className="w-full md:w-2/3 lg:w-1/2" key={resetFlag}>
<div className="w-full md:w-2/3 lg:w-1/2">
{currentPage === "settings" && (
<>
<h1 className="text-3xl font-semibold md:text-4xl">
Expand Down Expand Up @@ -77,7 +68,7 @@ const SettingsPage = () => {
</>
)}
<div className="divider"></div>
<ResetSettings onReset={handleReset} />
<ResetSettings />
</div>
</>
)}
Expand Down
7 changes: 0 additions & 7 deletions src/utils/ThemeContext/ThemeProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,12 @@ export function ThemeProvider({
};
}, [theme]);

// Method to reset the theme
const resetTheme = () => {
localStorage.removeItem(storageKey); // Remove the theme from localStorage
setTheme(defaultTheme); // Reset to the default theme
};

const value = {
theme,
setTheme: (newTheme) => {
localStorage.setItem(storageKey, newTheme);
setTheme(newTheme);
},
resetTheme, // Add resetTheme to the context value
};

return <ThemeProviderContext.Provider value={value}>{children}</ThemeProviderContext.Provider>;
Expand Down

0 comments on commit f50eb01

Please sign in to comment.