UNPKG

gcal-commander

Version:

A command-line interface for Google Calendar operations

55 lines (54 loc) 2.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.I18nService = void 0; const i18next_1 = __importDefault(require("i18next")); const i18next_fs_backend_1 = __importDefault(require("i18next-fs-backend")); const node_path_1 = __importDefault(require("node:path")); const languages_1 = require("../constants/languages"); class I18nService { initialized = false; async changeLanguage(language) { if (!this.initialized) { await this.init(); } await i18next_1.default.changeLanguage(language); } async init(language) { if (this.initialized) { // If language is specified and different from current, change language if (language && i18next_1.default.language !== language) { await i18next_1.default.changeLanguage(language); } return; } // eslint-disable-next-line unicorn/prefer-module const localesPath = node_path_1.default.join(__dirname, '../locales'); const loadPath = node_path_1.default.join(localesPath, '{{lng}}/{{ns}}.json'); await i18next_1.default.use(i18next_fs_backend_1.default).init({ lng: language || languages_1.DEFAULT_LANGUAGE, // Use provided language or default to English fallbackLng: languages_1.DEFAULT_LANGUAGE, preload: [languages_1.DEFAULT_LANGUAGE], // Preload only default language for faster startup backend: { loadPath, }, ns: ['common', 'commands'], // Available namespaces defaultNS: 'commands', interpolation: { escapeValue: false, // React already escapes values }, }); this.initialized = true; } // eslint-disable-next-line @typescript-eslint/no-explicit-any t(key, options) { if (!this.initialized) { throw new Error(`I18n service not initialized. Cannot translate key: ${key}`); } const result = i18next_1.default.t(key, options); return result; } } exports.I18nService = I18nService;