mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
14.20.2021 5e39c0040ddde260831a5b9f73c0bbfec3738f94
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
109
package de.micromata.borgbutler;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.text.MessageFormat;
import java.util.*;
 
/**
 * For internationalization.
 */
public class I18n {
    private Logger log = LoggerFactory.getLogger(I18n.class);
    private ResourceBundle resourceBundle;
    private Map<Locale, I18n> i18nMap;
 
    public I18n get(Locale locale) {
        if (i18nMap == null) {
            i18nMap = new HashMap<>();
        }
        I18n i18n = i18nMap.get(locale);
        if (i18n == null) {
            i18n = create(locale);
            i18nMap.put(locale, i18n);
        }
        return i18n;
    }
 
    protected I18n create(Locale locale) {
        return new I18n(this.resourceBundle.getBaseBundleName(), locale);
    }
 
    /**
     * Uses the default message bundle "MessagesBundle" of class path with systems default locale.
     */
    public I18n(String bundleName) {
        this.resourceBundle = I18n.getBundle(bundleName);
    }
 
    public I18n(String bundleName, Locale locale) {
        this.resourceBundle = I18n.getBundle(bundleName, locale);
    }
 
    /**
     * Throws an error if messageId not found.
     *
     * @param messageId
     * @return localized message.
     */
    public String getMessage(String messageId) {
        return resourceBundle.getString(messageId);
    }
 
    /**
     * @param messageId
     * @return true, if the messageId is found in the bundle, otherwise false.
     */
    public boolean containsMessage(String messageId) {
        return resourceBundle.containsKey(messageId);
    }
 
    /**
     * @param messageId
     * @param params    Message parameter to replace in message.
     * @return localized message.
     * @see MessageFormat#format(String, Object...)
     */
    public String formatMessage(String messageId, Object... params) {
        if (params == null || params.length == 0) {
            return getMessage(messageId);
        }
        try {
            return MessageFormat.format(resourceBundle.getString(messageId), params);
        } catch (MissingResourceException ex) {
            log.error("Missing resource '" + messageId + "': " + ex.getMessage(), ex);
            return messageId;
        }
    }
 
    public ResourceBundle getResourceBundle() {
        return resourceBundle;
    }
 
    /**
     *
     * @param bundleName
     * @param locale
     * @return The root bundle if the given locale's language is "en" or language not found, otherwise the desired bundle for the given locale.
     */
    public static ResourceBundle getBundle(String bundleName, Locale locale) {
        if ("en".equals(locale.getLanguage())) {
            return ResourceBundle.getBundle(bundleName, Locale.ROOT);
        }
        return ResourceBundle.getBundle(bundleName, locale);
        //return ResourceBundle.getBundle(bundleName, locale,
        //        ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES));
    }
 
    /**
     * Simply calls {@link ResourceBundle#getBundle(String)}.
     * @param bundleName
     * @return The bundle for {@link Locale#getDefault()} or root bundle if not found..
     */
    public static ResourceBundle getBundle(String bundleName) {
        return ResourceBundle.getBundle(bundleName);
        //return ResourceBundle.getBundle(bundleName, locale,
        //        ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES));
    }
}