-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
108 lines (103 loc) · 3.21 KB
/
index.tsx
File metadata and controls
108 lines (103 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { getTranslation } from './utils/getTranslation';
import { LanguageFieldIcon } from './components/LanguageFieldIcon';
import { PLUGIN_ID } from './pluginId';
import { getLanguageOptions, languageCode } from './utils/getLanguageOptions';
import { getCurrentContentLocale } from './utils/getCurrentContentLocale';
type TranslateOptions = Record<string, string>;
const prefixPluginTranslations = (translate: TranslateOptions, pluginId: string): TranslateOptions => {
if (!pluginId) {
throw new TypeError("pluginId can't be empty");
}
return Object.keys(translate).reduce((acc, current) => {
acc[`${pluginId}.${current}`] = translate[current];
return acc;
}, {} as TranslateOptions);
};
export default {
register(app: any) {
const locale = getCurrentContentLocale() || 'nl';
const optionsForStrapi = getLanguageOptions(languageCode, locale).map((lang) => ({
key: lang.code,
value: lang.code,
metadatas: {
intlLabel: {
id: `${PLUGIN_ID}.${lang.code}`,
defaultMessage: lang.name,
},
},
}));
app.customFields.register({
name: PLUGIN_ID,
pluginId: PLUGIN_ID,
type: 'string',
icon: LanguageFieldIcon,
intlLabel: {
id: getTranslation(`${PLUGIN_ID}.label`),
defaultMessage: 'Select language',
},
intlDescription: {
id: getTranslation(`${PLUGIN_ID}.description`),
defaultMessage: 'Select language',
},
components: {
Input: async () =>
import('./components/LanguageField').then((module) => ({
default: module.LanguageField,
})),
},
options: {
advanced: [
{
sectionTitle: {
id: 'global.settings',
defaultMessage: 'Settings',
},
items: [
{
name: 'required',
type: 'checkbox',
intlLabel: {
id: 'form.attribute.item.requiredField',
defaultMessage: 'Required field',
},
description: {
id: 'form.attribute.item.requiredField.description',
defaultMessage: "You won't be able to create an entry if this field is empty",
},
},
{
intlLabel: {
id: 'my-plugin.settings.defaultLanguage',
defaultMessage: 'Default language',
},
name: 'options.defaultLanguage',
type: 'select',
options: optionsForStrapi,
},
],
},
],
},
});
},
async registerTrads({ locales }: { locales: string[] }) {
const importedTranslations = await Promise.all(
locales.map((locale: any) => {
return import(`./translations/${locale}.json`)
.then(({ default: data }) => {
return {
data: prefixPluginTranslations(data, PLUGIN_ID),
locale,
};
})
.catch(() => {
return {
data: {},
locale,
};
});
}),
);
return Promise.resolve(importedTranslations);
},
};