• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trydofor / professional-mirana / #75

14 Jan 2025 10:10AM UTC coverage: 87.258% (+3.2%) from 84.041%
#75

push

trydofor
✨ common error code enum #49

310 of 319 new or added lines in 6 files covered. (97.18%)

3 existing lines in 2 files now uncovered.

6725 of 7707 relevant lines covered (87.26%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/src/main/java/pro/fessional/mirana/i18n/I18nAware.java
1
package pro.fessional.mirana.i18n;
2

3
import org.jetbrains.annotations.NotNull;
4

5
import java.io.Serializable;
6
import java.text.MessageFormat;
7
import java.util.Locale;
8

9
/**
10
 * code should be fixed.
11
 * hint and args can be changed.
12
 *
13
 * @author trydofor
14
 * @since 2019-09-09
15
 */
16
public interface I18nAware extends Serializable {
17

18
    /**
19
     * the i18n code, also template id
20
     */
21
    default String getI18nCode() {
UNCOV
22
        return null;
×
23
    }
24

25
    /**
26
     * the default message or template (if no template by code)
27
     */
28
    default String getI18nHint() {
29
        return null;
1✔
30
    }
31

32
    /**
33
     * the args of template
34
     */
35
    default Object[] getI18nArgs() {
36
        return null;
1✔
37
    }
38

39
    @NotNull
40
    default I18nString toI18nString() {
41
        return toI18nString(null, (Object[]) null);
1✔
42
    }
43

44
    /**
45
     * use getI18nHint() if hint is null
46
     */
47
    @NotNull
48
    default I18nString toI18nString(String hint) {
49
        return toI18nString(hint, (Object[]) null);
1✔
50
    }
51

52
    /**
53
     * use getI18nHint() if hint is null, getI18nArgs() if args is null
54
     */
55
    @NotNull
56
    default I18nString toI18nString(String hint, Object... args) {
57
        hint = hint == null ? getI18nHint() : hint;
1✔
58
        args = args == null ? getI18nArgs() : args;
1✔
59
        return new I18nString(getI18nCode(), hint, args);
1✔
60
    }
61

62
    /**
63
     * use Locale.getDefault() if locale is null.
64
     * return the i18nCode if message format get empty.
65
     */
66
    default String toString(Locale locale) {
67
        return toString(locale, (code, args, hint, lang) -> {
1✔
68
            if (hint == null || hint.isEmpty() || args == null || args.length == 0) return hint;
1✔
69

70
            return new MessageFormat(hint, lang == null ? Locale.getDefault() : lang)
1✔
71
                .format(args);
1✔
72
        });
73
    }
74

75
    /**
76
     * <pre>
77
     * should use Locale.getDefault() if locale is null.
78
     * return the i18nCode if message format get empty.
79
     * recursively evaluate the I18nAware in i18nArgs.
80
     * </pre>
81
     */
82
    default String toString(Locale locale, @NotNull I18nSource source) {
83
        Object[] args = getI18nArgs();
1✔
84
        if (args != null) {
1✔
85
            for (int i = 0; i < args.length; i++) {
1✔
86
                if (args[i] instanceof I18nAware) {
1✔
87
                    args[i] = ((I18nAware) args[i]).toString(locale, source);
1✔
88
                }
89
            }
90
        }
91
        String msg = source.getMessage(getI18nCode(), args, getI18nHint(), locale);
1✔
92
        return msg == null || msg.isEmpty() ? getI18nCode() : msg;
1✔
93
    }
94

95
    @FunctionalInterface
96
    interface I18nSource {
97
        /**
98
         * <pre>
99
         * should return null or code if template is not found by code.
100
         * hint is the default message or template
101
         * refer spring MessageSource
102
         * </pre>
103
         */
104
        String getMessage(String code, Object[] args, String hint, Locale lang);
105

106
        /**
107
         * varargs sugar for coding
108
         */
109
        default String getMessage(String code, Locale lang, String hint, Object[] args) {
NEW
110
            return getMessage(code, args, hint, lang);
×
111
        }
112
    }
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc