Translate system fields when creating new collections (#8104)

* translate system fields when create collections

* handle '$t:' translations only on interface

Co-authored-by: Jose Varela <joselcvarela@gmail.com>
This commit is contained in:
Azri Kahar
2021-11-03 02:56:27 +08:00
committed by GitHub
parent f80b3f02d2
commit 27603b74b6
3 changed files with 14 additions and 5 deletions

View File

@@ -0,0 +1,9 @@
import { i18n } from '@/lang';
export function translate(literal: string): string {
let translated = literal;
if (literal.startsWith('$t:')) translated = i18n.global.t(literal.replace('$t:', ''));
return translated;
}

View File

@@ -1,4 +1,4 @@
import { i18n } from '@/lang';
import { translate as translateString } from './translate-literal';
import { cloneDeep } from 'lodash';
export function translate<T extends Record<string, any>>(obj: T): T {
@@ -6,8 +6,7 @@ export function translate<T extends Record<string, any>>(obj: T): T {
Object.entries(newObj).forEach(([key, val]) => {
if (val && typeof val === 'object') (newObj as Record<string, any>)[key] = translate(val);
if (val && typeof val === 'string' && val.startsWith('$t:'))
(newObj as Record<string, any>)[key] = i18n.global.t(val.replace('$t:', ''));
if (val && typeof val === 'string') (newObj as Record<string, any>)[key] = translateString(val);
});
return newObj;

View File

@@ -14,7 +14,7 @@
:collection="part.collection"
:field="part.field"
/>
<span v-else>{{ part }}</span>
<span v-else>{{ translate(part) }}</span>
</template>
</div>
</template>
@@ -27,6 +27,7 @@ import { DisplayConfig, Field } from '@directus/shared/types';
import { getDisplays } from '@/displays';
import ValueNull from '@/views/private/components/value-null';
import { getDefaultDisplayForType } from '@/utils/get-default-display-for-type';
import { translate } from '@/utils/translate-literal';
export default defineComponent({
components: { ValueNull },
@@ -113,7 +114,7 @@ export default defineComponent({
.map((p) => p ?? null)
);
return { parts, templateEl };
return { parts, templateEl, translate };
},
});
</script>