diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index d480101..94113d4 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -124,7 +124,7 @@ if [[ "$MODE" == "frontend" || "$MODE" == "dual" || "$MODE" == "standalone" ]]; TITLE_ESCAPED=$(sed_escape "$TITLE_HTML_ESCAPED") sed -i "s/LibreSpeed<\\/title>/<title>$TITLE_ESCAPED<\\/title>/g; s/<h1>LibreSpeed<\\/h1>/<h1>$TITLE_ESCAPED<\\/h1>/g" /var/www/html/index-classic.html sed -i "s/<title>LibreSpeed<\\/title>/<title>$TITLE_ESCAPED<\\/title>/g" /var/www/html/index.html - sed -i "s/<title>LibreSpeed - Free and Open Source Speedtest<\\/title>/<title>$TITLE_ESCAPED - Free and Open Source Speedtest<\\/title>/g; s/<h1>Free and Open Source Speedtest\\.<\\/h1>/<h1>$TITLE_ESCAPED<\\/h1>/g" /var/www/html/index-modern.html + sed -i "s/<title data-i18n=\"page.title\">LibreSpeed - Free and Open Source Speedtest<\\/title>/<title>$TITLE_ESCAPED - Free and Open Source Speedtest<\\/title>/g; s/<h1 data-i18n=\"brand.title\">Free and Open Source Speedtest\\.<\\/h1>/<h1>$TITLE_ESCAPED<\\/h1>/g" /var/www/html/index-modern.html fi # Replace modern page tagline if TAGLINE is set @@ -133,7 +133,7 @@ if [[ "$MODE" == "frontend" || "$MODE" == "dual" || "$MODE" == "standalone" ]]; TAGLINE_ONE_LINE=${TAGLINE_ONE_LINE//$'\n'/ } TAGLINE_HTML_ESCAPED=$(html_escape "$TAGLINE_ONE_LINE") TAGLINE_ESCAPED=$(sed_escape "$TAGLINE_HTML_ESCAPED") - sed -i "s/<p class=\"tagline\">No Flash, No Java, No Websockets, No Bullsh\\*t<\\/p>/<p class=\"tagline\">$TAGLINE_ESCAPED<\\/p>/g" /var/www/html/index-modern.html + sed -i "s/<p class=\"tagline\" data-i18n=\"brand.tagline\">No Flash, No Java, No Websockets, No Bullsh\\*t<\\/p>/<p class=\"tagline\">$TAGLINE_ESCAPED<\\/p>/g" /var/www/html/index-modern.html fi # Support legacy EMAIL env var as fallback for GDPR_EMAIL diff --git a/frontend/README.md b/frontend/README.md index b19be33..3233d80 100644 --- a/frontend/README.md +++ b/frontend/README.md @@ -18,6 +18,20 @@ page fetches them relative to itself: - `../settings.json` overrides selected `speedtest_worker.js` settings. - `index.html` and `../index-modern.html` show how the frontend is wired up. +## Localization + +The modern page ships with English, Swedish, Portuguese and Spanish. The language is the saved choice, then the first +supported language of the browser, then English; the selector in the footer changes it and saves the choice in the +browser. + +- `javascript/i18n.js` loads `locales/<language>.json` (flat JSON, dotted keys) and fills the elements marked with + `data-i18n="key"` (text) or `data-i18n-attr="attribute:key"` (attributes). The English text stays in the HTML as the + fallback. +- Texts written by `javascript/index.js` go through `LibreSpeedI18n.t(key, fallback, { name: value })`. +- To add a language, copy `locales/en.json` to `locales/<id>.json`, translate it and add `{ id, name }` to `LANGUAGES` in + `javascript/i18n.js`. `tests/e2e/i18n.spec.js` checks that every catalog has the same keys as the English one. +- Setting `TITLE` or `TAGLINE` in the Docker image replaces the translated title, heading and tagline with that text. + ## Notes - The modern frontend expects modern browser features and does not support old diff --git a/frontend/index.html b/frontend/index.html index 8a804d7..1fc6080 100755 --- a/frontend/index.html +++ b/frontend/index.html @@ -8,13 +8,14 @@ /> <meta name="description" - content="Free and Open Source Speedtest. Run it right now in your browser, or self-host on a PHP, Golang, Rust or Node server. License: LGPL." + content="Free and Open Source Speedtest. Run it right now in your browser, or self-host on a PHP, Golang, Rust or Node server. License: LGPL." data-i18n-attr="content:page.description" /> <link rel="shortcut icon" href="images/favicon.svg" /> <script type="text/javascript" src="speedtest.js"></script> + <script type="text/javascript" src="javascript/i18n.js"></script> <script type="text/javascript" src="javascript/index.js"></script> <link rel="stylesheet" type="text/css" href="styling/index.css" /> - <title>LibreSpeed - Free and Open Source Speedtest + LibreSpeed - Free and Open Source Speedtest @@ -22,30 +23,30 @@ LibreSpeed
-

Free and Open Source Speedtest.

-

No Flash, No Java, No Websockets, No Bullsh*t

+

Free and Open Source Speedtest.

+

No Flash, No Java, No Websockets, No Bullsh*t

- select... + select...
-

current server

-

searching nearest server...

+

current server

+

searching nearest server...

@@ -53,100 +54,103 @@

00 Mbps

-

Download

+

Download

00 Mbps

-

Upload

+

Upload

-
- Close + Close
- Test results in graphical form - + Test results in graphical form +
- Close + Close
-

Privacy Policy

-

+

Privacy Policy

+

This HTML5 speed test server is configured with telemetry enabled.

-

What data we collect

-

+

What data we collect

+

At the end of the test, the following data is collected and stored:

-

How we use the data

-

Data collected through this service is used to:

+

How we use the data

+

Data collected through this service is used to:

-

No personal information is disclosed to third parties.

+

No personal information is disclosed to third parties.

-

Your consent

-

+

Your consent

+

By starting the test, you consent to the terms of this privacy policy.

-

Data removal

-

+

Data removal

+

If you want to have your information deleted, you need to provide either the ID of the test or your IP address. This is the only way to identify your data, without this information we won't be able to comply with your request.

- Contact this email address for all deletion requests: + Contact this email address for all deletion requests: TO BE FILLED BY DEVELOPER.

- +
diff --git a/frontend/javascript/i18n.js b/frontend/javascript/i18n.js new file mode 100644 index 0000000..ff9cf66 --- /dev/null +++ b/frontend/javascript/i18n.js @@ -0,0 +1,110 @@ +/** + * Localization for the modern page + * + * Flat JSON catalogs with dotted keys in ../locales/.json. The text of the page in English stays in the HTML + * as the fallback, so a missing key or a failed request leaves the page readable. + * data-i18n="key" sets the text of the element + * data-i18n-attr="attribute:key,..." sets attributes of the element + * LibreSpeedI18n.t(key, fallback, { name: value }) for the text written by scripts, with {name} placeholders + * + * The language is the saved choice (localStorage), then the first supported language of the browser, then English. + * A

- Close + Close
- Test results in graphical form - + Test results in graphical form +
- Close + Close
-

Privacy Policy

-

+

Privacy Policy

+

This HTML5 speed test server is configured with telemetry enabled.

-

What data we collect

-

+

What data we collect

+

At the end of the test, the following data is collected and stored:

    -
  • Test ID
  • -
  • Time of testing
  • -
  • Test results (download and upload speed, ping and jitter)
  • -
  • IP address
  • -
  • ISP information
  • -
  • Approximate location (inferred from IP address, not GPS)
  • -
  • User agent and browser locale
  • -
  • Test log (contains no personal information)
  • +
  • Test ID
  • +
  • Time of testing
  • +
  • Test results (download and upload speed, ping and jitter)
  • +
  • IP address
  • +
  • ISP information
  • +
  • Approximate location (inferred from IP address, not GPS)
  • +
  • User agent and browser locale
  • +
  • Test log (contains no personal information)
-

How we use the data

-

Data collected through this service is used to:

+

How we use the data

+

Data collected through this service is used to:

    -
  • +
  • Allow sharing of test results (sharable image for forums, etc.)
  • -
  • +
  • To improve the service offered to you (for instance, to detect problems on our side)
-

No personal information is disclosed to third parties.

+

No personal information is disclosed to third parties.

-

Your consent

-

+

Your consent

+

By starting the test, you consent to the terms of this privacy policy.

-

Data removal

-

+

Data removal

+

If you want to have your information deleted, you need to provide either the ID of the test or your IP address. This is the only way to identify your data, without this information we won't be able to comply with your request.

- Contact this email address for all deletion requests: + Contact this email address for all deletion requests: TO BE FILLED BY DEVELOPER.

- +
diff --git a/tests/e2e/i18n.spec.js b/tests/e2e/i18n.spec.js new file mode 100644 index 0000000..7d17541 --- /dev/null +++ b/tests/e2e/i18n.spec.js @@ -0,0 +1,156 @@ +const fs = require("node:fs"); +const path = require("node:path"); +const { test, expect } = require("@playwright/test"); + +const staticRepositoryUrl = "http://127.0.0.1:18184"; +const pageUrl = `${staticRepositoryUrl}/index-modern.html`; +const languages = ["en", "sv", "pt", "es"]; +const root = path.resolve(__dirname, "../.."); + +const singleServerList = [ + { + name: "Test server", + server: "//127.0.0.1:18184/", + dlURL: "garbage.php", + ulURL: "empty.php", + pingURL: "empty.php", + getIpURL: "getIP.php", + sponsorName: "Test sponsor", + sponsorURL: "https://example.com/" + } +]; + +// A single server is shown without reachability checks, so the page reaches the READY state offline +async function openPage(page) { + await page.route("**/server-list.json", route => route.fulfill({ json: singleServerList })); + await page.goto(pageUrl); + await expect(page.locator("#start-button")).not.toHaveText("Loading..."); +} + +test.describe("Localization of the modern page", () => { + test.describe("English browser", () => { + test.use({ locale: "en-US" }); + + test("shows the English text of the page and lists the four languages", async ({ page }) => { + await openPage(page); + await expect(page.locator("main > h1")).toHaveText("Free and Open Source Speedtest."); + await expect(page.locator("#start-button")).toHaveText("Let's start"); + await expect(page.locator("html")).toHaveAttribute("lang", "en"); + await expect(page.locator("[data-language-select] option")).toHaveText([ + "English", + "Svenska", + "Português", + "Español" + ]); + }); + + test("changes language from the selector, keeps it after a reload and goes back to English", async ({ page }) => { + await openPage(page); + const select = page.locator("[data-language-select]"); + + await select.selectOption("pt"); + await expect(page.locator("main > h1")).toHaveText("Teste de velocidade livre e de código aberto."); + await expect(page.locator("html")).toHaveAttribute("lang", "pt"); + await expect(page).toHaveTitle(/código aberto/); + await expect(page.locator("#start-button")).toHaveText("Vamos começar"); + await expect(page.locator("#sponsor")).toHaveText("Patrocinador: Test sponsor"); + await expect(page.locator("#sponsor a")).toHaveAttribute("href", "https://example.com/"); + await expect(page.locator("#selected-server")).toHaveText("Test server"); + + await page.reload(); + await expect(page.locator("main > h1")).toHaveText("Teste de velocidade livre e de código aberto."); + await expect(select).toHaveValue("pt"); + + await select.selectOption("en"); + await expect(page.locator("main > h1")).toHaveText("Free and Open Source Speedtest."); + await expect(page.locator("#start-button")).toHaveText("Let's start"); + await expect(page.locator("#sponsor")).toHaveText("Sponsor: Test sponsor"); + }); + + test("translates the privacy policy, the alt texts and the label of the selector", async ({ page }) => { + await openPage(page); + await page.locator("[data-language-select]").selectOption("es"); + await expect(page.locator("#privacy h1")).toHaveText("Política de Privacidad"); + await expect(page.locator("#close-privacy")).toHaveText("Cerrar"); + await expect(page.locator("#privacy a[href^='mailto:']")).toHaveText("TO BE FILLED BY DEVELOPER"); + await expect(page.locator("#share .close-dialog img")).toHaveAttribute("alt", "Cerrar"); + await expect(page.locator("[data-language-select]")).toHaveAttribute("aria-label", "Idioma"); + }); + + test("keeps the page in English when the translation cannot be loaded", async ({ page }) => { + await page.route("**/frontend/locales/sv.json", route => route.abort()); + await openPage(page); + await page.locator("[data-language-select]").selectOption("sv"); + await expect(page.locator("main > h1")).toHaveText("Free and Open Source Speedtest."); + await expect(page.locator("#start-button")).toHaveText("Let's start"); + }); + }); + + test.describe("Swedish browser", () => { + test.use({ locale: "sv-SE" }); + + test("uses the language of the browser, without a flash of English", async ({ page }) => { + await openPage(page); + await expect(page.locator("main > h1")).toHaveText("Gratis hastighetstest med öppen källkod."); + await expect(page.locator("html")).toHaveAttribute("lang", "sv"); + await expect(page.locator("html")).not.toHaveAttribute("data-i18n-pending", ""); + await expect(page.locator("#start-button")).toHaveText("Låt oss börja"); + await expect(page.locator("[data-language-select]")).toHaveValue("sv"); + }); + + test("translates the alert when no server can be reached", async ({ page }) => { + const dialogs = []; + page.on("dialog", dialog => { + dialogs.push(dialog.message()); + dialog.dismiss(); + }); + // Two servers that cannot be reached + await page.route("**/server-list.json", route => + route.fulfill({ json: [singleServerList[0], { ...singleServerList[0], name: "Other server" }] }) + ); + // The servers fail only once the Swedish catalog is in, as they would after the seconds a real ping takes + await page.route("**/empty.php*", async route => { + await page.waitForFunction(() => document.documentElement.lang === "sv"); + await route.abort(); + }); + await page.goto(pageUrl); + await expect.poll(() => dialogs.length).toBeGreaterThan(0); + expect(dialogs[0]).toContain("Det går inte att nå någon av hastighetstestets servrar"); + }); + }); + + test.describe("Browser in a language without a catalog", () => { + test.use({ locale: "de-DE" }); + + test("falls back to English", async ({ page }) => { + await openPage(page); + await expect(page.locator("main > h1")).toHaveText("Free and Open Source Speedtest."); + await expect(page.locator("html")).toHaveAttribute("lang", "en"); + await expect(page.locator("[data-language-select]")).toHaveValue("en"); + }); + }); + + test("every catalog has the keys of the English one, and the pages only use keys that exist", () => { + const catalog = id => JSON.parse(fs.readFileSync(path.join(root, `frontend/locales/${id}.json`), "utf8")); + const english = Object.keys(catalog("en")); + + for (const id of languages) { + expect(Object.keys(catalog(id)), `${id}.json`).toEqual(english); + } + + const sources = ["index-modern.html", "frontend/index.html", "frontend/javascript/index.js"] + .map(file => fs.readFileSync(path.join(root, file), "utf8")) + .join("\n"); + const used = [ + ...[...sources.matchAll(/data-i18n="([^"]+)"/g)].map(match => match[1]), + ...[...sources.matchAll(/data-i18n-attr="([^"]+)"/g)].flatMap(match => + match[1].split(",").map(binding => binding.split(":")[1]) + ), + ...[...sources.matchAll(/\bt\(\s*"([\w.-]+)"/g)].map(match => match[1]) + ]; + expect(used.length).toBeGreaterThan(40); + for (const key of used) { + expect(english, key).toContain(key); + } + }); +}); diff --git a/tests/e2e/static-repository-assets.spec.js b/tests/e2e/static-repository-assets.spec.js index bc91b29..6b6d9a8 100644 --- a/tests/e2e/static-repository-assets.spec.js +++ b/tests/e2e/static-repository-assets.spec.js @@ -8,6 +8,11 @@ const expectedAssets = [ "/server-list.json", "/frontend/styling/index.css", "/frontend/javascript/index.js", + "/frontend/javascript/i18n.js", + "/frontend/locales/en.json", + "/frontend/locales/sv.json", + "/frontend/locales/pt.json", + "/frontend/locales/es.json", "/frontend/images/logo.svg", "/frontend/images/favicon.svg", "/frontend/images/close-button.svg",