@adminforth/i18n
Advanced tools
Comparing version 1.0.20-next.0 to 1.0.20
@@ -8,3 +8,7 @@ # Changelog | ||
## [1.0.20] | ||
### Fixed | ||
- fix automatic translations | ||
## [1.0.14] | ||
@@ -11,0 +15,0 @@ |
@@ -55,2 +55,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
function ensureTemplateHasAllParams(template, newTemplate) { | ||
// string ensureTemplateHasAllParams("a {b} c {d}", "я {b} і {d} в") // true | ||
// string ensureTemplateHasAllParams("a {b} c {d}", "я і {d} в") // false | ||
// string ensureTemplateHasAllParams("a {b} c {d}", "я {bb} і {d} в") // false | ||
const existingParams = template.match(/{[^}]+}/g); | ||
const newParams = newTemplate.match(/{[^}]+}/g); | ||
const existingParamsSet = new Set(existingParams); | ||
const newParamsSet = new Set(newParams); | ||
return existingParamsSet.size === newParamsSet.size && [...existingParamsSet].every(p => newParamsSet.has(p)); | ||
} | ||
class AiTranslateError extends Error { | ||
@@ -193,4 +203,19 @@ constructor(message) { | ||
resourceConfig.options.allowedActions.create = false; | ||
// add hook to validate user did not screw up with template params | ||
resourceConfig.hooks.edit.beforeSave.push((_c) => __awaiter(this, [_c], void 0, function* ({ updates, oldRecord }) { | ||
for (const lang of this.options.supportedLanguages) { | ||
if (lang === 'en') { | ||
continue; | ||
} | ||
if (updates[this.trFieldNames[lang]]) { // if user set '', it will have '' in updates, then it is fine, we shoudl nto check it | ||
if (!ensureTemplateHasAllParams(oldRecord[this.enFieldName], updates[this.trFieldNames[lang]])) { | ||
return { ok: false, error: `Template params mismatch for ${updates[this.enFieldName]}. Template param names should be the same as in original string. E. g. 'Hello {name}', should be 'Hola {name}' and not 'Hola {nombre}'!` }; | ||
} | ||
} | ||
} | ||
return { ok: true }; | ||
})); | ||
// add hook on edit of any translation | ||
resourceConfig.hooks.edit.afterSave.push((_c) => __awaiter(this, [_c], void 0, function* ({ record, oldRecord }) { | ||
resourceConfig.hooks.edit.afterSave.push((_d) => __awaiter(this, [_d], void 0, function* ({ updates, oldRecord }) { | ||
console.log('🪲edit.afterSave', JSON.stringify(updates, null, 2), '-----', JSON.stringify(oldRecord, null, 2)); | ||
if (oldRecord) { | ||
@@ -203,5 +228,2 @@ // find lang which changed | ||
} | ||
if (record[this.trFieldNames[lang]] !== oldRecord[this.trFieldNames[lang]]) { | ||
langsChanged.push(lang); | ||
} | ||
} | ||
@@ -218,3 +240,3 @@ // clear frontend cache for all langsChanged | ||
// add hook on delete of any translation to reset cache | ||
resourceConfig.hooks.delete.afterSave.push((_d) => __awaiter(this, [_d], void 0, function* ({ record }) { | ||
resourceConfig.hooks.delete.afterSave.push((_e) => __awaiter(this, [_e], void 0, function* ({ record }) { | ||
for (const lang of this.options.supportedLanguages) { | ||
@@ -233,7 +255,7 @@ this.cache.clear(`${this.resourceConfig.resourceId}:frontend:${lang}`); | ||
}; | ||
resourceConfig.hooks.list.afterDatasourceResponse.push((_e) => __awaiter(this, [_e], void 0, function* ({ response }) { | ||
resourceConfig.hooks.list.afterDatasourceResponse.push((_f) => __awaiter(this, [_f], void 0, function* ({ response }) { | ||
response.forEach(addIncompleteField); | ||
return { ok: true }; | ||
})); | ||
resourceConfig.hooks.show.afterDatasourceResponse.push((_f) => __awaiter(this, [_f], void 0, function* ({ response }) { | ||
resourceConfig.hooks.show.afterDatasourceResponse.push((_g) => __awaiter(this, [_g], void 0, function* ({ response }) { | ||
addIncompleteField(response.length && response[0]); | ||
@@ -243,3 +265,3 @@ return { ok: true }; | ||
// also add edit hook beforeSave to update completedFieldName | ||
resourceConfig.hooks.edit.beforeSave.push((_g) => __awaiter(this, [_g], void 0, function* ({ record, oldRecord }) { | ||
resourceConfig.hooks.edit.beforeSave.push((_h) => __awaiter(this, [_h], void 0, function* ({ record, oldRecord }) { | ||
const futureRecord = Object.assign(Object.assign({}, oldRecord), record); | ||
@@ -251,3 +273,3 @@ const futureCompletedFieldValue = yield this.computeCompletedFieldValue(futureRecord); | ||
// add list hook to support filtering by fully_translated virtual field | ||
resourceConfig.hooks.list.beforeDatasourceRequest.push((_h) => __awaiter(this, [_h], void 0, function* ({ query }) { | ||
resourceConfig.hooks.list.beforeDatasourceRequest.push((_j) => __awaiter(this, [_j], void 0, function* ({ query }) { | ||
if (!query.filters || query.filters.length === 0) { | ||
@@ -285,2 +307,3 @@ query.filters = []; | ||
resourceConfig.options.bulkActions.push({ | ||
id: 'translate_all', | ||
label: 'Translate selected', | ||
@@ -291,3 +314,7 @@ icon: 'flowbite:language-outline', | ||
state: 'selected', | ||
action: (_j) => __awaiter(this, [_j], void 0, function* ({ selectedIds, tr }) { | ||
allowed: (_k) => __awaiter(this, [_k], void 0, function* ({ resource, adminUser, selectedIds, allowedActions }) { | ||
console.log('allowedActions', JSON.stringify(allowedActions)); | ||
return allowedActions.edit; | ||
}), | ||
action: (_l) => __awaiter(this, [_l], void 0, function* ({ selectedIds, tr }) { | ||
let translatedCount = 0; | ||
@@ -410,2 +437,7 @@ try { | ||
} | ||
// make sure LLM did not screw up with template params | ||
if (translation[this.enFieldName].includes('{') && !ensureTemplateHasAllParams(translation[this.enFieldName], translatedStr)) { | ||
console.warn(`LLM Screwed up with template params mismatch for "${translation[this.enFieldName]}"on language ${lang}, it returned "${translatedStr}"`); | ||
continue; | ||
} | ||
updateStrings[translation[this.primaryKeyFieldName]].updates[this.trFieldNames[lang]] = translatedStr; | ||
@@ -452,3 +484,3 @@ } | ||
// because this will translate all languages, we can set completedLangs to all languages | ||
const futureCompletedFieldValue = this.fullCompleatedFieldValue; | ||
const futureCompletedFieldValue = yield this.computeCompletedFieldValue(updates); | ||
yield this.adminforth.resource(this.resourceConfig.resourceId).update(strId, Object.assign(Object.assign({}, updates), { [this.options.completedFieldName]: futureCompletedFieldValue })); | ||
@@ -455,0 +487,0 @@ }))); |
46
index.ts
@@ -52,2 +52,13 @@ import AdminForth, { AdminForthPlugin, Filters, suggestIfTypo, AdminForthDataTypes } from "adminforth"; | ||
function ensureTemplateHasAllParams(template, newTemplate) { | ||
// string ensureTemplateHasAllParams("a {b} c {d}", "я {b} і {d} в") // true | ||
// string ensureTemplateHasAllParams("a {b} c {d}", "я і {d} в") // false | ||
// string ensureTemplateHasAllParams("a {b} c {d}", "я {bb} і {d} в") // false | ||
const existingParams = template.match(/{[^}]+}/g); | ||
const newParams = newTemplate.match(/{[^}]+}/g); | ||
const existingParamsSet = new Set(existingParams); | ||
const newParamsSet = new Set(newParams); | ||
return existingParamsSet.size === newParamsSet.size && [...existingParamsSet].every(p => newParamsSet.has(p)); | ||
} | ||
class AiTranslateError extends Error { | ||
@@ -220,5 +231,20 @@ constructor(message: string) { | ||
// add hook to validate user did not screw up with template params | ||
resourceConfig.hooks.edit.beforeSave.push(async ({ updates, oldRecord }: { updates: any, oldRecord?: any }): Promise<{ ok: boolean, error?: string }> => { | ||
for (const lang of this.options.supportedLanguages) { | ||
if (lang === 'en') { | ||
continue; | ||
} | ||
if (updates[this.trFieldNames[lang]]) { // if user set '', it will have '' in updates, then it is fine, we shoudl nto check it | ||
if (!ensureTemplateHasAllParams(oldRecord[this.enFieldName], updates[this.trFieldNames[lang]])) { | ||
return { ok: false, error: `Template params mismatch for ${updates[this.enFieldName]}. Template param names should be the same as in original string. E. g. 'Hello {name}', should be 'Hola {name}' and not 'Hola {nombre}'!` }; | ||
} | ||
} | ||
} | ||
return { ok: true }; | ||
}); | ||
// add hook on edit of any translation | ||
resourceConfig.hooks.edit.afterSave.push(async ({ record, oldRecord }: { record: any, oldRecord?: any }): Promise<{ ok: boolean, error?: string }> => { | ||
resourceConfig.hooks.edit.afterSave.push(async ({ updates, oldRecord }: { updates: any, oldRecord?: any }): Promise<{ ok: boolean, error?: string }> => { | ||
console.log('🪲edit.afterSave', JSON.stringify(updates, null, 2),'-----', JSON.stringify(oldRecord, null, 2)); | ||
if (oldRecord) { | ||
@@ -231,5 +257,2 @@ // find lang which changed | ||
} | ||
if (record[this.trFieldNames[lang]] !== oldRecord[this.trFieldNames[lang]]) { | ||
langsChanged.push(lang); | ||
} | ||
} | ||
@@ -247,2 +270,3 @@ | ||
return { ok: true }; | ||
@@ -326,2 +350,3 @@ }); | ||
{ | ||
id: 'translate_all', | ||
label: 'Translate selected', | ||
@@ -332,2 +357,6 @@ icon: 'flowbite:language-outline', | ||
state: 'selected', | ||
allowed: async ({ resource, adminUser, selectedIds, allowedActions }) => { | ||
console.log('allowedActions', JSON.stringify(allowedActions)); | ||
return allowedActions.edit; | ||
}, | ||
action: async ({ selectedIds, tr }) => { | ||
@@ -469,2 +498,7 @@ let translatedCount = 0; | ||
} | ||
// make sure LLM did not screw up with template params | ||
if (translation[this.enFieldName].includes('{') && !ensureTemplateHasAllParams(translation[this.enFieldName], translatedStr)) { | ||
console.warn(`LLM Screwed up with template params mismatch for "${translation[this.enFieldName]}"on language ${lang}, it returned "${translatedStr}"`); | ||
continue; | ||
} | ||
updateStrings[ | ||
@@ -517,3 +551,3 @@ translation[this.primaryKeyFieldName] | ||
translatedStr: string | ||
}> = {}; | ||
}> = {}; | ||
@@ -546,3 +580,3 @@ | ||
// because this will translate all languages, we can set completedLangs to all languages | ||
const futureCompletedFieldValue = this.fullCompleatedFieldValue; | ||
const futureCompletedFieldValue = await this.computeCompletedFieldValue(updates); | ||
@@ -549,0 +583,0 @@ await this.adminforth.resource(this.resourceConfig.resourceId).update(strId, { |
{ | ||
"name": "@adminforth/i18n", | ||
"version": "1.0.20-next.0", | ||
"version": "1.0.20", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5481197
5253
1
2