Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const createTranslator = (dictionary = {}) => (key) => {
return dictionary[key] ?? key;
};

28 changes: 28 additions & 0 deletions server/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import prefixer from './prefixer.js';
import Template from './template.js';
import {getColumns} from './columns.js';
import {getThemes} from './theme.js';
import {readFileSync} from 'node:fs';
import {join, dirname} from 'node:path';
import {fileURLToPath} from 'node:url';

const require = createRequire(import.meta.url);
const {stringify} = JSON;
Expand Down Expand Up @@ -163,6 +166,27 @@ function indexProcessing(config, options) {

const name = config('name');

const __dirname = dirname(fileURLToPath(import.meta.url));
let lang = config('lang');
if (!lang || lang === 'undefined') {
lang = 'en';
}

let dictPath = join(__dirname, '..', 'json', 'i18n', `${lang}.json`);
let [readError, jsonString] = tryCatch(readFileSync, dictPath, 'utf8');

if (readError) {
dictPath = join(__dirname, '..', '..', 'json', 'i18n', `${lang}.json`);
[readError, jsonString] = tryCatch(readFileSync, dictPath, 'utf8');
}

if (readError) {
dictPath = join(__dirname, '..', 'json', 'i18n', 'en.json');
[readError, jsonString] = tryCatch(readFileSync, dictPath, 'utf8');
}

const i18nPack = readError ? {} : JSON.parse(jsonString);

data = rendy(data, {
title: CloudFunc.getTitle({
name,
Expand All @@ -174,7 +198,11 @@ function indexProcessing(config, options) {
themes: getThemes()[config('theme')],
});

const i18nScript = `<script>window.__CLOUDCMD_I18N_PACK__ = ${stringify(i18nPack)};</script>`;
data = data.replace('<head>', `<head>${i18nScript}`);

return data;

}

function buildIndex(config, html, data) {
Expand Down
26 changes: 26 additions & 0 deletions test-e2e/client/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { test, expect } from '@playwright/test';

test.describe('Cloud Commander i18n Client E2E Tests', () => {
test('should inject __CLOUDCMD_I18N_PACK__ into index.html during bootstrap', async ({ page }) => {
await page.goto('/');

const i18nPack = await page.evaluate(() => window.__CLOUDCMD_I18N_PACK__);
expect(i18nPack).toBeDefined();
});

test('should render translated text tokens visible in the user interface context', async ({ page }) => {
await page.goto('/');

await page.evaluate(() => {
window.__CLOUDCMD_I18N_PACK__ = {
"F2 - Rename": "F2 - Zmień nazwę"
};
});

const testToken = await page.evaluate(() => {
return window.__CLOUDCMD_I18N_PACK__["F2 - Rename"];
});

expect(testToken).toBe('F2 - Zmień nazwę');
});
});
Loading