Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | 175x 1x 27x 50x 124x 27x 97x 68x 68x 68x 68x 68x 235x 209x 235x 235x 235x 507x 507x 1182x 272x 910x 507x 242x 241x 241x 753x 241x 241x 241x 266x 266x 248x 266x 241x 32x 18x 18x 4x 4x 14x 10x 11x 11x 22x 22x 1x 3x 102x 102x 102x 102x 102x 102x 102x 102x 120x 120x 93x 93x 102x 9x 9x 93x 25x 25x 93x 93x 127x 127x 127x 127x 127x 150x 119x 31x 96x 27x 4x 23x 23x 23x 23x 21x 2x 1x 1x 1x 94x 94x 94x 94x 94x 56x 56x 7x 56x 56x 6x 6x 6x 2x 1x 1x 1x 68x | /**
* Coherent.js Translator
*
* Handles translation of strings with interpolation and pluralization
*
* @module i18n/translator
*/
/**
* Escape a string for literal use inside a regular expression.
* @param {string} value
* @returns {string}
*/
function escapeRegExp(value) {
return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
const HTML_ESCAPES = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
/**
* Escape a value for HTML text or a quoted attribute.
* @param {unknown} value
* @returns {string}
*/
function escapeHtml(value) {
return String(value).replace(/[&<>"']/g, (char) => HTML_ESCAPES[char]);
}
/**
* The primary language subtag of a locale (`pt-BR` → `pt`).
* @param {unknown} locale
* @returns {string}
*/
function primaryLanguage(locale) {
return String(locale).split(/[-_]/)[0].toLowerCase();
}
/**
* `t()`'s third argument is either a locale string (the original signature)
* or an options object `{ locale, escape }`.
* @param {unknown} localeOrOptions
* @returns {{ locale?: string | null, escape?: boolean }}
*/
function normalizeCallOptions(localeOrOptions) {
if (localeOrOptions !== null && typeof localeOrOptions === 'object') {
return localeOrOptions;
}
return { locale: localeOrOptions };
}
/**
* Translator
* Manages translations and locale switching
*/
export class Translator {
constructor(options = {}) {
const { interpolation, ...rest } = options;
this.options = {
defaultLocale: 'en',
fallbackLocale: 'en',
missingKeyHandler: null,
// HTML-escape interpolated params (never the translation itself).
escape: false,
...rest,
// Merged, so overriding only `prefix` keeps the default `suffix`.
interpolation: {
prefix: '{{',
suffix: '}}',
...interpolation
}
};
this.translations = new Map();
this.currentLocale = this.options.defaultLocale;
this.loadedLocales = new Set();
}
/**
* Add translations for a locale
*
* @param {string} locale - Locale code (e.g., 'en', 'fr', 'es')
* @param {Object} translations - Translation object
*/
addTranslations(locale, translations) {
if (!this.translations.has(locale)) {
this.translations.set(locale, {});
}
const existing = this.translations.get(locale);
this.translations.set(locale, this.deepMerge(existing, translations));
this.loadedLocales.add(locale);
}
/**
* Deep merge objects
*/
deepMerge(target, source) {
const result = { ...target };
for (const [key, value] of Object.entries(source)) {
if (value && typeof value === 'object' && !Array.isArray(value)) {
result[key] = this.deepMerge(result[key] || {}, value);
} else {
result[key] = value;
}
}
return result;
}
/**
* Loaded locales that can serve `locale`, most specific first: the locale
* itself, then its parents with trailing subtags dropped (`zh-Hant-TW` →
* `zh-Hant` → `zh`). Matching ignores case and accepts `_` for `-`.
*
* @param {string} locale
* @returns {string[]}
*/
localeCandidates(locale) {
if (typeof locale !== 'string' || locale === '') return [];
const byLowerCase = new Map();
for (const loaded of this.loadedLocales) {
byLowerCase.set(String(loaded).toLowerCase(), loaded);
}
const candidates = [];
const parts = locale.replace(/_/g, '-').toLowerCase().split('-');
while (parts.length > 0) {
const match = byLowerCase.get(parts.join('-'));
if (match !== undefined && !candidates.includes(match)) {
candidates.push(match);
}
parts.pop();
}
return candidates;
}
/**
* Resolve a requested locale to a loaded one (`fr-FR` → `fr` when only
* `fr` is loaded), or `null` when neither it nor a parent is loaded.
*
* @param {string} locale
* @returns {string|null}
*/
resolveLocale(locale) {
return this.localeCandidates(locale)[0] ?? null;
}
/**
* Set current locale
*
* Resolves to the closest loaded locale (`fr-FR` → `fr`); if neither it nor
* a parent is loaded, the fallback locale is used.
*
* @param {string} locale - Locale code
*/
setLocale(locale) {
const resolved = this.resolveLocale(locale);
if (resolved === null) {
console.warn(`Locale ${locale} not loaded, using fallback`);
this.currentLocale = this.options.fallbackLocale;
} else {
this.currentLocale = resolved;
}
}
/**
* Get current locale
*
* @returns {string} Current locale code
*/
getLocale() {
return this.currentLocale;
}
/**
* Get a translator bound to one locale, without touching the shared
* instance's current locale.
*
* Use this on the server: `setLocale()` mutates `currentLocale`, which every
* concurrent request rendering with the same instance shares, so one
* request's locale leaks into another's output. A bound translator always
* passes its own locale. It reads the shared translations live, so
* translations added later are visible.
*
* The locale resolves like `setLocale()` (`fr-FR` → `fr`), falling back to
* the fallback locale — silently, since request locales are untrusted input.
*
* @param {string} locale - Requested locale (e.g. from Accept-Language)
* @param {{escape?: boolean}} [options] - `escape` default for this
* translator's calls (defaults to the shared instance's `escape`)
* @returns {{ locale: string, t: Function, has: Function, getLocale: () => string }}
*/
forLocale(locale, options = {}) {
const boundLocale = this.resolveLocale(locale) ?? this.options.fallbackLocale;
return {
locale: boundLocale,
t: (key, params = {}, localeOrOptions = null) => {
const callOptions = normalizeCallOptions(localeOrOptions);
return this.t(key, params, {
locale: callOptions.locale || boundLocale,
escape: callOptions.escape ?? options.escape ?? this.options.escape
});
},
has: (key, override = null) => this.has(key, override || boundLocale),
getLocale: () => boundLocale
};
}
/**
* Translate a key
*
* @param {string} key - Translation key (supports dot notation)
* @param {Object} [params] - Interpolation parameters
* @param {string|{locale?: string|null, escape?: boolean}|null} [localeOrOptions]
* Override locale, or `{ locale, escape }`. `escape: true` HTML-escapes the
* interpolated params (defaults to the translator's `escape` option).
* @returns {string} Translated string
*/
t(key, params = {}, localeOrOptions = null) {
const callOptions = normalizeCallOptions(localeOrOptions);
const targetLocale = callOptions.locale || this.currentLocale;
const escape = callOptions.escape ?? this.options.escape;
params = params || {};
// Look the key up in the target locale, its parents (fr-FR → fr), then
// the fallback locale and its parents.
const chain = [
...this.localeCandidates(targetLocale),
...this.localeCandidates(this.options.fallbackLocale)
];
let translation = null;
let messageLocale = targetLocale;
for (const candidate of chain) {
translation = this.getTranslation(key, candidate);
if (translation !== null) {
messageLocale = candidate;
break;
}
}
// Handle missing translation
if (translation === null) {
Iif (this.options.missingKeyHandler) {
return this.options.missingKeyHandler(key, targetLocale);
}
return key;
}
// Handle pluralization with the rules of the language the message is
// written in: English fallback text must not use Russian plural rules.
if (typeof translation === 'object' && params.count !== undefined) {
const pluralLocale = primaryLanguage(messageLocale) === primaryLanguage(targetLocale)
? targetLocale
: messageLocale;
translation = this.selectPlural(translation, params.count, pluralLocale);
}
// Interpolate parameters
Eif (typeof translation === 'string') {
return this.interpolate(translation, params, { escape });
}
return String(translation);
}
/**
* Get translation from nested object
*/
getTranslation(key, locale) {
const translations = this.translations.get(locale);
Iif (!translations) return null;
const keys = String(key).split('.');
let value = translations;
for (const k of keys) {
// Own properties only: `constructor`, `toString`, `__proto__` … are
// inherited from Object.prototype and are not translations.
if (value && typeof value === 'object' && Object.hasOwn(value, k)) {
value = value[k];
} else {
return null;
}
}
return value;
}
/**
* Select plural form
*/
selectPlural(pluralObject, count, locale) {
// Check for explicit zero first (takes precedence over Intl rules)
if (count === 0 && pluralObject.zero) {
return pluralObject.zero;
}
// Use Intl.PluralRules for locale-specific pluralization
Eif (typeof Intl !== 'undefined' && Intl.PluralRules) {
let rule;
try {
rule = new Intl.PluralRules(locale).select(count);
} catch {
// Not a valid BCP 47 tag (e.g. `en_US`): use the simple rules below.
}
if (rule !== undefined && pluralObject[rule]) {
return pluralObject[rule];
}
}
// Fallback to simple rules
if (count === 1 && pluralObject.one) {
return pluralObject.one;
} else Eif (pluralObject.other) {
return pluralObject.other;
}
return pluralObject.one || pluralObject.other || '';
}
/**
* Interpolate parameters into string
*
* @param {string} str - Translation template (trusted; never escaped)
* @param {Object} params - Values for the placeholders
* @param {{escape?: boolean}} [options] - `escape: true` HTML-escapes each
* value; defaults to the translator's `escape` option
*/
interpolate(str, params, options = {}) {
Iif (!params || typeof params !== 'object') return str;
const escape = options.escape ?? this.options.escape;
const format = escape ? escapeHtml : String;
const names = Object.keys(params);
if (names.length === 0) return str;
const { prefix, suffix } = this.options.interpolation;
// Longest names first, so `{{ab}}` is never read as `{{a}}` + `b}}`.
const alternatives = names
.sort((a, b) => b.length - a.length)
.map(escapeRegExp)
.join('|');
const pattern = new RegExp(`${escapeRegExp(prefix)}(${alternatives})${escapeRegExp(suffix)}`, 'g');
// One pass with a replacer function: `$&`, `$'` or `$$` in a value are
// inserted literally, and a value that itself contains a placeholder is
// not interpolated a second time.
return str.replace(pattern, (_match, name) => format(params[name]));
}
/**
* Check if translation exists
*
* @param {string} key - Translation key
* @param {string} [locale] - Locale to check
* @returns {boolean} True if translation exists
*/
has(key, locale = null) {
const targetLocale = locale || this.currentLocale;
// The locale and its parents (fr-FR → fr), but not the fallback locale.
return this.localeCandidates(targetLocale)
.some(candidate => this.getTranslation(key, candidate) !== null);
}
/**
* Get all translations for current locale
*
* @returns {Object} All translations
*/
getTranslations(locale = null) {
const targetLocale = locale || this.currentLocale;
return this.translations.get(targetLocale) || {};
}
/**
* Get all loaded locales
*
* @returns {Array<string>} Array of locale codes
*/
getLoadedLocales() {
return Array.from(this.loadedLocales);
}
/**
* Remove translations for a locale
*
* @param {string} locale - Locale code
*/
removeLocale(locale) {
this.translations.delete(locale);
this.loadedLocales.delete(locale);
Iif (this.currentLocale === locale) {
this.currentLocale = this.options.defaultLocale;
}
}
/**
* Clear all translations
*/
clear() {
this.translations.clear();
this.loadedLocales.clear();
this.currentLocale = this.options.defaultLocale;
}
}
/**
* Create a translator instance
*
* @param {Object} [options] - Translator options
* @returns {Translator} Translator instance
*/
export function createTranslator(options = {}) {
return new Translator(options);
}
/**
* Create a scoped translator
* Automatically prefixes all keys with a namespace
*
* @param {Translator} translator - Base translator
* @param {string} namespace - Namespace prefix
* @returns {Object} Scoped translator
*/
export function createScopedTranslator(translator, namespace) {
return {
t: (key, params, locale) => {
return translator.t(`${namespace}.${key}`, params, locale);
},
has: (key, locale) => {
return translator.has(`${namespace}.${key}`, locale);
},
getLocale: () => translator.getLocale(),
setLocale: (locale) => translator.setLocale(locale)
};
}
export default {
Translator,
createTranslator,
createScopedTranslator
};
|