Skip to content

Commit

Permalink
test: translation template strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Benehiko committed Sep 26, 2023
1 parent 235a3fb commit c9dcb17
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/locales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion src/locales/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
69 changes: 61 additions & 8 deletions src/react-components/translations.spec.ts
Original file line number Diff line number Diff line change
@@ -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<SupportedLanguages>
templates: Partial<TemplateStrings>
}>({
en: async ({}, use) => {
await use(supportedLanguages.en)
},
translations: async ({}, use) => {
const translations: Partial<SupportedLanguages> = {}
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<TemplateStrings> = {}
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)
}
})
})
}
})
})

0 comments on commit c9dcb17

Please sign in to comment.