getters.ts 2.92 KB
Newer Older
1
2
import {FormState} from '../formModule';
import {GetterTree} from 'vuex';
3
import {RootState} from '@form-plugin/store';
MARCO Jonathan's avatar
MARCO Jonathan committed
4
import {Locale, LocaleAttributes} from '#form-plugin/models/messages';
MARCO Jonathan's avatar
MARCO Jonathan committed
5
import {Field} from '#form-plugin/models/form';
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

export interface LocaleNameAndRTLStatus {
    locale: string;
    rtl: boolean;
}

export const messagesGetter: GetterTree<FormState, RootState> = {
    /**
     * Return the list with all locale name.
     *
     * @param {FormState} state
     * @return {string[]}
     */
    availableLocales: (state: FormState): string[] => Object.keys(state.messages.locales),
    /**
     * Return the name of the default locale.
     *
     * @param {FormState} state
     * @return {string}
     */
    fallbackLocale: (state: FormState): string => state.messages.defaultLocale,
    /**
     * Return all locales name and the rtl status.
     *
     * @param {FormState} state
     * @return {{locale: string, rtl: boolean}[]}
     */
    locales: (state: FormState): LocaleNameAndRTLStatus[] => Object.entries(state.messages.locales)
                                                                   .map(([locale, {rtl}]) => ({locale, rtl})),
    /**
     * Return the translation for one field, one property.
     *
     * @param {FormState} state
     * @return {(field, locale, prop) => string}
     */
MARCO Jonathan's avatar
MARCO Jonathan committed
41
42
43
44
    translation: (state: FormState): (
        field: string,
        locale: string,
        prop: LocaleAttributes,
45
46
    ) => string => {
        return (field, locale, prop): string => {
47
            return (state.messages.locales[locale][field] as Locale)?.[prop] || '';
MARCO Jonathan's avatar
MARCO Jonathan committed
48
        };
49
50
51
52
53
54
55
56
    },
    /**
     * Return the translation for one field.
     * If the translation for the current locale not exist, returns the fallback translation, if it not exists returns a empty string.
     *
     * @param {FormState} state
     * @return {(id, prop, type?: string) => any}
     */
MARCO Jonathan's avatar
MARCO Jonathan committed
57
58
59
60
    translated: (state: FormState): (
        id: string,
        prop: LocaleAttributes,
        type: 'fields' | 'options',
61
62
    ) => string => {
        return (id, prop, type = 'fields'): string => {
MARCO Jonathan's avatar
MARCO Jonathan committed
63
            let translation: string = '';
64
            if (state.messages.current && state.messages.locales?.[state.messages.current]?.[id]) {
65
                translation = <string | undefined>(state.messages.locales?.[state.messages.current]?.[id] as Locale)?.[prop] || '';
66
67
68
69
70
            }

            if (
                translation === '' &&
                state.messages.current !== state.messages.defaultLocale &&
71
                state.messages.locales?.[state.messages.defaultLocale]?.[id]
72
            ) {
73
                translation = <string>(state.messages.locales?.[state.messages.defaultLocale]?.[id] as Locale)?.[prop] || '';
74
75
            }

MARCO Jonathan's avatar
MARCO Jonathan committed
76
            if (translation === '' && (state[type][id] as Field)[prop]) {
MARCO Jonathan's avatar
MARCO Jonathan committed
77
                translation = <string>(state[type][id] as Field)[prop];
78
79
            }

MARCO Jonathan's avatar
MARCO Jonathan committed
80
81
            return translation;
        };
82
    },
MARCO Jonathan's avatar
MARCO Jonathan committed
83
};