1 | /** | |
2 | * SpamSchutz - simple way to protect your mail addresses from naïve spammers | |
3 | * Copyright (C) 2011, Aiki IT | |
4 | * <p/> | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * <p/> | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * <p/> | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | package de.aikiit.spamprotector.util; | |
19 | ||
20 | import com.google.common.base.Strings; | |
21 | import lombok.AccessLevel; | |
22 | import lombok.NoArgsConstructor; | |
23 | import org.apache.logging.log4j.LogManager; | |
24 | import org.apache.logging.log4j.Logger; | |
25 | ||
26 | import java.text.MessageFormat; | |
27 | import java.util.Arrays; | |
28 | import java.util.Locale; | |
29 | import java.util.MissingResourceException; | |
30 | import java.util.ResourceBundle; | |
31 | ||
32 | /** | |
33 | * Helper to deal with localizations and ease the usage of | |
34 | * resource bundles within spamschutz. | |
35 | */ | |
36 | @NoArgsConstructor(access = AccessLevel.PRIVATE) | |
37 | public final class LocalizationHelper { | |
38 | ||
39 | /** | |
40 | * Logger. | |
41 | */ | |
42 | private static final Logger LOG = | |
43 | LogManager.getLogger(LocalizationHelper.class); | |
44 | ||
45 | /** | |
46 | * ResourceBundle used for this application. | |
47 | */ | |
48 | private static final String BASE_NAME = "spamprotector"; | |
49 | private static final Locale FALLBACK_LOCALE = Locale.GERMANY; | |
50 | ||
51 | private static ResourceBundle BUNDLE; | |
52 | private static Locale LOCALE; | |
53 | private static MessageFormat FORMAT; | |
54 | ||
55 | static { | |
56 | setLocale(); | |
57 | } | |
58 | ||
59 | /** | |
60 | * Set locale depending on system properties, in case of errors fallback is Locale.GERMANY. | |
61 | */ | |
62 | public static void setLocale() { | |
63 | String userLanguage = System.getProperty("user.language"); | |
64 | String userCountry = System.getProperty("user.country"); | |
65 | ||
66 | LOG.info(String.format("Your system emits the following l10n-properties: language=%s, country=%s", userLanguage, userCountry)); | |
67 | ||
68 |
2
1. setLocale : negated conditional → KILLED 2. setLocale : negated conditional → KILLED |
if (Strings.isNullOrEmpty(userLanguage) || Strings.isNullOrEmpty(userCountry)) { |
69 | LOCALE = FALLBACK_LOCALE; | |
70 | LOG.info("Falling back to locale " + LOCALE); | |
71 | } else { | |
72 | LOCALE = new Locale(userLanguage, userCountry); | |
73 | LOG.info("Setting locale to " + LOCALE); | |
74 | } | |
75 | ||
76 | BUNDLE = ResourceBundle.getBundle(BASE_NAME, LOCALE); | |
77 | FORMAT = new MessageFormat(""); | |
78 |
1
1. setLocale : removed call to java/text/MessageFormat::setLocale → SURVIVED |
FORMAT.setLocale(LOCALE); |
79 | } | |
80 | ||
81 | /** | |
82 | * @return the currently set Locale of this application. Fallback is Locale.GERMANY. | |
83 | */ | |
84 | public static Locale getLocale() { | |
85 |
1
1. getLocale : negated conditional → KILLED |
if (LOCALE == null) { |
86 | LOG.warn("Returning fallback Locale for Germany - please make sure you've configured your environment correctly via system properties 'user.country'/'user.language'."); | |
87 |
1
1. getLocale : replaced return value with null for de/aikiit/spamprotector/util/LocalizationHelper::getLocale → NO_COVERAGE |
return FALLBACK_LOCALE; |
88 | } | |
89 |
1
1. getLocale : replaced return value with null for de/aikiit/spamprotector/util/LocalizationHelper::getLocale → KILLED |
return LOCALE; |
90 | } | |
91 | ||
92 | /** | |
93 | * @return the currently set language | |
94 | */ | |
95 | public static String getLanguage() { | |
96 |
1
1. getLanguage : replaced return value with "" for de/aikiit/spamprotector/util/LocalizationHelper::getLanguage → KILLED |
return getLocale().getLanguage(); |
97 | } | |
98 | ||
99 | /** | |
100 | * Helper function to retrieve a given key | |
101 | * from the underlying resource bundle. | |
102 | * | |
103 | * @param key Key to retrieve from the bundle, | |
104 | * e.g. <i>spamprotector.foo.title</i> | |
105 | * @return Returns the value from the bundle. | |
106 | */ | |
107 | public static String getBundleString(final String key) { | |
108 | LOG.debug("Retrieving key " + key); | |
109 | try { | |
110 |
1
1. getBundleString : replaced return value with "" for de/aikiit/spamprotector/util/LocalizationHelper::getBundleString → KILLED |
return BUNDLE.getString(key); |
111 | } catch (MissingResourceException mre) { | |
112 | LOG.error("Retrieving unknown key " + key + ". Please fix your property files."); | |
113 |
1
1. getBundleString : replaced return value with "" for de/aikiit/spamprotector/util/LocalizationHelper::getBundleString → KILLED |
return key; |
114 | } | |
115 | } | |
116 | ||
117 | /** | |
118 | * Helper function to retrieve a given key | |
119 | * from the underlying resource bundle while | |
120 | * applying localization parameters. | |
121 | * | |
122 | * @param key Key to retrieve from the bundle, | |
123 | * e.g. <i>spamprotector.foo.title</i> | |
124 | * @param parameters Object array with all parameters. | |
125 | * @return Returns the value from the bundle | |
126 | * with the given parameters applied. | |
127 | * @see <a href="https://docs.oracle.com/javase/tutorial/i18n/format/messageFormat.html"> | |
128 | * I18N-tutorial</a> | |
129 | */ | |
130 | public static String getParameterizedBundleString(final String key, final Object... parameters) { | |
131 | LOG.debug("Applying " + ((parameters == null) ? null : Arrays.toString(parameters)) + " parameters to " + key); | |
132 |
1
1. getParameterizedBundleString : removed call to java/text/MessageFormat::applyPattern → KILLED |
FORMAT.applyPattern(getBundleString(key)); |
133 |
1
1. getParameterizedBundleString : replaced return value with "" for de/aikiit/spamprotector/util/LocalizationHelper::getParameterizedBundleString → KILLED |
return FORMAT.format(parameters); |
134 | } | |
135 | ||
136 | } | |
Mutations | ||
68 |
1.1 2.2 |
|
78 |
1.1 |
|
85 |
1.1 |
|
87 |
1.1 |
|
89 |
1.1 |
|
96 |
1.1 |
|
110 |
1.1 |
|
113 |
1.1 |
|
132 |
1.1 |
|
133 |
1.1 |