mpare. * @param {string} rhsLangTag - The right-hand-side language tag to compare. * * @returns {boolean} * `true` if the language tags match, either directly or after normalization. * `false` if either tag is invalid or empty, or if they do not match. * * @see https://datatracker.ietf.org/doc/html/rfc5646#appendix-A */ static langTagsMatch(lhsLangTag, rhsLangTag) { if (!lhsLangTag || !rhsLangTag) { return false; } if (lhsLangTag === rhsLangTag) { // A simple direct match. return true; } if (lhsLangTag.split("-")[0] !== rhsLangTag.split("-")[0]) { // The language components of the tags do not match so there is no need to normalize them and compare. return false; } try { return ( TranslationsUtils.#normalizeLangTag(lhsLangTag) === TranslationsUtils.#normalizeLangTag(rhsLangTag) ); } catch { // One of the locales is not valid, just continue on to return false. } return false; } /** * Serializes a language pair into a unique key that is human readable. This is useful * for caching, deduplicating, and logging. * * e.g. * "en -> fr" * "en -> fr,base" * "zh-Hans,tiny -> fr,base" * * @param {LanguagePair} languagePair */ static serializeLanguagePair({ sourceLanguage, targetLanguage, sourceVariant, targetVariant, }) { let key = sourceLanguage; if (sourceVariant) { key += `,${sourceVariant}`; } key += ` -> ${targetLanguage}`; if (targetVariant) { key += `,${targetVariant}`; } return key; } /** * Deletes all translations language model files. * * @returns {Promise} - A list of record IDs. */ static async deleteAllLanguageFiles() { const { RemoteSettings } = ChromeUtils.importESModule( "resource://services-settings/remote-settings.sys.mjs" ); const client = RemoteSettings( TranslationsUtils.translationsModelsCollectionName ); try { await client.attachments.deleteAll(); } catch (error) { if (!(error instanceof lazy.RemoteSettingsClient.EmptyDatabaseError)) { lazy.console.error( "Failed to delete Translations language files.", error ); } } try { const records = await client.get({ syncIfEmpty: false, loadDumpIfNewer: false, }); return records.map(record => record.id); } catch (error) { lazy.console.error("Failed to list Translations model records.", error); return []; } } } PK