Skip to content

Commit 96149b9

Browse files
committed
i18n : clear cache during manula updates
1 parent adbdbec commit 96149b9

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

adminforth/plugins/i18n/Changelog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [1.0.20] - next
8+
## [1.0.21] - next
9+
10+
## [1.0.20]
11+
12+
### Fixed
13+
- fix automatic translations
914

1015
## [1.0.14]
1116

adminforth/plugins/i18n/index.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -244,35 +244,41 @@ export default class I18N extends AdminForthPlugin {
244244

245245
// add hook on edit of any translation
246246
resourceConfig.hooks.edit.afterSave.push(async ({ updates, oldRecord }: { updates: any, oldRecord?: any }): Promise<{ ok: boolean, error?: string }> => {
247-
console.log('🪲edit.afterSave', JSON.stringify(updates, null, 2),'-----', JSON.stringify(oldRecord, null, 2));
248247
if (oldRecord) {
249248
// find lang which changed
250249
let langsChanged: LanguageCode[] = [];
251250
for (const lang of this.options.supportedLanguages) {
252251
if (lang === 'en') {
253252
continue;
254253
}
254+
if (updates[this.trFieldNames[lang]]) {
255+
langsChanged.push(lang);
256+
}
255257
}
256-
258+
257259
// clear frontend cache for all langsChanged
258260
for (const lang of langsChanged) {
259-
this.cache.clear(`${this.resourceConfig.resourceId}:frontend:${lang}`);
261+
if (oldRecord[this.options.categoryFieldName] === 'frontend') {
262+
this.cache.clear(`${this.resourceConfig.resourceId}:frontend:${lang}`);
263+
} else {
264+
this.cache.clear(`${this.resourceConfig.resourceId}:${oldRecord[this.options.categoryFieldName]}:${lang}:${oldRecord[this.enFieldName]}`);
265+
}
260266
}
261-
262267
this.updateUntranslatedMenuBadge();
263-
264268
}
265269
// clear frontend cache for all lan
266-
267-
268270
return { ok: true };
269271
});
270272

271273
// add hook on delete of any translation to reset cache
272274
resourceConfig.hooks.delete.afterSave.push(async ({ record }: { record: any }): Promise<{ ok: boolean, error?: string }> => {
273275
for (const lang of this.options.supportedLanguages) {
274-
this.cache.clear(`${this.resourceConfig.resourceId}:frontend:${lang}`);
275-
this.cache.clear(`${this.resourceConfig.resourceId}:${record[this.options.categoryFieldName]}:${lang}:${record[this.enFieldName]}`);
276+
// if frontend, clear frontend cache
277+
if (record[this.options.categoryFieldName] === 'frontend') {
278+
this.cache.clear(`${this.resourceConfig.resourceId}:frontend:${lang}`);
279+
} else {
280+
this.cache.clear(`${this.resourceConfig.resourceId}:${record[this.options.categoryFieldName]}:${lang}:${record[this.enFieldName]}`);
281+
}
276282
}
277283
this.updateUntranslatedMenuBadge();
278284
return { ok: true };
@@ -349,7 +355,7 @@ export default class I18N extends AdminForthPlugin {
349355
// if optional `confirm` is provided, user will be asked to confirm action
350356
confirm: 'Are you sure you want to translate selected items?',
351357
state: 'selected',
352-
allowed: ({ resource, adminUser, selectedIds, allowedActions }) => {
358+
allowed: async ({ resource, adminUser, selectedIds, allowedActions }) => {
353359
console.log('allowedActions', JSON.stringify(allowedActions));
354360
return allowedActions.edit;
355361
},
@@ -569,7 +575,7 @@ JSON.stringify(strings.reduce((acc: object, s: { en_string: string }): object =>
569575
Object.entries(updateStrings).map(
570576
async ([_, { updates, strId }]: [string, { updates: any, category: string, strId: string }]) => {
571577
// because this will translate all languages, we can set completedLangs to all languages
572-
const futureCompletedFieldValue = this.computeCompletedFieldValue(updates);
578+
const futureCompletedFieldValue = await this.computeCompletedFieldValue(updates);
573579

574580
await this.adminforth.resource(this.resourceConfig.resourceId).update(strId, {
575581
...updates,

0 commit comments

Comments
 (0)