feat: unify language handling across the application
- Updated the language configuration to use a single source of truth from `DEFAULT_LANGUAGE` for the bot, API, and Mini App, eliminating auto-detection from user settings. - Refactored the `get_lang` function to always return `DEFAULT_LANGUAGE`, ensuring consistent language usage throughout the application. - Modified the handling of language in various components, including API responses and UI elements, to reflect the new language management approach. - Enhanced documentation and comments to clarify the changes in language handling. - Added unit tests to verify the new language handling behavior and ensure coverage for the updated functionality.
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
/**
|
||||
* Internationalization: language detection and translations for webapp.
|
||||
* Internationalization: language from backend config (window.__DT_LANG) and translations.
|
||||
*/
|
||||
|
||||
import { getInitData } from "./auth.js";
|
||||
|
||||
/** @type {Record<string, Record<string, string>>} */
|
||||
export const MESSAGES = {
|
||||
en: {
|
||||
@@ -135,34 +133,25 @@ const WEEKDAY_KEYS = [
|
||||
* @param {string} code - e.g. 'ru', 'en', 'uk'
|
||||
* @returns {'ru'|'en'}
|
||||
*/
|
||||
function normalizeLang(code) {
|
||||
if (!code || typeof code !== "string") return "ru";
|
||||
export function normalizeLang(code) {
|
||||
if (!code || typeof code !== "string") return "en";
|
||||
const lower = code.toLowerCase();
|
||||
if (lower.startsWith("ru")) return "ru";
|
||||
return "en";
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect language: Telegram initData user.language_code → navigator → fallback 'ru'.
|
||||
* Get application language from backend config (window.__DT_LANG).
|
||||
* Set by /app/config.js from DEFAULT_LANGUAGE. Fallback to 'en' if missing or invalid.
|
||||
* @returns {'ru'|'en'}
|
||||
*/
|
||||
export function getLang() {
|
||||
const initData = getInitData();
|
||||
if (initData) {
|
||||
try {
|
||||
const params = new URLSearchParams(initData);
|
||||
const userStr = params.get("user");
|
||||
if (userStr) {
|
||||
const user = JSON.parse(decodeURIComponent(userStr));
|
||||
const code = user && user.language_code;
|
||||
if (code) return normalizeLang(code);
|
||||
}
|
||||
} catch (e) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
const nav = navigator.language || (navigator.languages && navigator.languages[0]) || "";
|
||||
return normalizeLang(nav);
|
||||
const raw =
|
||||
typeof window !== "undefined" && window.__DT_LANG != null
|
||||
? String(window.__DT_LANG)
|
||||
: "";
|
||||
const lang = normalizeLang(raw);
|
||||
return lang === "ru" ? "ru" : "en";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user