Skip to content

Commit

Permalink
another fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Danila-Arg committed Oct 21, 2024
1 parent 1e5f39a commit 949fed3
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions Localizations/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ function initTranslations(companies: Companies): Translations {
return translations;
}

function copyBaseDescriptionToAllLang(translations: TranslationResult, description: string) {
function copyBaseDescriptionToAllLang(translations: TranslationResult, description: string) : TranslationResult {

Check failure on line 66 in Localizations/translate.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 113. Maximum allowed is 100
const newtranslations = {...translations};

Check failure on line 67 in Localizations/translate.ts

View workflow job for this annotation

GitHub Actions / lint

A space is required after '{'

Check failure on line 67 in Localizations/translate.ts

View workflow job for this annotation

GitHub Actions / lint

A space is required before '}'
for (const lang of languages) {
translations[lang] = description;

Check failure on line 69 in Localizations/translate.ts

View workflow job for this annotation

GitHub Actions / lint

Assignment to property of function parameter 'translations'
}

return newtranslations;
}

function isStringNullOrEmpty(value: string | null | undefined) : boolean {
Expand All @@ -79,7 +82,7 @@ function isStringNumeric(value: string | null | undefined) : boolean {

function isDescriptionNeedToTranslate(value: string | null | undefined) : boolean {
return !isStringNullOrEmpty(value)
&& !isStringNumeric(value)
&& !isStringNumeric(value);
}

function removeObsoleteCompanyDescriptions(translations: Translations, companies: Companies) {
Expand All @@ -96,6 +99,35 @@ function removeObsoleteCompanyDescriptions(translations: Translations, companies
}
}

async function generateTranslations(
companyTranslations: TranslationResult,
baseDescriptionChanged: boolean,
companyId: string,
newDescription: string)

Check failure on line 106 in Localizations/translate.ts

View workflow job for this annotation

GitHub Actions / lint

Expected newline before ')'

Check failure on line 106 in Localizations/translate.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
: Promise<number> {
let translationsCount = 0;
// If there are no translations for this company, generate them.
for (const lang of languages) {
// Only translate it if there's a language missing or if the
// base description changed.
if (!baseDescriptionChanged && companyTranslations[lang] !== undefined) {
consola.info(`Language description ${companyTranslations[lang]} not changed`);
continue;

Check failure on line 115 in Localizations/translate.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected use of continue statement
}

try {
consola.debug(`Translating ${companyId} description to ${lang}`);
const translatedText = await translateContent(newDescription, lang);
companyTranslations[lang] = translatedText;

Check failure on line 121 in Localizations/translate.ts

View workflow job for this annotation

GitHub Actions / lint

Assignment to property of function parameter 'companyTranslations'
translationsCount += 1;
} catch (ex) {
consola.error(`Failed to translate ${companyId} description to ${lang}`, ex);
}
}

return translationsCount;
}

async function translateCompanyDescriptions(translations: Translations, companies: Companies) {
const syncedTranslations = translations;
let translatedCompaniesCount = 0;
Expand All @@ -108,20 +140,17 @@ async function translateCompanyDescriptions(translations: Translations, companie
for (const companyId in companies) {
const company = companies[companyId];
const newDescription = company.description;
const companyTranslations = syncedTranslations[companyId] ?? {
let companyTranslations = syncedTranslations[companyId] ?? {
[defaultLanguage]: newDescription,
};
const baseDescriptionChanged = companyTranslations[defaultLanguage] !== newDescription;

// Update the base language now.
companyTranslations[defaultLanguage] = newDescription;
if (!isDescriptionNeedToTranslate(company.description)) {
copyBaseDescriptionToAllLang(companyTranslations, company.description);
}
else {
translationsCount += await generateTranslations(
companyTranslations, baseDescriptionChanged, companyId, newDescription);

companyTranslations = copyBaseDescriptionToAllLang(companyTranslations, company.description);

Check failure on line 151 in Localizations/translate.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 105. Maximum allowed is 100
} else {
translationsCount += await generateTranslations(companyTranslations, baseDescriptionChanged, companyId, newDescription);

Check failure on line 153 in Localizations/translate.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 132. Maximum allowed is 100
}

// Signals that there were changes in company translations.
Expand All @@ -145,36 +174,7 @@ async function translateCompanyDescriptions(translations: Translations, companie

async function syncTranslations(translations: Translations, companies: Companies) {
removeObsoleteCompanyDescriptions(translations, companies);
await translateCompanyDescriptions(translations, companies)
}

async function generateTranslations(
companyTranslations: TranslationResult,
baseDescriptionChanged: boolean,
companyId: string,
newDescription: string) : Promise<number> {

let translationsCount = 0;
// If there are no translations for this company, generate them.
for (const lang of languages) {
// Only translate it if there's a language missing or if the
// base description changed.
if (!baseDescriptionChanged && companyTranslations[lang] !== undefined) {
consola.info(`Language description ${companyTranslations[lang]} not changed`);
continue;
}

try {
consola.debug(`Translating ${companyId} description to ${lang}`);
const translatedText = await translateContent(newDescription, lang);
companyTranslations[lang] = translatedText;
translationsCount += 1;
} catch (ex) {
consola.error(`Failed to translate ${companyId} description to ${lang}`, ex);
}
}

return translationsCount;
await translateCompanyDescriptions(translations, companies);
}

async function main() {
Expand Down

0 comments on commit 949fed3

Please sign in to comment.