From 9781a0ad6888de433c906172206cc61c14204b46 Mon Sep 17 00:00:00 2001 From: Tola Leng Date: Fri, 23 May 2025 20:40:29 +0800 Subject: [PATCH] Updated Language Context --- application/src/contexts/LanguageContext.tsx | 26 ++++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/application/src/contexts/LanguageContext.tsx b/application/src/contexts/LanguageContext.tsx index 3bbebd0..a8b13bb 100644 --- a/application/src/contexts/LanguageContext.tsx +++ b/application/src/contexts/LanguageContext.tsx @@ -1,11 +1,11 @@ import React, { createContext, useContext, useState, ReactNode } from "react"; -import { translations, Language, TranslationKey } from "@/translations"; +import { translations, Language, TranslationModule, TranslationKey } from "@/translations"; type LanguageContextType = { language: Language; setLanguage: (language: Language) => void; - t: (key: string) => string; + t: (key: string, module?: M) => string; }; const LanguageContext = createContext({ @@ -19,8 +19,24 @@ export const useLanguage = () => useContext(LanguageContext); export const LanguageProvider = ({ children }: { children: ReactNode }) => { const [language, setLanguage] = useState("en"); - const t = (key: string) => { - return translations[language][key as TranslationKey] || key; + const t = (key: string, module?: M): string => { + // If module is provided, look up in that specific module + if (module) { + const translatedValue = translations[language][module][key as TranslationKey]; + return typeof translatedValue === 'string' ? translatedValue : key; + } + + // If no module is provided, search through all modules + for (const mod in translations[language]) { + const moduleKey = mod as TranslationModule; + const translatedValue = translations[language][moduleKey][key as any]; + if (translatedValue && typeof translatedValue === 'string') { + return translatedValue; + } + } + + // If no translation found, return the key + return key; }; return ( @@ -28,4 +44,4 @@ export const LanguageProvider = ({ children }: { children: ReactNode }) => { {children} ); -}; +}; \ No newline at end of file