E2E 測試在地化應用程式可能具有挑戰性,翻譯金鑰可能會使測試程式碼更難以閱讀和維護。本文示範如何使用 Playwright 在 React 應用程式中測試 i18next 翻譯,並採用避免翻譯鍵的簡化方法。這個想法可以用在任何有 i18next 或類似函式庫的專案中。
此方法是基於我上一篇文章中有關在 RBAC 應用程式中使用 Playwright 固定裝置進行授權的概念(使用 Playwright 進行測試:使授權更輕鬆、更具可讀性)。
這是一個測試中的實際範例:
const LOCALES = ["en", "es", "zh"]; describe("Author page", () => { for (let locale of LOCALES) { test(`it has a link to articles. {locale: ${locale}}`, async ({ page, tkey }) => { await page.goto("/authors/123"); const link = await page.getByRole("link").nth(0).textContent(); expect(link).toBe(tkey("Mis articulos", "menu")); }); } });
這裡的關鍵概念是使用實際短語而不是 i18n 鍵進行翻譯。傳統方法通常使用翻譯鍵,這可能難以閱讀。例如:expect(link).toBe(t("menu.current_user_articles_link"));。這違背了編寫易於閱讀的測試的原則。
實作圍繞使用與測試中的翻譯鍵相對應的短語進行。以下是典型翻譯文件的範例:
{ "en": { "menu": { "current_user_articles_link": "My articles" } }, "es": { "menu": { "current_user_articles_link": "Mis articulos" } }, "zh": { "menu": { "current_user_articles_link": "我的文章" } } }
我們將其轉換為交換的 JSON 格式,其中短語會對應到對應的按鍵(在本範例中使用西班牙語作為主要語言):
{ "Mis articulos": "menu.current_user_articles_link" }
為了實現此轉換,我們將建立一個實用函數來交換 JSON 檔案中的鍵和值。這是使用 TypeScript 和 Deno 的實作:
import { readdir, readFile, writeFile } from 'node:fs/promises'; import { join } from 'node:path'; const CONSTANTS = { DIRECTORY_PATH: 'app/public/locale/es', // Path to locale language files SWAPPED_FILE_PATH: 'e2e/fixtures/i18n/es_swapped.json', // Output path for swapped JSON FILE_ENCODING: 'utf8', } as const; interface TranslationObject { [key: string]: string | TranslationObject; } type SwappedTranslations = Record<string, Record<string, string>>; function swapKeysAndValues(obj: TranslationObject): Record<string, string> { const swappedObject: Record<string, string> = {}; function traverseObj(value: unknown, path = ''): void { if (value && typeof value === 'object') { for (const [key, val] of Object.entries(value)) { traverseObj(val, path ? `${path}.${key}` : key); } } else if (typeof value === 'string') { swappedObject[value] = path; } } traverseObj(obj); return swappedObject; } const JSON_EXTENSION_REGEX = /\.json$/; async function buildJsonWithNamespaces(): Promise<void> { try { const files = await readdir(CONSTANTS.DIRECTORY_PATH); const result: SwappedTranslations = {}; for (const file of files) { const filePath = join(CONSTANTS.DIRECTORY_PATH, file); const fileContent = await readFile(filePath, CONSTANTS.FILE_ENCODING); const parsedFileContent = JSON.parse(fileContent) as TranslationObject; const swappedContent = swapKeysAndValues(parsedFileContent); const key = file.replace(JSON_EXTENSION_REGEX, ''); result[key] = swappedContent; } await writeFile( CONSTANTS.SWAPPED_FILE_PATH, JSON.stringify(result, null, 2), CONSTANTS.FILE_ENCODING, ); console.info('✅ Successfully generated swapped translations'); } catch (error) { console.error( '❌ Failed to generate swapped translations:', error instanceof Error ? error.message : error, ); process.exit(1); } } console.info('? Converting locale to swapped JSON...'); buildJsonWithNamespaces();
使用以下指令執行此腳本: deno run --allow-read --allow-write path_to_swap.ts
現在讓我們在 Playwright 中為 i18n 建立一個固定裝置。此實現的靈感來自 playwright-i18next-fixture 庫,但進行了自訂修改以實現更好的控制:
import * as fs from "node:fs"; import path from "node:path"; import Backend from "i18next-http-backend"; import { initReactI18next } from "react-i18next"; import { test as base } from "@playwright/test"; import { createInstance, type i18n, type TFunction } from "i18next"; const CONFIG = { TRANSLATIONS_PATH: "translations_path_to_files/or_api_endpoint", SWAPPED_TRANSLATIONS_PATH: "e2e/fixtures/i18n/es_swapped.json", LOCAL_STORAGE_KEY: "locale", SUPPORTED_LANGUAGES: ["es", "en", "zh"] as const, DEFAULT_LANGUAGE: "es", NAMESPACES: ["shared", "menu"] as const, DEFAULT_NS: "shared", } as const; const data = fs.existsSync(CONFIG.SWAPPED_TRANSLATIONS_PATH) ? JSON.parse(fs.readFileSync(CONFIG.SWAPPED_TRANSLATIONS_PATH, "utf8")) : {}; export function findTranslationByKey( key: string, namespace: string, ): string | undefined { const value = data[namespace][key]; if (value && typeof value === "string") return value; throw new Error( `? Translation not found for the namespace "${namespace}" and key "${key}"`, ); } type SupportedLanguage = (typeof CONFIG.SUPPORTED_LANGUAGES)[number]; type Namespace = (typeof CONFIG.NAMESPACES)[number]; export const i18nOptions = { plugins: [Backend, initReactI18next], options: { lng: CONFIG.DEFAULT_LANGUAGE, load: "languageOnly", ns: CONFIG.NAMESPACES, defaultNS: CONFIG.DEFAULT_NS, supportedLngs: CONFIG.SUPPORTED_LANGUAGES, backend: { allowMultiLoading: true, loadPath: CONFIG.TRANSLATIONS_PATH, }, react: { useSuspense: true, }, }, } as const; let storedI18n: i18n | undefined; async function initI18n({ plugins, options, }: typeof i18nOptions): Promise<i18n> { if (!storedI18n?.isInitialized) { const i18n = plugins.reduce( (i18n, plugin) => i18n.use(plugin), createInstance(), ); await i18n.init(options); storedI18n = i18n; return i18n; } return storedI18n; } export type Tkey = (key: string, namespace?: Namespace) => string; interface I18nFixtures { i18n: i18n; t: TFunction; tkey: Tkey; } /* Fixture for i18n functionality. It initializes the i18next instance and checks the language setting in the storageState created by Playwright. Similar technique was used in my RBAC example. For more details, see my article about authorization testing (https://dev.to/a-dev/testing-with-playwright-how-to-make-authorization-less-painful-and-more-readable-3344) and the official Playwright documentation (https://playwright.dev/docs/api/class-browsercontext#browser-context-storage-state). */ export const i18nFixture = base.extend<I18nFixtures>({ i18n: async ({ storageState }, use) => { const i18nInstance = await initI18n(i18nOptions); if (storageState) { try { const data = JSON.parse( fs.readFileSync(path.resolve(storageState as string), "utf8"), ); const localStorage = data?.origins?.[0]?.localStorage; const language = localStorage?.find( (i) => i.name === CONFIG.LOCAL_STORAGE_KEY, )?.value as SupportedLanguage | undefined; if (!language) { throw new Error( `No language setting found in localStorage (key: ${CONFIG.LOCAL_STORAGE_KEY})`, ); } if (!CONFIG.SUPPORTED_LANGUAGES.includes(language)) { throw new Error( `Unsupported language: ${language}. Supported languages: ${CONFIG.SUPPORTED_LANGUAGES.join(", ")}`, ); } // Change language if different from current if (i18nInstance.language !== language) { await i18nInstance.changeLanguage(language); } } catch (error) { throw new Error(`Failed to process storage state: ${error.message}`); } } await use(i18nInstance); }, tkey: async ({ t }, use) => { await use((str?: string, namespace = "shared"): string => { if (!str) return "⭕ Error: no translation"; const tkey = findTranslationByKey(str, namespace); return t(`${namespace}:${tkey}`); }); }, t: async ({ i18n }, use) => { await use(i18n.t); }, });
最後,我們可以在 Playwright 測驗中使用 i18nFixture 來處理翻譯和語言設定。要了解有關使用裝置的更多信息,請查看官方 Playwright 文件。我建議建立一個匯出所有裝置的 index.ts 文件,然後可以將其匯入到您的測試文件中。
import { mergeTests } from '@playwright/test'; import { i18nFixture } from './fixtures/i18n'; const test = mergeTests({ ...i18nFixture, }); export { test };
祝您與 Playwright 和 i18next 一起測試愉快! ?
如果您有任何疑問或建議,請隨時在下面發表評論。
以上是使用 Playwright 進行測試:在測試中使用 iext 翻譯,但不使用 `t(key)`的詳細內容。更多資訊請關注PHP中文網其他相關文章!