Skip to content

Commit 01e5bc4

Browse files
committed
Fix i18n scripts
1 parent 21445e8 commit 01e5bc4

File tree

6 files changed

+69
-46
lines changed

6 files changed

+69
-46
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"format-test": "yarn prettier -c .",
3737
"i18n": "i18next \"packages/**/*.{ts,tsx}\" [-oc] -c i18next-parser.config.js",
3838
"i18n-test": "yarn i18n --fail-on-update",
39-
"i18n-to-po": "node ./scripts/i18n/i18n-to-po.js",
39+
"i18n-to-po": "node ./scripts/i18n/i18n-to-po.mjs",
4040
"lint": "yarn lint-ts && yarn lint-css",
4141
"lint-css": "yarn stylelint packages/**/**/*.scss",
4242
"lint-css-fix": "yarn stylelint packages/**/**/*.scss --fix",
@@ -46,7 +46,7 @@
4646
"memsource-download": "./scripts/i18n/memsource-download.sh",
4747
"memsource-upload": "./scripts/i18n/memsource-upload.sh",
4848
"ocp-console": "./scripts/start-ocp-console.sh",
49-
"po-to-i18n": "node ./scripts/i18n/po-to-i18n.js",
49+
"po-to-i18n": "node ./scripts/i18n/po-to-i18n.mjs",
5050
"prepare": "husky",
5151
"serve-local-build": "./http-server.sh ./plugins/odf/dist",
5252
"serve-local-build-mco": "./http-server.sh ./plugins/mco/dist",
@@ -76,8 +76,8 @@
7676
"eslint-plugin-import/tsconfig-paths/json5": "^1.0.2",
7777
"loader-utils": "^2.0.4",
7878
"micromatch": "^4.0.8",
79-
"minimist": "^1.2.8",
8079
"minimatch": "^3.0.5",
80+
"minimist": "^1.2.8",
8181
"nanoid": "^3.3.8",
8282
"postcss": "^8.4.49",
8383
"tough-cookie": "^4.1.3",

scripts/i18n/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This i18n directory contains all the required scripts for i18n automation. Also,
1010

1111
4. Upload translations: you need to create a new project using your project's template. Each template will have its own unique id and an owner (example: for odf-console, name of our template is "OCP ODF UI" and id is "193065").
1212
Run `yarn memsource-upload -v <VERSION> -s <SPRINT>`.
13-
This script will create a new project for you and will call `export-pos.sh` and `i18n-to-po.js` which will export all i18next `.json` files in `.po` format in all the languages we currently support, and will upload these `.po` files under newly created project on Memsource.
13+
This script will create a new project for you and will call `export-pos.sh` and `i18n-to-po.mjs` which will export all i18next `.json` files in `.po` format in all the languages we currently support, and will upload these `.po` files under newly created project on Memsource.
1414

1515
5. Download translations: for downloading we download all files in `.po` format and then convert into `.json`.
1616
Run `yarn memsource-download -p <PROJECT_ID>` (example: from the Memsource project URL "https://cloud.memsource.com/web/project2/show/FBfZeTEWPYaC4VXhgrW0R2" the series of numbers and letters after /show/).

scripts/i18n/common.js

-19
This file was deleted.

scripts/i18n/common.mjs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
export function deleteFile(filePath) {
5+
try {
6+
fs.unlinkSync(filePath);
7+
} catch (e) {
8+
console.error(`Failed to delete file ${filePath}:`, e);
9+
}
10+
}
11+
12+
export function deleteDir(dirPath) {
13+
try {
14+
fs.rmSync(dirPath, { recursive: true, force: true });
15+
} catch (e) {
16+
console.error(`Failed to delete directory ${dirPath}:`, e);
17+
}
18+
}

scripts/i18n/i18n-to-po.js renamed to scripts/i18n/i18n-to-po.mjs

+38-19
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
const fs = require('fs');
2-
const path = require('path');
3-
const { i18nextToPo } = require('i18next-conv');
4-
const minimist = require('minimist');
5-
const common = require('./common.js');
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { fileURLToPath } from 'url';
4+
import { i18nextToPo } from 'i18next-conv';
5+
import minimist from 'minimist';
6+
import * as common from './common.mjs';
7+
8+
// __dirname is not defined by default in ES module scope
9+
const __filename = fileURLToPath(import.meta.url);
10+
const __dirname = path.dirname(__filename);
611

712
function save(target) {
813
return (result) => {
914
fs.writeFileSync(target, result);
1015
};
1116
}
1217

13-
function removeValues(i18nFile, filePath) {
14-
const file = require(i18nFile);
18+
async function removeValues(i18nFile, filePath) {
19+
const file = await import(i18nFile, { assert: { type: 'json' } }).then(
20+
(m) => m.default
21+
);
1522

1623
const updatedFile = {};
1724

@@ -26,14 +33,26 @@ function removeValues(i18nFile, filePath) {
2633
fs.writeFileSync(tmpFile, JSON.stringify(updatedFile, null, 2));
2734
}
2835

29-
function consolidateWithExistingTranslations(filePath, fileName, language) {
30-
const englishFile = require(filePath);
36+
async function consolidateWithExistingTranslations(
37+
filePath,
38+
fileName,
39+
language
40+
) {
41+
const englishFile = await import(filePath, { assert: { type: 'json' } }).then(
42+
(m) => m.default
43+
);
3144
const englishKeys = Object.keys(englishFile);
32-
let existingTranslationsPath = `../../locales/${language}/${fileName}.json`;
33-
if (fs.existsSync(path.join(__dirname, existingTranslationsPath))) {
34-
const existingTranslationsFile = require(
35-
path.join(__dirname, existingTranslationsPath)
36-
);
45+
const existingTranslationsPath = `../../locales/${language}/${fileName}.json`;
46+
const existingTranslationsFullPath = path.join(
47+
__dirname,
48+
existingTranslationsPath
49+
);
50+
51+
if (fs.existsSync(existingTranslationsFullPath)) {
52+
const existingTranslationsFile = await import(
53+
existingTranslationsFullPath,
54+
{ assert: { type: 'json' } }
55+
).then((m) => m.default);
3756
const existingKeys = Object.keys(existingTranslationsFile);
3857
const matchingKeys = englishKeys.filter(
3958
(k) => existingKeys.indexOf(k) > -1
@@ -47,7 +66,7 @@ function consolidateWithExistingTranslations(filePath, fileName, language) {
4766
}
4867
}
4968

50-
function processFile(fileName, language) {
69+
async function processFile(fileName, language) {
5170
let tmpFile;
5271
let dirPath;
5372
const i18nFile = path.join(__dirname, `../../locales/en/${fileName}.json`);
@@ -62,8 +81,8 @@ function processFile(fileName, language) {
6281

6382
tmpFile = path.join(__dirname, `../../locales/tmp/${fileName}.json`);
6483

65-
removeValues(i18nFile, tmpFile);
66-
consolidateWithExistingTranslations(tmpFile, fileName, language);
84+
await removeValues(i18nFile, tmpFile);
85+
await consolidateWithExistingTranslations(tmpFile, fileName, language);
6786

6887
fs.mkdirSync(path.join(__dirname, `../../po-files/${language}`), {
6988
recursive: true,
@@ -116,9 +135,9 @@ if (args.help) {
116135
} else if (args.files && args.language) {
117136
if (Array.isArray(args.files)) {
118137
for (let i = 0; i < args.files.length; i++) {
119-
processFile(args.files[i], args.language);
138+
await processFile(args.files[i], args.language);
120139
}
121140
} else {
122-
processFile(args.files, args.language);
141+
await processFile(args.files, args.language);
123142
}
124143
}

scripts/i18n/po-to-i18n.js renamed to scripts/i18n/po-to-i18n.mjs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
const fs = require('fs');
2-
const path = require('path');
3-
const { gettextToI18next } = require('i18next-conv');
4-
const minimist = require('minimist');
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { fileURLToPath } from 'url';
4+
import { gettextToI18next } from 'i18next-conv';
5+
import minimist from 'minimist';
6+
7+
// __dirname is not defined by default in ES module scope
8+
const __filename = fileURLToPath(import.meta.url);
9+
const __dirname = path.dirname(__filename);
510

611
function save(target) {
712
return (result) => {

0 commit comments

Comments
 (0)