Skip to content

Commit

Permalink
Added locale sync
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Sep 27, 2023
1 parent 43eb5a7 commit 2d4848b
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 8 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/release-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ jobs:
registry-url: https://registry.npmjs.org/

- name: Install the dependencies
run: npm i
run: npm ci

- name: Prepare BETA
run: node scripts/beta-release.js $GITHUB_RUN_ID

- name: Run localization sync
run: npm run localization:sync
env:
TRANSLATION_API_KEY: ${{ secrets.TRANSLATION_API_KEY }}
TRANSLATION_API_LOCATION: ${{ secrets.TRANSLATION_API_LOCATION }}
TRANSLATION_API_URL: ${{ secrets.TRANSLATION_API_URL }}

- name: Publish
run: npx @vscode/vsce publish -p ${{ secrets.VSCE_PAT }} --baseImagesUrl https://raw.githubusercontent.com/estruyf/vscode-front-matter/dev

Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ jobs:
registry-url: https://registry.npmjs.org/

- name: Install the dependencies
run: npm i
run: npm ci

- name: Prepare MAIN release
run: node scripts/main-release.js

- name: Run localization sync
run: npm run localization:sync
env:
TRANSLATION_API_KEY: ${{ secrets.TRANSLATION_API_KEY }}
TRANSLATION_API_LOCATION: ${{ secrets.TRANSLATION_API_LOCATION }}
TRANSLATION_API_URL: ${{ secrets.TRANSLATION_API_URL }}

- name: Publish
run: npx @vscode/vsce publish -p ${{ secrets.VSCE_PAT }}

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ e2e/extensions
e2e/sample

localization.log
localization.md
localization.md
.env
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2558,6 +2558,7 @@
"cheerio": "1.0.0-rc.12",
"css-loader": "5.2.7",
"date-fns": "2.23.0",
"dotenv": "^16.3.1",
"downshift": "6.0.6",
"eslint": "^8.33.0",
"fuse.js": "6.5.3",
Expand Down
122 changes: 117 additions & 5 deletions scripts/sync-localization.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const { v4: uuidv4 } = require('uuid');
require('dotenv').config()

const transKey = process.env.TRANSLATION_API_KEY || "";
const apiUrl = process.env.TRANSLATION_API_URL || "";
const location = process.env.TRANSLATION_API_LOCATION || "";

const getTranslation = (translation) => {
let value = undefined;

if (translation && translation.translations && translation.translations.length > 0) {
value = translation.translations[0].text;
}

return value;
}

const callTranslationService = async (body, locale) => {
const response = await fetch(`${apiUrl}/translate?api-version=3.0&from=en&to=${locale}`, {
method: 'POST',
headers: {
'Ocp-Apim-Subscription-Key': transKey,
'Ocp-Apim-Subscription-Region': location,
'Content-type': 'application/json',
'Accept': 'application/json',
'X-ClientTraceId': uuidv4().toString(),
},
body
});

if (!response.ok) {
return undefined;
}

return await response.json();
}

(async () => {
// Get all the files from the l10n directory
Expand All @@ -11,6 +47,7 @@ const glob = require('glob');
const enContent = JSON.parse(enFile);
const enKeys = Object.keys(enContent);

console.log(`Starting l10n bundles`);
for (const file of files) {
if (file.endsWith(`bundle.l10n.json`)) {
continue;
Expand All @@ -20,17 +57,53 @@ const glob = require('glob');
const fileContent = fs.readFileSync(path.join(__dirname, `../l10n/${file}`), 'utf8');
let content = {};

// Get the locale
const fileName = path.basename(file);
const fileSplit = fileName.split('.');
const locale = fileSplit[fileSplit.length - 2];
if (!locale) {
continue;
}
console.log(`- Processing: ${locale}`);

try {
content = JSON.parse(fileContent);
} catch (e) {
// Ignore the error
}

const keysToTranslate = [];

// Loop through the EN keys
for (const key of enKeys) {
// If the key does not exist in the file, add it
if (!content[key]) {
content[key] = `${enContent[key]}`;
if (!content[key] || content[key].startsWith(`🚧: `)) {
keysToTranslate.push({
name: key,
value: enContent[key],
});

if (!apiUrl || !transKey || !location) {
content[key] = `${enContent[key]}`;
}
}
}

if (apiUrl && transKey && location) {
if (keysToTranslate.length > 0) {
const body = JSON.stringify(keysToTranslate.map(key => ({ text: key.value })));
const data = await callTranslationService(body, locale);

for (let i = 0; i < keysToTranslate.length; i++) {
const keyToTranslate = keysToTranslate[i];
const translation = getTranslation(data[i]);

if (keyToTranslate.name && translation) {
content[keyToTranslate.name] = translation;
} else {
content[keyToTranslate.name] = `${keyToTranslate.value}`;
}
}
}
}

Expand All @@ -46,21 +119,60 @@ const glob = require('glob');

const pkgFiles = glob.sync(path.join(__dirname, '../package.nls.*.json'));


console.log(``);
console.log(`Starting nls bundles`);
for (const file of pkgFiles) {
const fileContent = fs.readFileSync(file, 'utf8');
let content = {};

// Get the locale
const fileName = path.basename(file);
const fileSplit = fileName.split('.');
const locale = fileSplit[fileSplit.length - 2];
if (!locale) {
continue;
}
console.log(`- Processing: ${locale}`);

try {
content = JSON.parse(fileContent);
} catch (e) {
// Ignore the error
}

const keysToTranslate = [];

// Loop through the EN keys
for (const key of enPkgKeys) {
// If the key does not exist in the file, add it
if (!content[key]) {
content[key] = `🚧: ${enPkgContent[key]}`;
const contentValue = content[key];
if (!contentValue || contentValue.startsWith(`🚧: `)) {
keysToTranslate.push({
name: key,
value: enPkgContent[key],
});

if (!apiUrl || !transKey || !location) {
content[key] = `🚧: ${enPkgContent[key]}`;
}
}
}

if (apiUrl && transKey && location) {
if (keysToTranslate.length > 0) {
const body = JSON.stringify(keysToTranslate.map(key => ({ text: key.value })));
const data = await callTranslationService(body, locale);

for (let i = 0; i < keysToTranslate.length; i++) {
const keyToTranslate = keysToTranslate[i];
const translation = getTranslation(data[i]);

if (keyToTranslate.name && translation) {
content[keyToTranslate.name] = translation;
} else {
content[keyToTranslate.name] = `${keyToTranslate.value}`;
}
}
}
}

Expand Down

0 comments on commit 2d4848b

Please sign in to comment.