Skip to content

Commit

Permalink
Merge pull request #11 from tzdesign/fix/en.json
Browse files Browse the repository at this point in the history
Fix/en.json
  • Loading branch information
tzdesign authored Jul 14, 2024
2 parents 2891ddd + aaf23c5 commit ca64dc1
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 124 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"scripts": {
"build": "tsc",
"test:translate": "tsc && node dist/index.js translate -b src/test/en-US.json -l de-DE de-CH fr-FR sk-SK -i de-DE",
"test:translate": "tsc && node dist/index.js translate -b src/test/en-US.json -l de-DE de-CH fr-FR sk-SK en_US -i de-DE ",
"test:translate:withprefix": "tsc && node dist/index.js translate -b src/test/message-en.json -l de-DE de-CH fr-FR sk-SK -i de-DE",
"test:stale": "tsc && node dist/index.js remove-stale -b src/test/en-US.json -l de-DE de-CH sk-SK fr-FR",
"test:stale:withprefix": "tsc && node dist/index.js remove-stale -b src/test/message-en.json -l de-DE de-CH sk-SK fr-FR",
Expand Down
247 changes: 124 additions & 123 deletions src/translate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ export default async function translate(this: Command) {
console.log(`\tOutput: ${outputDir(options.base ?? "", options.output)}`);
console.log(`\tLocales: ${options.locales.join(", ")}`);
console.log(
`\tLocales informal: ${
options.informalLocales.length === 0
? "none"
: options.informalLocales.join(", ")
`\tLocales informal: ${options.informalLocales.length === 0
? "none"
: options.informalLocales.join(", ")
}`
);
console.log("");
Expand Down Expand Up @@ -50,143 +49,145 @@ export default async function translate(this: Command) {
throw new Error("Could not find source language.");

for (const targetLocaleString of options.locales) {
const isInformal = options.informalLocales?.includes(targetLocaleString);

const targetLocale = new Intl.Locale(targetLocaleString);
const targetLanguage =
targetLanguages.find(s => s.code === targetLocale.baseName) ??
targetLanguages.find(s => s.code === targetLocale.language);

if (targetLanguage === undefined) {
console.error(
`Could not find target language for ${
targetLocaleString.green
}.\nSee supported target languages are ${
targetLanguages.map(t => t.code).join(", ").yellow
}`
);
continue;
}
try {
const isInformal = options.informalLocales?.includes(targetLocaleString);
const guessedTarget = targetLocaleString === 'en' ? 'en-US' : targetLocaleString.replace(/_/g, "-");
const targetLocale = new Intl.Locale(guessedTarget);
const targetLanguage =
targetLanguages.find(s => s.code === targetLocale.baseName) ??
targetLanguages.find(s => s.code === targetLocale.language);

if (targetLanguage === undefined) {
console.error(
`Could not find target language for ${targetLocaleString.green
}.\nSee supported target languages are ${targetLanguages.map(t => t.code).join(", ").yellow
}`
);
continue;
}

const languageName = `${targetLanguage.name} ${
isInformal ? "(informal)" : ""
}`;

console.log(`Start translation for ${languageName.green}`);

const filename = outputFilename(targetLocaleString, {
base: options.base,
output: options.output,
});

if (fs.existsSync(filename) === false) {
fs.writeFileSync(
filename,
JSON.stringify(
{ locale: targetLocaleString, translations: {} },
null,
2
)
);
}
const languageName = `${targetLanguage.name} ${isInformal ? "(informal)" : ""
}`;

const targetTranslationSafe = localizeFileSchema.safeParse(
JSON.parse(fs.readFileSync(filename).toString())
);
console.log(`Start translation for ${languageName.green} (${targetLocaleString})`);

if (targetTranslationSafe.success === false) {
console.error(
`Could not parse ${targetLocaleString}.json make sure there is no merge conflict inside the file. Will continue with next locale.`
.red,
targetTranslationSafe.error
);
continue;
}
const filename = outputFilename(targetLocaleString, {
base: options.base,
output: options.output,
});

let targetTranslation = targetTranslationSafe.data;
const progress = new cliProgress.SingleBar(
{},
cliProgress.Presets.shades_classic
);
if (fs.existsSync(filename) === false) {
fs.writeFileSync(
filename,
JSON.stringify(
{ locale: targetLocaleString, translations: {} },
null,
2
)
);
}

const filteredTranslations = Object.entries(translations).filter(
([key]) => {
const translation = targetTranslation.translations[key];
const targetTranslationSafe = localizeFileSchema.safeParse(
JSON.parse(fs.readFileSync(filename).toString())
);

return (
translation === undefined ||
(typeof translation === "string" && translation.trim() === "")
if (targetTranslationSafe.success === false) {
console.error(
`Could not parse ${targetLocaleString}.json make sure there is no merge conflict inside the file. Will continue with next locale.`
.red,
targetTranslationSafe.error
);
continue;
}
);

if (filteredTranslations.length === 0) {
console.log(
`\tNo new translations for ${languageName} found. Will skip.\n`.green
let targetTranslation = targetTranslationSafe.data;
const progress = new cliProgress.SingleBar(
{},
cliProgress.Presets.shades_classic
);
continue;
}

progress.start(filteredTranslations.length, 0);
for (let [key, sourceText] of filteredTranslations) {
if (options.compiledI18n && Boolean(sourceText) === false) {
sourceText = key;
}
progress.increment();
const metaKey =
targetLocale.language +
(options.informalLocales?.includes(targetLocaleString)
? "-informal"
: "");

if (targetTranslation.translations[key]) continue;
if (meta[metaKey]?.[JSON.stringify(sourceText)]) {
targetTranslation.translations[key] =
meta[metaKey][JSON.stringify(sourceText)];
const filteredTranslations = Object.entries(translations).filter(
([key]) => {
const translation = targetTranslation.translations[key];

return (
translation === undefined ||
(typeof translation === "string" && translation.trim() === "")
);
}
);

if (filteredTranslations.length === 0) {
console.log(
`\tNo new translations for ${languageName} found. Will skip.\n`.green
);
continue;
}

meta[metaKey] = meta[metaKey] ?? {};

const object =
typeof sourceText === "string" ? { flat: sourceText } : sourceText;

for (const [plural, source] of Object.entries(object)) {
const result = await translator.translateText(
prepareVariables(source),
sourceLanguage.code as deepl.SourceLanguageCode,
targetLanguage.code as deepl.TargetLanguageCode,
{
formality:
options.informalLocales?.includes(targetLocaleString) &&
targetLanguage.supportsFormality
? "less"
: undefined,
tagHandling: "xml",
ignoreTags: ["x"],
}
);
if (plural === "flat") {
targetTranslation.translations[key] = cleanVariables(result.text);
} else {
if (
targetTranslation.translations[key] === undefined ||
targetTranslation.translations[key] === ""
) {
targetTranslation.translations[key] = {};
}
(targetTranslation.translations[key] as Record<string, string>)[
plural.toString()
] = cleanVariables(result.text);
progress.start(filteredTranslations.length, 0);
for (let [key, sourceText] of filteredTranslations) {
if (options.compiledI18n && Boolean(sourceText) === false) {
sourceText = key;
}
progress.increment();
const metaKey =
targetLocale.language +
(options.informalLocales?.includes(targetLocaleString)
? "-informal"
: "");

if (targetTranslation.translations[key]) continue;
if (meta[metaKey]?.[JSON.stringify(sourceText)]) {
targetTranslation.translations[key] =
meta[metaKey][JSON.stringify(sourceText)];
continue;
}

meta[metaKey] = meta[metaKey] ?? {};

const object =
typeof sourceText === "string" ? { flat: sourceText } : sourceText;

for (const [plural, source] of Object.entries(object)) {
const result = await translator.translateText(
prepareVariables(source),
sourceLanguage.code as deepl.SourceLanguageCode,
targetLanguage.code as deepl.TargetLanguageCode,
{
formality:
options.informalLocales?.includes(targetLocaleString) &&
targetLanguage.supportsFormality
? "less"
: undefined,
tagHandling: "xml",
ignoreTags: ["x"],
}
);
if (plural === "flat") {
targetTranslation.translations[key] = cleanVariables(result.text);
} else {
if (
targetTranslation.translations[key] === undefined ||
targetTranslation.translations[key] === ""
) {
targetTranslation.translations[key] = {};
}
(targetTranslation.translations[key] as Record<string, string>)[
plural.toString()
] = cleanVariables(result.text);
}

meta[metaKey][source] = cleanVariables(result.text);
meta[metaKey][source] = cleanVariables(result.text);
}
}
}
progress.stop();
console.log("");

fs.writeFileSync(filename, JSON.stringify(targetTranslation, null, 2));
progress.stop();
console.log("");

fs.writeFileSync(filename, JSON.stringify(targetTranslation, null, 2));
} catch (error) {
console.error(targetLocaleString, error);
}
}

writeMeta(meta, outputDir(options.base ?? "", options.output));
Expand Down

0 comments on commit ca64dc1

Please sign in to comment.