Skip to content

Commit 5a54402

Browse files
authored
Merge pull request #59 from utPLSQL/bugfix/nls_from_environment
Added locale initialization from environment variables NLS_LANG, LC_ALL or LANG Fixes #56
2 parents 2cb8ec9 + 2e88a8c commit 5a54402

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ You can also download all development versions from [Bintray](https://bintray.co
2727
The latest CLI is always compatible with all database frameworks of the same minor version.
2828
For example CLI-3.0.4 is compatible with database framework 3.0.0-3.0.4 but not with database framework 2.x and 3.1.x.
2929

30+
## Localization and NLS settings
31+
utPLSQL-cli will use the environment variables (in that order) "NLS_LANG", "LC_ALL" or "LANG" to change the locale and therefore the NLS settings.
32+
If neither environment variable is available, it will use the JVM default locale.
33+
34+
Example: to change the NLS-settings to English American, you can do the following:
35+
```
36+
export LC_ALL=en_US.utf-8
37+
```
38+
39+
The charset-part of LC_ALL is ignored.
40+
3041
## Usage
3142

3243
`utplsql run <ConnectionURL> [-p=(ut_path|ut_paths)] [-f=format [-o=output_file] [-s] ...]`

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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 NLS_LANG is set, we try to parse its content and set locale according to its value if valid
11+
* 2. If environment variable LC_ALL is set, we try to parse its content and set locale according to its value if valid
12+
* 3. If environment variable LANG is set, we try to parse its content and set locale according to its value if valid
13+
* 4. Otherwise we use default locale
14+
*
15+
* @author pesse
16+
*/
17+
class LocaleInitializer {
18+
19+
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
20+
21+
/** Sets the default locale according to the rules described above
22+
*
23+
*/
24+
static void initLocale() {
25+
26+
boolean localeChanged = setDefaultLocale(System.getenv("NLS_LANG"));
27+
28+
if ( !localeChanged )
29+
localeChanged = setDefaultLocale(System.getenv("LC_ALL"));
30+
if ( !localeChanged )
31+
setDefaultLocale(System.getenv("LANG"));
32+
}
33+
34+
/** Set the default locale from a given string like LC_ALL or LANG environment variable
35+
*
36+
* @param localeString Locale-string from LC_ALL or LANG, e.g "en_US.utf-8"
37+
* @return true if successful, false if not
38+
*/
39+
private static boolean setDefaultLocale( String localeString ) {
40+
if ( localeString == null || localeString.isEmpty() )
41+
return false;
42+
43+
try {
44+
Matcher m = REGEX_LOCALE.matcher(localeString);
45+
if (m.find()) {
46+
StringBuilder sb = new StringBuilder();
47+
sb.append(m.group(1));
48+
if (m.group(2) != null)
49+
sb.append("-").append(m.group(2));
50+
51+
Locale l = new Locale.Builder().setLanguageTag(sb.toString()).build();
52+
if ( l != null ) {
53+
Locale.setDefault(l);
54+
return true;
55+
}
56+
}
57+
}
58+
catch ( Exception e ) {
59+
System.out.println("Could not get locale from " + localeString);
60+
}
61+
62+
return false;
63+
}
64+
}

0 commit comments

Comments
 (0)