Skip to content

Commit

Permalink
Translations: Add script to convert from translation template data to…
Browse files Browse the repository at this point in the history
… TypeScript
  • Loading branch information
personalizedrefrigerator committed Sep 21, 2023
1 parent 095cfe1 commit 96762b4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/firebase-hosting-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Install dependencies
run: npm install
- name: Build
run: bash ./build_tools/build-website.sh
run: bash ./scripts/build-website.sh
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Lint
run: npm run lint-ci
- name: Build pages
run: bash build_tools/build-website.sh
run: bash scripts/build-website.sh
- name: Setup Pages
uses: actions/configure-pages@v2
- name: Upload artifact
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"lint-staged": "lint-staged",
"lint-ci": "eslint . --max-warnings=0 --ext .js --ext .ts",
"build-translation-templates": "lerna run build-translation-templates",
"translation-template-to-ts": "ts-node scripts/markdownTranslationFormToTs.ts",
"preinstall": "git config core.hookspath .githooks",
"postinstall": "npm run build"
},
Expand Down
File renamed without changes.
80 changes: 80 additions & 0 deletions scripts/markdownTranslationFormToTs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

// Converts a response to the GitHub translation form to
// possible contents of a TypeScript file.

import {
createInterface as createReadlineInterface, Interface as ReadlineInterface
} from 'node:readline/promises';
import { stdin, stderr } from 'node:process';

const readAllInput = (inputReader: ReadlineInterface): Promise<string> => {
const lines: string[] = [];
inputReader.on('line', line => {
lines.push(line);
});

return new Promise(resolve => {
inputReader.on('close', () => {
resolve(lines.join('\n'));
});
});
};

const main = async () => {
const inputReader = createReadlineInterface({ input: stdin, output: stderr });
inputReader.prompt();

const input = await readAllInput(inputReader);

const data: Record<string, string> = {};

let nextKey: string|null = null;
for (const line of input.split('\n')) {
if (!nextKey) {
nextKey = line;
continue;
} else if (line.trim() === '') {
continue;
}

const value = line;
const currentKey = nextKey;
nextKey = null;

if (value !== 'No response') {
data[currentKey] = value;
}
}

const lang = data.Language;
delete data.Language;

const translationLines: string[] = [];
for (let key in data) {
key = key.replace(/[^0-9a-zA-Z_]/g, '?');
if (key.startsWith('_')) {
throw new Error('Invalid key, ' + key);
}

let value = data[key];
if (!value.match(/^(?:function|\(.*=>)/)) {
value = JSON.stringify(value);
}

translationLines.push(`\t${key}: ${value},`);
}

const tsFileData = [
'import { defaultEditorLocalization, EditorLocalization } from \'../localization\';',
'throw new Error(\'This file needs to be manually verified -- translations can contain JavaScript\');',
`// ${lang} localization`,
'const localization: EditorLocalization = {',
'\t...defaultEditorLocalization,',
...translationLines,
'}',
];

console.log(tsFileData.join('\n'));
};

void main();

0 comments on commit 96762b4

Please sign in to comment.