diff options
| author | physcik <mynameisgennadiy@vk.com> | 2026-04-11 16:40:22 +0500 |
|---|---|---|
| committer | physcik <mynameisgennadiy@vk.com> | 2026-04-11 16:40:22 +0500 |
| commit | 5d2c8369389013895264caf16e71d44bddabb22c (patch) | |
| tree | c4c9556cce9b25d2a852d32f8e2624c2ff19041b /front/src/Locales | |
| parent | ae614c0d1174d3b5527f1fb3dc6e339d7ad6a10b (diff) | |
Language toggle
Diffstat (limited to 'front/src/Locales')
| -rw-r--r-- | front/src/Locales/Context.tsx | 5 | ||||
| -rw-r--r-- | front/src/Locales/Language.ts | 5 | ||||
| -rw-r--r-- | front/src/Locales/Locales.ts | 30 | ||||
| -rw-r--r-- | front/src/Locales/en_US.ts | 10 | ||||
| -rw-r--r-- | front/src/Locales/ru_RU.ts | 10 |
5 files changed, 60 insertions, 0 deletions
diff --git a/front/src/Locales/Context.tsx b/front/src/Locales/Context.tsx new file mode 100644 index 0000000..a15d57d --- /dev/null +++ b/front/src/Locales/Context.tsx @@ -0,0 +1,5 @@ +import { createContext } from "react"; + +export type AllowedLanguages = "en" | "ru"; + +export const LanguageContext = createContext<AllowedLanguages>("en"); diff --git a/front/src/Locales/Language.ts b/front/src/Locales/Language.ts new file mode 100644 index 0000000..a96ee9f --- /dev/null +++ b/front/src/Locales/Language.ts @@ -0,0 +1,5 @@ +class Language { + LocalizedStrings: Map<string, string>; +} + +export default Language; diff --git a/front/src/Locales/Locales.ts b/front/src/Locales/Locales.ts new file mode 100644 index 0000000..af76045 --- /dev/null +++ b/front/src/Locales/Locales.ts @@ -0,0 +1,30 @@ +import { createContext } from 'vm'; +import { LanguageContext } from './Context'; +import en_us from './en_US'; +import Language from './Language'; +import ru_ru from './ru_RU'; + + +const defaultLocale: Language = en_us; + +const nameToLocale = new Map<string, Language>([ + ['en', en_us], + ['ru', ru_ru], +]); + +function getCurrentLocale(lang: string): Language { + const outp = nameToLocale.get(lang); + if (outp == undefined) return defaultLocale; + return outp; +} + +export function GetString(query: string, lang: string): string { + const locale = getCurrentLocale(lang); + const found = locale.LocalizedStrings.get(query); + if (found == undefined) return defaultLocale.LocalizedStrings.get(query) || query; + return found; +} + +export function SetLocale(locale: string, setLang: (x: string) => void) { + if (nameToLocale.has(locale)) setLang(locale); +} diff --git a/front/src/Locales/en_US.ts b/front/src/Locales/en_US.ts new file mode 100644 index 0000000..f99d324 --- /dev/null +++ b/front/src/Locales/en_US.ts @@ -0,0 +1,10 @@ +import Language from "./Language"; + +const lang = new Language(); +lang.LocalizedStrings = new Map<string, string>([ + ["index", "index"], + ["classes", "classes"], + ["contents", "contents"], +]); + +export default lang; diff --git a/front/src/Locales/ru_RU.ts b/front/src/Locales/ru_RU.ts new file mode 100644 index 0000000..fe1df70 --- /dev/null +++ b/front/src/Locales/ru_RU.ts @@ -0,0 +1,10 @@ +import Language from "./Language"; + +const lang = new Language(); +lang.LocalizedStrings = new Map<string, string>([ + ["index", "главная"], + ["classes", "классы"], + ["contents", "содержание"], +]); + +export default lang; |
