@adminforth/i18n
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -311,2 +311,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
// returns translated count | ||
bulkTranslate(_a) { | ||
@@ -335,15 +336,20 @@ return __awaiter(this, arguments, void 0, function* ({ selectedIds }) { | ||
const updateStrings = {}; | ||
const translateToLang = (langIsoCode, strings) => __awaiter(this, void 0, void 0, function* () { | ||
const translateToLang = (langIsoCode_1, strings_1, ...args_1) => __awaiter(this, [langIsoCode_1, strings_1, ...args_1], void 0, function* (langIsoCode, strings, plurals = false) { | ||
if (strings.length === 0) { | ||
return 0; | ||
} | ||
if (strings.length > maxKeysInOneReq) { | ||
let totalTranslated = 0; | ||
for (let i = 0; i < strings.length; i += maxKeysInOneReq) { | ||
const slicedStrings = strings.slice(i, i + maxKeysInOneReq); | ||
yield translateToLang(langIsoCode, slicedStrings); | ||
console.log('🪲🔪slicedStrings ', slicedStrings); | ||
totalTranslated += yield translateToLang(langIsoCode, slicedStrings, plurals); | ||
} | ||
return; | ||
return totalTranslated; | ||
} | ||
const lang = langIsoCode; | ||
const isSlavikPlural = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang); | ||
const requestSlavicPlurals = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang) && plurals; | ||
const prompt = ` | ||
I need to translate strings in JSON to ${lang} language from English for my web app. | ||
${isSlavikPlural ? `If string contains '|' it means it is plural form, you should provide 4 translations (zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''} | ||
${requestSlavicPlurals ? `You should provide 4 translations (in format zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''} | ||
Keep keys, as is, write translation into values! Here are the strings: | ||
@@ -383,7 +389,8 @@ | ||
for (const translation of translationsTargeted) { | ||
translation[this.trFieldNames[lang]] = translatedStr; | ||
//translation[this.trFieldNames[lang]] = translatedStr; | ||
// process.env.HEAVY_DEBUG && console.log(`🪲translated to ${lang} ${translation.en_string}, ${translatedStr}`) | ||
if (!updateStrings[enStr]) { | ||
updateStrings[enStr] = { | ||
if (!updateStrings[translation[this.primaryKeyFieldName]]) { | ||
updateStrings[translation[this.primaryKeyFieldName]] = { | ||
updates: {}, | ||
translatedStr, | ||
category: translation[this.options.categoryFieldName], | ||
@@ -393,9 +400,15 @@ strId: translation[this.primaryKeyFieldName], | ||
} | ||
updateStrings[enStr].updates[this.trFieldNames[lang]] = translatedStr; | ||
updateStrings[translation[this.primaryKeyFieldName]].updates[this.trFieldNames[lang]] = translatedStr; | ||
} | ||
} | ||
return res.length; | ||
}); | ||
const langsInvolved = new Set(Object.keys(needToTranslateByLang)); | ||
let totalTranslated = 0; | ||
yield Promise.all(Object.entries(needToTranslateByLang).map((_b) => __awaiter(this, [_b], void 0, function* ([lang, strings]) { | ||
yield translateToLang(lang, strings); | ||
// first translate without plurals | ||
const stringsWithoutPlurals = strings.filter(s => !s.en_string.includes('|')); | ||
totalTranslated += yield translateToLang(lang, stringsWithoutPlurals, false); | ||
const stringsWithPlurals = strings.filter(s => s.en_string.includes('|')); | ||
totalTranslated += yield translateToLang(lang, stringsWithPlurals, true); | ||
}))); | ||
@@ -413,2 +426,3 @@ yield Promise.all(Object.entries(updateStrings).map((_c) => __awaiter(this, [_c], void 0, function* ([_, { updates, strId }]) { | ||
} | ||
return totalTranslated; | ||
}); | ||
@@ -415,0 +429,0 @@ } |
51
index.ts
@@ -350,3 +350,4 @@ import AdminForth, { AdminForthPlugin, Filters, suggestIfTypo, AdminForthDataTypes } from "adminforth"; | ||
async bulkTranslate({ selectedIds }: { selectedIds: string[] }) { | ||
// returns translated count | ||
async bulkTranslate({ selectedIds }: { selectedIds: string[] }): Promise<number> { | ||
@@ -386,22 +387,29 @@ const needToTranslateByLang : Partial< | ||
const updateStrings: Record<string, { | ||
updates: any, category: string, strId: string | ||
updates: any, | ||
category: string, | ||
strId: string, | ||
translatedStr: string | ||
}> = {}; | ||
const translateToLang = async (langIsoCode: LanguageCode, strings: { en_string: string, category: string }[]) => { | ||
const translateToLang = async (langIsoCode: LanguageCode, strings: { en_string: string, category: string }[], plurals=false): Promise<number> => { | ||
if (strings.length === 0) { | ||
return 0; | ||
} | ||
if (strings.length > maxKeysInOneReq) { | ||
let totalTranslated = 0; | ||
for (let i = 0; i < strings.length; i += maxKeysInOneReq) { | ||
const slicedStrings = strings.slice(i, i + maxKeysInOneReq); | ||
await translateToLang(langIsoCode, slicedStrings); | ||
console.log('🪲🔪slicedStrings ', slicedStrings); | ||
totalTranslated += await translateToLang(langIsoCode, slicedStrings, plurals); | ||
} | ||
return; | ||
return totalTranslated; | ||
} | ||
const lang = langIsoCode; | ||
const isSlavikPlural = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang); | ||
const requestSlavicPlurals = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang) && plurals; | ||
const prompt = ` | ||
I need to translate strings in JSON to ${lang} language from English for my web app. | ||
${isSlavikPlural ? `If string contains '|' it means it is plural form, you should provide 4 translations (zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''} | ||
${requestSlavicPlurals ? `You should provide 4 translations (in format zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''} | ||
Keep keys, as is, write translation into values! Here are the strings: | ||
@@ -447,11 +455,15 @@ | ||
res = JSON.parse(res); | ||
for (const [enStr, translatedStr] of Object.entries(res)) { | ||
for (const [enStr, translatedStr] of Object.entries(res) as [string, string][]) { | ||
const translationsTargeted = translations.filter(t => t[this.enFieldName] === enStr); | ||
// might be several with same en_string | ||
for (const translation of translationsTargeted) { | ||
translation[this.trFieldNames[lang]] = translatedStr; | ||
//translation[this.trFieldNames[lang]] = translatedStr; | ||
// process.env.HEAVY_DEBUG && console.log(`🪲translated to ${lang} ${translation.en_string}, ${translatedStr}`) | ||
if (!updateStrings[enStr]) { | ||
updateStrings[enStr] = { | ||
if (!updateStrings[translation[this.primaryKeyFieldName]]) { | ||
updateStrings[translation[this.primaryKeyFieldName]] = { | ||
updates: {}, | ||
translatedStr, | ||
category: translation[this.options.categoryFieldName], | ||
@@ -461,6 +473,9 @@ strId: translation[this.primaryKeyFieldName], | ||
} | ||
updateStrings[enStr].updates[this.trFieldNames[lang]] = translatedStr; | ||
updateStrings[ | ||
translation[this.primaryKeyFieldName] | ||
].updates[this.trFieldNames[lang]] = translatedStr; | ||
} | ||
} | ||
return res.length; | ||
} | ||
@@ -470,4 +485,10 @@ | ||
let totalTranslated = 0; | ||
await Promise.all(Object.entries(needToTranslateByLang).map(async ([lang, strings]: [LanguageCode, { en_string: string, category: string }[]]) => { | ||
await translateToLang(lang, strings); | ||
// first translate without plurals | ||
const stringsWithoutPlurals = strings.filter(s => !s.en_string.includes('|')); | ||
totalTranslated += await translateToLang(lang, stringsWithoutPlurals, false); | ||
const stringsWithPlurals = strings.filter(s => s.en_string.includes('|')); | ||
totalTranslated += await translateToLang(lang, stringsWithPlurals, true); | ||
})); | ||
@@ -496,2 +517,4 @@ | ||
return totalTranslated; | ||
} | ||
@@ -498,0 +521,0 @@ |
{ | ||
"name": "@adminforth/i18n", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5470522
581
5082