From 1bf100c33dd1aa2851ec524d843f20ce1930024f Mon Sep 17 00:00:00 2001 From: Perry Mitchell Date: Sun, 31 Mar 2024 21:49:38 +0300 Subject: [PATCH] Fire onLanguageChanged only when lang differs Fixes #1269 --- source/shared/i18n/trans.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/source/shared/i18n/trans.ts b/source/shared/i18n/trans.ts index b4407cca..7a2636b2 100644 --- a/source/shared/i18n/trans.ts +++ b/source/shared/i18n/trans.ts @@ -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, @@ -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);