File size: 11,558 Bytes
fe77b2f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
// Enhanced Internationalization (i18n) utility for the AI Chat application
class I18n {
constructor() {
this.currentLocale = 'en';
this.translations = {};
this.fallbackLocale = 'en';
this.loadedLocales = new Set();
this.cache = new Map();
this.observers = [];
this.pluralRules = new Map();
this.dateTimeFormats = new Map();
this.numberFormats = new Map();
}
// Load translations for a specific locale with caching
async loadLocale(locale) {
if (this.loadedLocales.has(locale)) {
return this.translations[locale];
}
try {
const response = await fetch(`../locales/${locale}.json`);
if (!response.ok) {
throw new Error(`Failed to load locale ${locale}: ${response.status}`);
}
const translations = await response.json();
this.translations[locale] = translations;
this.loadedLocales.add(locale);
// Cache commonly used translations
this.cacheTranslations(locale, translations);
// Setup locale-specific formatters
this.setupLocaleFormatters(locale);
return translations;
} catch (error) {
console.warn(`Could not load locale ${locale}:`, error);
if (locale !== this.fallbackLocale) {
return await this.loadLocale(this.fallbackLocale);
}
throw error;
}
}
// Cache frequently used translations
cacheTranslations(locale, translations) {
const commonKeys = [
'app.title',
'chat.placeholder',
'chat.send',
'ui.loading',
'ui.error',
'ui.retry'
];
commonKeys.forEach(key => {
const value = this.getNestedValue(translations, key);
if (value !== undefined) {
this.cache.set(`${locale}:${key}`, value);
}
});
}
// Setup locale-specific formatters
setupLocaleFormatters(locale) {
try {
// Date/time formatters
this.dateTimeFormats.set(locale, {
short: new Intl.DateTimeFormat(locale, {
timeStyle: 'short'
}),
medium: new Intl.DateTimeFormat(locale, {
dateStyle: 'medium',
timeStyle: 'short'
}),
relative: new Intl.RelativeTimeFormat(locale, {
numeric: 'auto'
})
});
// Number formatters
this.numberFormats.set(locale, {
decimal: new Intl.NumberFormat(locale),
percent: new Intl.NumberFormat(locale, {
style: 'percent'
}),
currency: new Intl.NumberFormat(locale, {
style: 'currency',
currency: 'USD'
})
});
// Plural rules
this.pluralRules.set(locale, new Intl.PluralRules(locale));
} catch (error) {
console.warn(`Failed to setup formatters for ${locale}:`, error);
}
}
// Set the current locale with enhanced features
async setLocale(locale) {
const previousLocale = this.currentLocale;
if (!this.translations[locale]) {
await this.loadLocale(locale);
}
this.currentLocale = locale;
// Update document language
document.documentElement.lang = locale;
// Update direction for RTL languages
const rtlLanguages = ['ar', 'he', 'fa', 'ur'];
document.documentElement.dir = rtlLanguages.includes(locale) ? 'rtl' : 'ltr';
// Store preference
localStorage.setItem('i18n-locale', locale);
// Notify observers
this.notifyObservers(locale, previousLocale);
// Trigger global locale change event
document.dispatchEvent(new CustomEvent('localeChanged', {
detail: {
locale: this.currentLocale,
previousLocale
}
}));
}
// Enhanced translation with context and pluralization
t(key, params = {}) {
const cacheKey = `${this.currentLocale}:${key}`;
// Check cache first
if (this.cache.has(cacheKey) && Object.keys(params).length === 0) {
return this.cache.get(cacheKey);
}
let translation = this.getNestedValue(
this.translations[this.currentLocale],
key
);
if (translation === undefined) {
// Fallback to default locale
translation = this.getNestedValue(
this.translations[this.fallbackLocale],
key
);
if (translation === undefined) {
console.warn(`Translation missing for key: ${key}`);
return key;
}
}
// Handle pluralization
if (typeof translation === 'object' && params.count !== undefined) {
translation = this.handlePluralization(translation, params.count);
}
// Handle interpolation
const result = this.interpolate(translation, params);
// Cache result if no parameters
if (Object.keys(params).length === 0) {
this.cache.set(cacheKey, result);
}
return result;
}
// Handle pluralization rules
handlePluralization(translations, count) {
const rules = this.pluralRules.get(this.currentLocale);
if (!rules) return translations.other || '';
const rule = rules.select(count);
return translations[rule] ||
translations.other ||
translations.one ||
'';
}
// Enhanced interpolation with formatting
interpolate(text, params) {
if (typeof text !== 'string') return text;
return text.replace(/\{\{(\w+)(?::(\w+))?\}\}/g, (match, key, format) => {
const value = params[key];
if (value === undefined) return match;
// Apply formatting if specified
if (format) {
return this.formatValue(value, format);
}
return value;
});
}
// Format values based on type
formatValue(value, format) {
const formatters = this.numberFormats.get(this.currentLocale);
const dateFormatters = this.dateTimeFormats.get(this.currentLocale);
switch (format) {
case 'number':
return formatters?.decimal.format(value) || value;
case 'percent':
return formatters?.percent.format(value) || value;
case 'currency':
return formatters?.currency.format(value) || value;
case 'date':
return dateFormatters?.medium.format(new Date(value)) || value;
case 'time':
return dateFormatters?.short.format(new Date(value)) || value;
case 'relative':
const now = Date.now();
const diff = Math.floor((value - now) / 1000);
return dateFormatters?.relative.format(diff, 'second') || value;
case 'uppercase':
return String(value).toUpperCase();
case 'lowercase':
return String(value).toLowerCase();
case 'capitalize':
return String(value).charAt(0).toUpperCase() + String(value).slice(1);
default:
return value;
}
}
// Get nested value from object using dot notation
getNestedValue(obj, path) {
return path.split('.').reduce((current, key) => {
return current && current[key] !== undefined ? current[key] : undefined;
}, obj);
}
// Add observer for locale changes
addObserver(callback) {
this.observers.push(callback);
}
// Remove observer
removeObserver(callback) {
const index = this.observers.indexOf(callback);
if (index > -1) {
this.observers.splice(index, 1);
}
}
// Notify all observers of locale change
notifyObservers(newLocale, oldLocale) {
this.observers.forEach(callback => {
try {
callback(newLocale, oldLocale);
} catch (error) {
console.error('Error in i18n observer:', error);
}
});
}
// Get current locale
getCurrentLocale() {
return this.currentLocale;
}
// Get available locales
getAvailableLocales() {
return Array.from(this.loadedLocales);
}
// Get all loaded translations (for debugging)
getAllTranslations() {
return this.translations;
}
// Initialize the i18n system with enhanced detection
async init(locale = null) {
try {
// Detect locale in order of preference
const detectedLocale = locale ||
localStorage.getItem('i18n-locale') ||
this.detectBrowserLocale() ||
this.fallbackLocale;
await this.loadLocale(detectedLocale);
await this.setLocale(detectedLocale);
console.log(`i18n initialized with locale: ${detectedLocale}`);
return true;
} catch (error) {
console.error('Failed to initialize i18n:', error);
return false;
}
}
// Detect browser locale
detectBrowserLocale() {
if (navigator.languages && navigator.languages.length) {
// Check each preferred language
for (const lang of navigator.languages) {
const shortLang = lang.split('-')[0];
// Return first supported language
const supportedLocales = ['en', 'es', 'fr', 'de', 'cs'];
if (supportedLocales.includes(shortLang)) {
return shortLang;
}
}
}
return navigator.language?.split('-')[0] || 'en';
}
// Preload multiple locales
async preloadLocales(locales) {
const promises = locales.map(locale => this.loadLocale(locale));
await Promise.allSettled(promises);
}
// Clear cache
clearCache() {
this.cache.clear();
}
// Get memory usage statistics
getStats() {
return {
loadedLocales: this.loadedLocales.size,
cachedTranslations: this.cache.size,
observers: this.observers.length,
currentLocale: this.currentLocale,
memoryUsage: JSON.stringify(this.translations).length
};
}
}
// Enhanced translation helper functions
class TranslationHelpers {
static createKeyExtractor(prefix = '') {
return (key) => prefix ? `${prefix}.${key}` : key;
}
static createScopedTranslator(i18n, scope) {
return (key, params) => i18n.t(`${scope}.${key}`, params);
}
static validateTranslations(translations, requiredKeys) {
const missing = [];
requiredKeys.forEach(key => {
if (!this.hasKey(translations, key)) {
missing.push(key);
}
});
return missing;
}
static hasKey(obj, path) {
return path.split('.').reduce((current, key) => {
return current && current[key] !== undefined ? current[key] : undefined;
}, obj) !== undefined;
}
static flattenTranslations(obj, prefix = '') {
const flattened = {};
for (const key in obj) {
const newKey = prefix ? `${prefix}.${key}` : key;
if (typeof obj[key] === 'object' && obj[key] !== null) {
Object.assign(flattened, this.flattenTranslations(obj[key], newKey));
} else {
flattened[newKey] = obj[key];
}
}
return flattened;
}
}
// Create global instance
const i18n = new I18n();
// Helper function for easy access with enhanced features
function t(key, params = {}) {
return i18n.t(key, params);
}
// Additional helper functions
function tc(key, count, params = {}) {
return i18n.t(key, { ...params, count });
}
function td(key, date, params = {}) {
return i18n.t(key, { ...params, date });
}
function tn(key, number, params = {}) {
return i18n.t(key, { ...params, number });
}
// Export for use in other modules
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
I18n,
i18n,
t,
tc,
td,
tn,
TranslationHelpers
};
}
// Export to global scope for debugging
window.i18nDebug = {
i18n,
TranslationHelpers,
getStats: () => i18n.getStats(),
clearCache: () => i18n.clearCache(),
getAllTranslations: () => i18n.getAllTranslations()
}; |