1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-13 06:43:35 +00:00

[CSA-27] Use new dependency-free locale service for WebAuthN translations (#4557)

This commit is contained in:
Matt Bishop
2023-02-04 09:23:42 -05:00
committed by GitHub
parent dc3a0b25cb
commit dcc7846138
8 changed files with 306 additions and 254 deletions

View File

@@ -1,5 +1,7 @@
import { I18nService as BaseI18nService } from "@bitwarden/common/services/i18n.service";
import { SupportedTranslationLocales } from "../../translation-constants";
export class I18nService extends BaseI18nService {
constructor(systemLanguage: string, localesDirectory: string) {
super(systemLanguage || "en-US", localesDirectory, async (formattedLocale: string) => {
@@ -14,61 +16,6 @@ export class I18nService extends BaseI18nService {
return locales;
});
// Please leave 'en' where it is, as it's our fallback language in case no translation can be found
this.supportedTranslationLocales = [
"en",
"af",
"ar",
"az",
"be",
"bg",
"bn",
"bs",
"ca",
"cs",
"da",
"de",
"el",
"en-GB",
"en-IN",
"eo",
"es",
"et",
"eu",
"fi",
"fil",
"fr",
"he",
"hi",
"hr",
"hu",
"id",
"it",
"ja",
"ka",
"km",
"kn",
"ko",
"lv",
"ml",
"nb",
"nl",
"nn",
"pl",
"pt-PT",
"pt-BR",
"ro",
"ru",
"si",
"sk",
"sl",
"sr",
"sv",
"tr",
"uk",
"vi",
"zh-CN",
"zh-TW",
];
this.supportedTranslationLocales = SupportedTranslationLocales;
}
}

View File

@@ -0,0 +1,31 @@
import { TranslationService as BaseTranslationService } from "@bitwarden/common/services/translation.service";
import { SupportedTranslationLocales } from "../translation-constants";
export class TranslationService extends BaseTranslationService {
private _translationLocale: string;
constructor(systemLanguage: string, localesDirectory: string) {
super(systemLanguage || "en-US", localesDirectory, async (formattedLocale: string) => {
const filePath =
this.localesDirectory +
"/" +
formattedLocale +
"/messages.json?cache=" +
process.env.CACHE_TAG;
const localesResult = await fetch(filePath);
const locales = await localesResult.json();
return locales;
});
this.supportedTranslationLocales = SupportedTranslationLocales;
}
get translationLocale(): string {
return this._translationLocale;
}
set translationLocale(locale: string) {
this._translationLocale = locale;
}
}

View File

@@ -1,5 +1,6 @@
import { b64Decode, getQsParam } from "./common";
import { buildDataString, parseWebauthnJson } from "./common-webauthn";
import { TranslationService } from "./translation.service";
require("./webauthn.scss");
@@ -7,9 +8,8 @@ let parsed = false;
let webauthnJson: any;
let parentUrl: string = null;
let sentSuccess = false;
let locale = "en";
let locales: any = {};
let locale: string = null;
let localeService: TranslationService = null;
function parseParameters() {
if (parsed) {
@@ -24,7 +24,7 @@ function parseParameters() {
parentUrl = decodeURIComponent(parentUrl);
}
locale = getQsParam("locale").replace("-", "_");
locale = getQsParam("locale") ?? "en";
const version = getQsParam("v");
@@ -61,18 +61,19 @@ function parseParametersV2() {
document.addEventListener("DOMContentLoaded", async () => {
parseParameters();
try {
locales = await loadLocales(locale);
localeService = new TranslationService(locale, "locales");
} catch {
// eslint-disable-next-line
console.error("Failed to load the locale", locale);
locales = await loadLocales("en");
error("Failed to load the provided locale " + locale);
localeService = new TranslationService("en", "locales");
}
document.getElementById("msg").innerText = translate("webAuthnFallbackMsg");
document.getElementById("remember-label").innerText = translate("rememberMe");
await localeService.init();
document.getElementById("msg").innerText = localeService.t("webAuthnFallbackMsg");
document.getElementById("remember-label").innerText = localeService.t("rememberMe");
const button = document.getElementById("webauthn-button");
button.innerText = translate("webAuthnAuthenticate");
button.innerText = localeService.t("webAuthnAuthenticate");
button.onclick = start;
document.getElementById("spinner").classList.add("d-none");
@@ -81,23 +82,13 @@ document.addEventListener("DOMContentLoaded", async () => {
content.classList.remove("d-none");
});
async function loadLocales(newLocale: string) {
const filePath = `locales/${newLocale}/messages.json?cache=${process.env.CACHE_TAG}`;
const localesResult = await fetch(filePath);
return await localesResult.json();
}
function translate(id: string) {
return locales[id]?.message || "";
}
function start() {
if (sentSuccess) {
return;
}
if (!("credentials" in navigator)) {
error(translate("webAuthnNotSupported"));
error(localeService.t("webAuthnNotSupported"));
return;
}
@@ -133,7 +124,7 @@ async function initWebAuthn(obj: any) {
window.postMessage({ command: "webAuthnResult", data: dataString, remember: remember }, "*");
sentSuccess = true;
success(translate("webAuthnSuccess"));
success(localeService.t("webAuthnSuccess"));
} catch (err) {
error(err);
}

View File

@@ -0,0 +1,56 @@
// Please leave 'en' where it is, as it's our fallback language in case no translation can be found
export const SupportedTranslationLocales: string[] = [
"en",
"af",
"ar",
"az",
"be",
"bg",
"bn",
"bs",
"ca",
"cs",
"da",
"de",
"el",
"en-GB",
"en-IN",
"eo",
"es",
"et",
"eu",
"fi",
"fil",
"fr",
"he",
"hi",
"hr",
"hu",
"id",
"it",
"ja",
"ka",
"km",
"kn",
"ko",
"lv",
"ml",
"nb",
"nl",
"nn",
"pl",
"pt-PT",
"pt-BR",
"ro",
"ru",
"si",
"sk",
"sl",
"sr",
"sv",
"tr",
"uk",
"vi",
"zh-CN",
"zh-TW",
];