|
6 | 6 | import java.util.regex.Matcher; |
7 | 7 | import java.util.regex.Pattern; |
8 | 8 |
|
9 | | -/** This class makes sure the java locale is set according to the environment variables LC_ALL and LANG |
| 9 | +/** |
| 10 | + * This class makes sure the java locale is set according to the environment variables LC_ALL and LANG |
10 | 11 | * We experienced that, in some cases, the locale was not set as expected, therefore this class implements some clear |
11 | 12 | * rules: |
12 | | - * 1. If environment variable LC_ALL is set, we try to parse its content and set locale according to its value if valid |
13 | | - * 2. If environment variable LANG is set, we try to parse its content and set locale according to its value if valid |
14 | | - * 3. Otherwise we use default locale |
| 13 | + * 1. If environment variable LC_ALL is set, we try to parse its content and set locale according to its value if valid |
| 14 | + * 2. If environment variable LANG is set, we try to parse its content and set locale according to its value if valid |
| 15 | + * 3. Otherwise we use default locale |
15 | 16 | * |
16 | | - * @author pesse |
| 17 | + * @author pesse |
17 | 18 | */ |
18 | 19 | class LocaleInitializer { |
19 | 20 |
|
20 | 21 | private static final Pattern REGEX_LOCALE = Pattern.compile("^([a-zA-Z]+)[_-]([a-zA-Z]+)"); // We only need the very first part and are pretty forgiving in parsing |
21 | 22 |
|
22 | | - /** Sets the default locale according to the rules described above |
23 | | - * |
| 23 | + /** |
| 24 | + * Sets the default locale according to the rules described above |
24 | 25 | */ |
25 | 26 | static void initLocale() { |
26 | 27 |
|
27 | 28 | boolean localeChanged = setDefaultLocale(EnvironmentVariableUtil.getEnvValue("LC_ALL")); |
28 | | - if ( !localeChanged ) |
| 29 | + if (!localeChanged) { |
29 | 30 | setDefaultLocale(EnvironmentVariableUtil.getEnvValue("LANG")); |
| 31 | + } |
30 | 32 | } |
31 | 33 |
|
32 | | - /** Set the default locale from a given string like LC_ALL or LANG environment variable |
| 34 | + /** |
| 35 | + * Set the default locale from a given string like LC_ALL or LANG environment variable |
33 | 36 | * |
34 | 37 | * @param localeString Locale-string from LC_ALL or LANG, e.g "en_US.utf-8" |
35 | 38 | * @return true if successful, false if not |
36 | 39 | */ |
37 | | - private static boolean setDefaultLocale( String localeString ) { |
38 | | - if ( localeString == null || localeString.isEmpty() ) |
| 40 | + private static boolean setDefaultLocale(String localeString) { |
| 41 | + if (localeString == null || localeString.isEmpty()) { |
39 | 42 | return false; |
| 43 | + } |
40 | 44 |
|
41 | 45 | try { |
42 | 46 | Matcher m = REGEX_LOCALE.matcher(localeString); |
43 | 47 | if (m.find()) { |
44 | 48 | StringBuilder sb = new StringBuilder(); |
45 | 49 | sb.append(m.group(1)); |
46 | | - if (m.group(2) != null) |
| 50 | + if (m.group(2) != null) { |
47 | 51 | sb.append("-").append(m.group(2)); |
| 52 | + } |
48 | 53 |
|
49 | 54 | Locale l = new Locale.Builder().setLanguageTag(sb.toString()).build(); |
50 | | - if ( l != null ) { |
| 55 | + if (l != null) { |
51 | 56 | Locale.setDefault(l); |
52 | 57 | return true; |
53 | 58 | } |
54 | 59 | } |
55 | | - } |
56 | | - catch ( Exception e ) { |
| 60 | + } catch (Exception e) { |
57 | 61 | System.out.println("Could not get locale from " + localeString); |
58 | 62 | } |
59 | 63 |
|
|
0 commit comments