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

Kai Reinhard
06.50.2019 de2f2ca49cce4f4bf8fd0b86004bc29a9029db92
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
package de.micromata.borgbutler;
 
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.*;
 
/**
 * For internationalization.
 */
public class CoreI18n extends I18n {
    private static Logger log = LoggerFactory.getLogger(CoreI18n.class);
    private static Locale[] DEFAULT_LOCALES = {Locale.ROOT, Locale.GERMAN};
 
    private static CoreI18n defaultInstance = new CoreI18n();
    public static final String BUNDLE_NAME = "BorgButlerCoreMessagesBundle";
    private static List<I18n> allResourceBundles = new ArrayList<>();
 
    public static CoreI18n getDefault() {
        return defaultInstance;
    }
 
    /**
     * Use this if only one locale is used (in a none multi user system).
     * At default the default message bundle "MessagesBundle" of the class path with the system's default locale is used.
     *
     * @param instance
     */
    public static void setDefault(CoreI18n instance) {
        defaultInstance = instance;
    }
 
    /**
     * Use this if only one locale is used (in a none multi user system).
     * Uses bundle "MessagesBundle" of the class path with the given locale.
     *
     * @param locale
     * @return new default instance for chaining.
     */
    public static CoreI18n setDefault(Locale locale) {
        defaultInstance = new CoreI18n(locale);
        return defaultInstance;
    }
 
    public static Set<String> getAllTranslations(String key) {
        Set<String> set = new HashSet<>();
        for (I18n i18n : getAllResourceBundles()) {
            String translation = i18n.getMessage(key);
            if (StringUtils.isNotBlank(translation))
                set.add(translation);
        }
        return set;
    }
 
    private static List<I18n> getAllResourceBundles() {
        if (!allResourceBundles.isEmpty()) {
            return allResourceBundles;
        }
        synchronized (allResourceBundles) {
            for (Locale locale : DEFAULT_LOCALES) {
                allResourceBundles.add(new CoreI18n(locale));
            }
        }
        return allResourceBundles;
    }
 
    /**
     * Uses the default message bundle "MessagesBundle" of class path with systems default locale.
     */
    public CoreI18n() {
        super(BUNDLE_NAME);
    }
 
    public CoreI18n(Locale locale) {
        super(BUNDLE_NAME, locale);
    }
 
    @Override
    protected I18n create(Locale locale) {
        return new CoreI18n(locale);
    }
 
}