import {PATHLOCALES} from "@/shared/constants/index.js"; class Localization { #locale =…
- User
import {PATH_LOCALES} from "@/shared/constants/index.js";
class Localization { #locale = ''; #defaultLanguage = 'en'; #dictionary = {};
constructor(locale = 'en') { this.#locale = locale; }
get dictionary() { return this.#dictionary; }
get locale() { return this.#locale; }
#constructLocalePath(locale) { return
${PATH_LOCALES}/${locale}.json; }async #fetchTranslation (locale) { let translation = {};
try { const localePath = this.#constructLocalePath(locale); const response = await fetch(localePath); translation = await response.json(); } catch (error) { throw new Error(`Не удалось загрузить перевод для языка ${locale} (${error})`); } return translation;}
async #loadTranslation(locale) { try { return await this.#fetchTranslation(locale); } catch (error) { console.error(error); if (locale !== this.#defaultLanguage) { return await this.#loadTranslation(this.#defaultLanguage); } } }
async loadLanguage(locale) { const formattedLocale = locale.split('-')[0];
if (this.#locale === formattedLocale) return; this.#locale = formattedLocale; this.#dictionary = await this.#loadTranslation(formattedLocale);}
translate(key) { return this.#dictionary[key] || key; } }
export default new Localization(); перепиши на effector
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.