Skip to content

Commit ba4b5b9

Browse files
pesseSamuel Nitsche
authored andcommitted
Added locale initialization from environment variables LC_ALL or LANG
Fixes #56
1 parent 2cb8ec9 commit ba4b5b9

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

src/main/java/org/utplsql/cli/Cli.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

1010
public class Cli {
1111

12-
public static final int DEFAULT_ERROR_CODE = 1;
12+
static final int DEFAULT_ERROR_CODE = 1;
1313

14-
public static final String HELP_CMD = "-h";
15-
public static final String RUN_CMD = "run";
14+
static final String HELP_CMD = "-h";
15+
private static final String RUN_CMD = "run";
1616

1717
public static void main(String[] args) {
18+
19+
LocaleInitializer.initLocale();
20+
1821
JCommander jc = new JCommander();
1922
// jc.addCommand(HELP_CMD, new HelpCommand());
2023
RunCommand runCmd = new RunCommand();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.utplsql.cli;
2+
3+
import java.util.Locale;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
6+
7+
/** This class makes sure the java locale is set according to the environment variables LC_ALL and LANG
8+
* We experienced that, in some cases, the locale was not set as expected, therefore this class implements some clear
9+
* rules:
10+
* 1. If environment variable LC_ALL is set, we try to parse its content and set locale according to its value if valid
11+
* 2. If environment variable LANG is set, we try to parse its content and set locale according to its value if valid
12+
* 3. Otherwise we use default locale
13+
*
14+
* @author pesse
15+
*/
16+
class LocaleInitializer {
17+
18+
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
19+
20+
/** Sets the default locale according to the rules described above
21+
*
22+
*/
23+
static void initLocale() {
24+
if ( !setDefaultLocale(System.getenv("LC_ALL")))
25+
setDefaultLocale(System.getenv("LANG"));
26+
}
27+
28+
/** Set the default locale from a given string like LC_ALL or LANG environment variable
29+
*
30+
* @param localeString Locale-string from LC_ALL or LANG, e.g "en_US.utf-8"
31+
* @return true if successful, false if not
32+
*/
33+
private static boolean setDefaultLocale( String localeString ) {
34+
if ( localeString == null || localeString.isEmpty() )
35+
return false;
36+
37+
try {
38+
Matcher m = REGEX_LOCALE.matcher(localeString);
39+
if (m.find()) {
40+
StringBuilder sb = new StringBuilder();
41+
sb.append(m.group(1));
42+
if (m.group(2) != null)
43+
sb.append("-").append(m.group(2));
44+
45+
Locale l = new Locale.Builder().setLanguageTag(sb.toString()).build();
46+
if ( l != null ) {
47+
Locale.setDefault(l);
48+
return true;
49+
}
50+
}
51+
}
52+
catch ( Exception e ) {
53+
System.out.println("Could not get locale from " + localeString);
54+
}
55+
56+
return false;
57+
}
58+
}

0 commit comments

Comments
 (0)