From c9dcb1787da3a0a70e2491684f60a66f3e2786f4 Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Tue, 26 Sep 2023 15:43:00 +0200 Subject: [PATCH] test: translation template strings --- src/locales/index.ts | 3 + src/locales/nl.json | 2 +- src/react-components/translations.spec.ts | 69 ++++++++++++++++++++--- 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/locales/index.ts b/src/locales/index.ts index 5f57bb59f..b86baac71 100644 --- a/src/locales/index.ts +++ b/src/locales/index.ts @@ -4,3 +4,6 @@ export { default as en } from "./en.json" export { default as es } from "./es.json" export { default as de } from "./de.json" +export { default as fr } from "./fr.json" +export { default as nl } from "./nl.json" +export { default as se } from "./se.json" diff --git a/src/locales/nl.json b/src/locales/nl.json index ffc203221..2aaaee0ca 100644 --- a/src/locales/nl.json +++ b/src/locales/nl.json @@ -92,7 +92,7 @@ "identities.messages.4000019": "moet > {minimum} zijn, maar is {actual}", "identities.messages.4000020": "moet <= {maximum} zijn, maar is {actual}", "identities.messages.4000021": "moet < {maximum} zijn, maar is {actual}", - "identities.messages.4000022": "{actual} is geen veelvoud van {basic}", + "identities.messages.4000022": "{actual} is geen veelvoud van {base}", "identities.messages.4000023": "maximaal {max_items} items toegestaan, maar er zijn {actual_items} items gevonden", "identities.messages.4000024": "minimaal {min_items} items toegestaan, maar er zijn {actual_items} items gevonden", "identities.messages.4000025": "items op index {index_a} en {index_b} zijn gelijk", diff --git a/src/react-components/translations.spec.ts b/src/react-components/translations.spec.ts index 1afdee1e8..38e248d6c 100644 --- a/src/react-components/translations.spec.ts +++ b/src/react-components/translations.spec.ts @@ -1,16 +1,69 @@ // Copyright © 2023 Ory Corp // SPDX-License-Identifier: Apache-2.0 -import { expect, test } from "@playwright/experimental-ct-react" +import { expect, test as base } from "@playwright/experimental-ct-react" import * as supportedLanguages from "../locales" -test("keys match", async () => { - const { en, ...otherLanguages } = supportedLanguages - const enKeys = Object.keys(en).sort() +type Language = { + [K in keyof typeof supportedLanguages.en]: string +} - for (const [language, translations] of Object.entries(otherLanguages)) { - await test.step(`Checking ${language}`, () => { - expect(Object.keys(translations).sort()).toEqual(enKeys) +type SupportedLanguages = { + [k in keyof typeof supportedLanguages]: Language +} + +type TemplateStrings = { + [k in keyof typeof supportedLanguages.en]: string[] +} + +const test = base.extend<{ + en: Language + translations: Partial + templates: Partial +}>({ + en: async ({}, use) => { + await use(supportedLanguages.en) + }, + translations: async ({}, use) => { + const translations: Partial = {} + for (const [language, translation] of Object.entries(supportedLanguages)) { + translations[language as keyof typeof supportedLanguages] = translation + } + await use(translations) + }, + templates: async ({ en }, use) => { + const templates: Partial = {} + for (const [key, value] of Object.entries(en)) { + const matches = value.match(/\{.+?}/g) + if (!matches) { + continue + } + templates[key as keyof typeof en] = matches + } + await use(templates) + }, +}) + +test("language keys and templates match", async ({ + en, + translations, + templates, +}) => { + await test.step("Checking language keys", () => { + for (const [, translation] of Object.entries(translations)) { + expect(Object.keys(translation).sort()).toEqual(Object.keys(en).sort()) + } + }) + + await test.step("Checking template strings", () => { + Object.entries(translations).forEach(([language, translation]) => { + console.log(`Checking ${language} template strings`) + Object.entries(templates).forEach(([key, templateStrings]) => { + for (const templateString of templateStrings) { + console.log(`Checking ${language} ${key} ${templateString}`) + expect(translation[key as keyof typeof en]).toContain(templateString) + } + }) }) - } + }) })