1export class Translator { 2 constructor(translations) { 3 this.translations = new Map(Object.entries(translations)); 4 } 5 6 static async create(locale, path) { 7 const response = await fetch(`${path}/${locale}.json`); 8 const data = await response.json(); 9 return new Translator(data); 10 } 11 12 translate(key, parameters = []) { 13 if (!this.translations.has(key)) { 14 console.error(`Translation with key ${key} has no value.`); 15 return key; 16 } 17 18 const value = this.translations.get(key); 19 if (parameters.length === 0) { 20 return value; 21 } 22 23 return value.replace(/{(\w+)}/g, (match, key) => { 24 return parameters[key] || match; 25 }); 26 } 27}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.