Skip to content

[release-4.17] Add translations scripts #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: release-4.17
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions i18n-scripts/export-pos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

set -exuo pipefail

source ./i18n-scripts/languages.sh

for f in locales/en/* ; do
for i in "${LANGUAGES[@]}"
do
yarn i18n-to-po -f "$(basename "$f" .json)" -l "$i"
done
done

112 changes: 112 additions & 0 deletions i18n-scripts/i18n-to-po.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const fs = require('fs');
const path = require('path');
const { i18nextToPo } = require('i18next-conv');
const minimist = require('minimist');
const common = require('./common.js');

function save(target) {
return (result) => {
fs.writeFileSync(target, result);
};
}

function removeValues(i18nFile, filePath) {
const file = require(i18nFile);

const updatedFile = {};

const keys = Object.keys(file);

for (let i = 0; i < keys.length; i++) {
updatedFile[keys[i]] = '';
}

const tmpFile = fs.openSync(filePath, 'w');

fs.writeFileSync(tmpFile, JSON.stringify(updatedFile, null, 2));
}

function consolidateWithExistingTranslations(filePath, fileName, language, packageDir) {
const existingTranslationsPath= `./../locales/${language}/${fileName}.json`;
const englishFile = require(filePath);
const englishKeys = Object.keys(englishFile);
if (fs.existsSync(path.join(__dirname, existingTranslationsPath))) {
const existingTranslationsFile = require(path.join(__dirname, existingTranslationsPath));
const existingKeys = Object.keys(existingTranslationsFile);
const matchingKeys = englishKeys.filter((k) => existingKeys.indexOf(k) > -1);

for (let i = 0; i < matchingKeys.length; i++) {
englishFile[matchingKeys[i]] = existingTranslationsFile[matchingKeys[i]];
}

fs.writeFileSync(filePath, JSON.stringify(englishFile, null, 2));
}
}

function processFile(fileName, language) {
let tmpFile;
const i18nFile = path.join(__dirname, `./../locales/en/${fileName}.json`);

try {
if (fs.existsSync(i18nFile)) {
fs.mkdirSync(path.join(__dirname, './../locales/tmp'), { recursive: true });

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

removeValues(i18nFile, tmpFile);
consolidateWithExistingTranslations(tmpFile, fileName, language);

fs.mkdirSync(path.join(__dirname, `./../po-files/${language}`), { recursive: true });
i18nextToPo(language, fs.readFileSync(tmpFile), {
language,
foldLength: 0,
ctxSeparator: '~',
})
.then(
save(
path.join(
__dirname,
`./../po-files/${language}/public__${path.basename(fileName)}.po`,
),
language,
),
)
.catch((e) => console.error(fileName, e));
}
} catch (err) {
console.error(`Failed to processFile ${fileName}:`, err);
}
common.deleteFile(tmpFile);
console.log(`Processed ${fileName}`);
}

const options = {
string: ['language', 'package', 'file'],
boolean: ['help'],
array: ['files'],
alias: {
h: 'help',
p: 'package',
f: 'files',
l: 'language',
},
default: {
files: [],
},
};

const args = minimist(process.argv.slice(2), options);

if (args.help) {
console.log(
"-h: help\n-l: language (i.e. 'ja')\n\n-f: file name to convert (i.e. 'nav')",
);
} else if (args.files && args.language) {
if (Array.isArray(args.files)) {
for (let i = 0; i < args.files.length; i++) {
processFile(args.files[i], args.language);
}
} else {
processFile(args.files, args.language);
}
}
3 changes: 3 additions & 0 deletions i18n-scripts/languages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

export LANGUAGES=( 'ja' 'zh-cn' 'ko' 'fr' 'es')
59 changes: 59 additions & 0 deletions i18n-scripts/memsource-download.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash

set -exuo pipefail

source ./i18n-scripts/languages.sh

while getopts p: flag
do
case "${flag}" in
p) PROJECT_ID=${OPTARG};;
*) echo "usage: $0 [-p]" >&2
exit 1;;
esac
done

echo "Checking if git workspace is clean"
GIT_STATUS="$(git status --short --untracked-files -- public/locales packages/**/locales)"
if [ -n "$GIT_STATUS" ]; then
echo "There are uncommitted files in public or package locales folders. Remove or commit the files, then run this script again."
git diff
exit 1
fi

echo "Downloading PO files from Project ID \"$PROJECT_ID\""

DOWNLOAD_PATH="$(mktemp -d)" || { echo "Failed to create temp folder"; exit 1; }

# Memsource job listing is limited to 50 jobs per page
# We need to pull all the files down by page and stop when we reach a page with no data
for i in "${LANGUAGES[@]}"
do
COUNTER=0
CURRENT_PAGE=( $(memsource job list --project-id "$PROJECT_ID" --target-lang "$i" -f value --page-number 0 -c uid) )
until [ -z "$CURRENT_PAGE" ]
do
((COUNTER++))
echo Downloading page "$COUNTER"
memsource job download --project-id "$PROJECT_ID" --output-dir "$DOWNLOAD_PATH/$i" --job-id "${CURRENT_PAGE[@]}"
CURRENT_PAGE=$(memsource job list --project-id "$PROJECT_ID" --target-lang "$i" -f value --page-number "$COUNTER" -c uid | tr '\n' ' ')
done
done

echo Importing downloaded PO files into OpenShift
for i in "${LANGUAGES[@]}"
do
# We don't treat zh-cn as a dialect in i18next right now, so we need to alter it to zh
if [ "$i" == 'zh-cn' ]
then
yarn po-to-i18n -d "$DOWNLOAD_PATH/$i" -l 'zh'
else
yarn po-to-i18n -d "$DOWNLOAD_PATH/$i" -l "$i"
fi
done

echo Creating commit
git add locales
git commit -m "chore(i18n): update translations

Adding latest translations from Memsource project https://cloud.memsource.com/web/project2/show/$PROJECT_ID"
38 changes: 38 additions & 0 deletions i18n-scripts/memsource-upload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

set -exuo pipefail

source ./i18n-scripts/languages.sh

while getopts v:s: flag
do
case "${flag}" in
v) VERSION=${OPTARG};;
s) SPRINT=${OPTARG};;
*) echo "usage: $0 [-v] [-s]" >&2
exit 1;;
esac
done

BRANCH=$(git branch --show-current)


echo "Creating project with title \"[OCP $VERSION] UI Localization networking-console-plugin - Sprint $SPRINT/Branch $BRANCH\""

PROJECT_INFO=$(memsource project create --name "[OCP $VERSION] UI Localization networking-console-plugin - Sprint $SPRINT/Branch $BRANCH" --template-id zBOwr4BxYwEq7xlJ37c1F3 -f json)
PROJECT_ID=$(echo "$PROJECT_INFO" | jq -r '.uid')

echo "Exporting PO files"
yarn export-pos
echo "Exported all PO files"

echo "Creating jobs for exported PO files"
for i in "${LANGUAGES[@]}"
do
memsource job create --filenames po-files/"$i"/*.po --target-langs "$i" --project-id "${PROJECT_ID}"
done

echo "Uploaded PO files to Memsource"

# Clean up PO file directory
rm -rf po-files
63 changes: 63 additions & 0 deletions i18n-scripts/po-to-i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const fs = require('fs');
const path = require('path');
const { gettextToI18next } = require('i18next-conv');
const minimist = require('minimist');

function save(target) {
return (result) => {
fs.writeFileSync(target, JSON.stringify(JSON.parse(result), null, 2));
};
}

function processFile(fileName, language) {
if (fileName.includes('.DS_Store')) {
return;
}
let newFilePath;
if (!fs.existsSync(path.join(__dirname, `../locales/${language}/`))) {
fs.mkdirSync(path.join(__dirname, `../locales/${language}/`), { recursive: true });
newFilePath = path.join(__dirname, `../locales/${language}/plugin__networking-console-plugin.json`);
console.log(`Saving /locales/${language}/plugin__networking-console-plugin.json`);
}
gettextToI18next(language, fs.readFileSync(fileName))
.then(save(newFilePath))
.catch((e) => console.error(fileName, e));
}

function processDirectory(directory, language) {
if (fs.existsSync(directory)) {
(async () => {
try {
const files = await fs.promises.readdir(directory);
for (const file of files) {
const filePath = path.join(directory, file);
processFile(filePath, language);
}
} catch (e) {
console.error(`Failed to processDirectory ${directory}:`, e);
}
})();
} else {
console.error('Directory does not exist.');
}
}

const options = {
string: ['language', 'directory'],
boolean: ['help'],
alias: {
h: 'help',
d: 'directory',
l: 'language',
},
};

const args = minimist(process.argv.slice(2), options);

if (args.help) {
console.log(
"-h: help\n-l: language (i.e. 'ja')\n-d: directory to convert files in (i.e. './new-pos')",
);
} else if (args.directory && args.language) {
processDirectory(args.directory, args.language);
}
28 changes: 14 additions & 14 deletions i18next-parser.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ const { CustomJSONLexer } = require('./i18n-scripts/lexers');

// eslint-disable-next-line no-undef
module.exports = {
sort: true,
createOldCatalogs: false,
keySeparator: false,
locales: ['en'],
namespaceSeparator: '~',
reactNamespace: false,
defaultNamespace: 'plugin__networking-console-plugin',
useKeysAsDefaultValue: true,

keySeparator: false,
// see below for more details
lexers: {
hbs: ['HandlebarsLexer'],
default: ['JavascriptLexer'],
handlebars: ['HandlebarsLexer'],

hbs: ['HandlebarsLexer'],
htm: ['HTMLLexer'],
html: ['HTMLLexer'],

mjs: ['JavascriptLexer'],
html: ['HTMLLexer'],
js: ['JavascriptLexer'], // if you're writing jsx inside .js files, change this to JsxLexer
ts: ['JavascriptLexer'],
jsx: ['JsxLexer'],
tsx: ['JsxLexer'],
json: [CustomJSONLexer],
jsx: ['JsxLexer'],
mjs: ['JavascriptLexer'],
ts: ['JavascriptLexer'],

default: ['JavascriptLexer'],
tsx: ['JsxLexer'],
},
locales: ['en', 'es', 'fr', 'ja', 'ko', 'zh'],
namespaceSeparator: '~',
reactNamespace: false,
sort: true,

useKeysAsDefaultValue: true,
};
Loading