Translate collections/fields live (#5081)

Fixes #5078
This commit is contained in:
Rijk van Zanten
2021-04-15 11:27:52 -04:00
committed by GitHub
parent 07b85b7eb7
commit 8af2cbadfa
7 changed files with 101 additions and 58 deletions

View File

@@ -32,8 +32,8 @@ export const useCollectionsStore = createStore({
const collections: CollectionRaw[] = response.data.data;
this.state.collections = collections.map((collection: CollectionRaw) => {
let name: string | VueI18n.TranslateResult;
const icon = collection.meta?.icon || 'label';
const name = formatTitle(collection.collection);
if (collection.meta && notEmpty(collection.meta.translations)) {
for (let i = 0; i < collection.meta.translations.length; i++) {
@@ -45,7 +45,22 @@ export const useCollectionsStore = createStore({
},
});
}
}
return {
...collection,
name,
icon,
};
});
this.translateCollections();
},
translateCollections() {
this.state.collections = this.state.collections.map((collection: CollectionRaw) => {
let name: string | VueI18n.TranslateResult;
if (i18n.te(`collection_names.${collection.collection}`)) {
name = i18n.t(`collection_names.${collection.collection}`);
} else {
name = formatTitle(collection.collection);
@@ -54,7 +69,6 @@ export const useCollectionsStore = createStore({
return {
...collection,
name,
icon,
};
});
},

View File

@@ -66,16 +66,16 @@ export const useFieldsStore = createStore({
*/
this.state.fields = [...fields.map(this.parseField), fakeFilesField];
this.translateFields();
},
async dehydrate() {
this.reset();
},
parseField(field: FieldRaw): Field {
let name: string | VueI18n.TranslateResult;
const name = formatTitle(field.field);
if (i18n.te(`fields.${field.collection}.${field.field}`)) {
name = i18n.t(`fields.${field.collection}.${field.field}`);
} else if (field.meta && notEmpty(field.meta.translations) && field.meta.translations.length > 0) {
if (field.meta && notEmpty(field.meta.translations) && field.meta.translations.length > 0) {
for (let i = 0; i < field.meta.translations.length; i++) {
const { language, translation } = field.meta.translations[i];
@@ -87,10 +87,6 @@ export const useFieldsStore = createStore({
},
});
}
name = i18n.t(`fields.${field.collection}.${field.field}`);
} else {
name = formatTitle(field.field);
}
return {
@@ -98,6 +94,22 @@ export const useFieldsStore = createStore({
name,
};
},
translateFields() {
this.state.fields = this.state.fields.map((field) => {
let name: string | VueI18n.TranslateResult;
if (i18n.te(`fields.${field.collection}.${field.field}`)) {
name = i18n.t(`fields.${field.collection}.${field.field}`);
} else {
name = formatTitle(field.field);
}
return {
...field,
name,
};
});
},
async createField(collectionKey: string, newField: Field) {
const stateClone = [...this.state.fields];