Skip to content

Commit

Permalink
Fire onLanguageChanged only when lang differs
Browse files Browse the repository at this point in the history
Fixes #1269
  • Loading branch information
perry-mitchell committed Mar 31, 2024
1 parent 03d31eb commit 1bf100c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion source/shared/i18n/trans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import i18next, { TOptions } from "i18next";
import translations from "./translations/index";
import { DEFAULT_LANGUAGE } from "../symbols";

let __lastLanguage: string | null = null;

export async function changeLanguage(lang: string) {
await i18next.changeLanguage(lang);
}

export async function initialise(lang: string) {
__lastLanguage = lang;
await i18next.init({
lng: lang,
fallbackLng: DEFAULT_LANGUAGE,
Expand All @@ -24,7 +27,11 @@ export async function initialise(lang: string) {
}

export function onLanguageChanged(callback: (lang: string) => void): () => void {
const cb = (lang: string) => callback(lang);
const cb = (lang: string) => {
if (__lastLanguage === lang) return;
__lastLanguage = lang;
callback(lang);
};
i18next.on("languageChanged", cb);
return () => {
i18next.off("languageChanged", cb);
Expand Down

0 comments on commit 1bf100c

Please sign in to comment.