From ba4b5b9b10e34522156d26b51e446827606490b9 Mon Sep 17 00:00:00 2001 From: pesse Date: Mon, 15 Jan 2018 11:48:52 +0100 Subject: [PATCH 1/3] Added locale initialization from environment variables LC_ALL or LANG Fixes https://github.com/utPLSQL/utPLSQL-cli/issues/56 --- src/main/java/org/utplsql/cli/Cli.java | 9 ++- .../org/utplsql/cli/LocaleInitializer.java | 58 +++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/utplsql/cli/LocaleInitializer.java diff --git a/src/main/java/org/utplsql/cli/Cli.java b/src/main/java/org/utplsql/cli/Cli.java index bdfbac3..f2db442 100644 --- a/src/main/java/org/utplsql/cli/Cli.java +++ b/src/main/java/org/utplsql/cli/Cli.java @@ -9,12 +9,15 @@ public class Cli { - public static final int DEFAULT_ERROR_CODE = 1; + static final int DEFAULT_ERROR_CODE = 1; - public static final String HELP_CMD = "-h"; - public static final String RUN_CMD = "run"; + static final String HELP_CMD = "-h"; + private static final String RUN_CMD = "run"; public static void main(String[] args) { + + LocaleInitializer.initLocale(); + JCommander jc = new JCommander(); // jc.addCommand(HELP_CMD, new HelpCommand()); RunCommand runCmd = new RunCommand(); diff --git a/src/main/java/org/utplsql/cli/LocaleInitializer.java b/src/main/java/org/utplsql/cli/LocaleInitializer.java new file mode 100644 index 0000000..596207b --- /dev/null +++ b/src/main/java/org/utplsql/cli/LocaleInitializer.java @@ -0,0 +1,58 @@ +package org.utplsql.cli; + +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** This class makes sure the java locale is set according to the environment variables LC_ALL and LANG + * We experienced that, in some cases, the locale was not set as expected, therefore this class implements some clear + * rules: + * 1. If environment variable LC_ALL is set, we try to parse its content and set locale according to its value if valid + * 2. If environment variable LANG is set, we try to parse its content and set locale according to its value if valid + * 3. Otherwise we use default locale + * + * @author pesse + */ +class LocaleInitializer { + + 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 + + /** Sets the default locale according to the rules described above + * + */ + static void initLocale() { + if ( !setDefaultLocale(System.getenv("LC_ALL"))) + setDefaultLocale(System.getenv("LANG")); + } + + /** Set the default locale from a given string like LC_ALL or LANG environment variable + * + * @param localeString Locale-string from LC_ALL or LANG, e.g "en_US.utf-8" + * @return true if successful, false if not + */ + private static boolean setDefaultLocale( String localeString ) { + if ( localeString == null || localeString.isEmpty() ) + return false; + + try { + Matcher m = REGEX_LOCALE.matcher(localeString); + if (m.find()) { + StringBuilder sb = new StringBuilder(); + sb.append(m.group(1)); + if (m.group(2) != null) + sb.append("-").append(m.group(2)); + + Locale l = new Locale.Builder().setLanguageTag(sb.toString()).build(); + if ( l != null ) { + Locale.setDefault(l); + return true; + } + } + } + catch ( Exception e ) { + System.out.println("Could not get locale from " + localeString); + } + + return false; + } +} From f66087528d5aa215b1f84ad262bab0ae8c71e792 Mon Sep 17 00:00:00 2001 From: pesse Date: Tue, 16 Jan 2018 09:46:13 +0100 Subject: [PATCH 2/3] Update documentation to describe localization --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 9c2a288..6359748 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,17 @@ You can also download all development versions from [Bintray](https://bintray.co The latest CLI is always compatible with all database frameworks of the same minor version. 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. +## Localization and NLS settings +utPLSQL-cli will use the environment variables "LC_ALL" or "LANG" to change the locale and therefore the NLS settings. +If neither environment variable is available, it will use the JVM default locale. + +Example: to change the NLS-settings to English American, you can do the following: +``` +export LC_ALL=en_US.utf-8 +``` + +The charset-part of LC_ALL is ignored. + ## Usage `utplsql run [-p=(ut_path|ut_paths)] [-f=format [-o=output_file] [-s] ...]` From 2e88a8c21c73977f7554ce20cd79ec1f303b5d17 Mon Sep 17 00:00:00 2001 From: pesse Date: Tue, 16 Jan 2018 14:19:13 +0100 Subject: [PATCH 3/3] Add NLS_LANG to the list of environment variables which can change the cli locale --- README.md | 2 +- .../java/org/utplsql/cli/LocaleInitializer.java | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6359748..2e07e01 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ The latest CLI is always compatible with all database frameworks of the same min 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. ## Localization and NLS settings -utPLSQL-cli will use the environment variables "LC_ALL" or "LANG" to change the locale and therefore the NLS settings. +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. If neither environment variable is available, it will use the JVM default locale. Example: to change the NLS-settings to English American, you can do the following: diff --git a/src/main/java/org/utplsql/cli/LocaleInitializer.java b/src/main/java/org/utplsql/cli/LocaleInitializer.java index 596207b..830c742 100644 --- a/src/main/java/org/utplsql/cli/LocaleInitializer.java +++ b/src/main/java/org/utplsql/cli/LocaleInitializer.java @@ -7,9 +7,10 @@ /** This class makes sure the java locale is set according to the environment variables LC_ALL and LANG * We experienced that, in some cases, the locale was not set as expected, therefore this class implements some clear * rules: - * 1. If environment variable LC_ALL is set, we try to parse its content and set locale according to its value if valid - * 2. If environment variable LANG is set, we try to parse its content and set locale according to its value if valid - * 3. Otherwise we use default locale + * 1. If environment variable NLS_LANG is set, we try to parse its content and set locale according to its value if valid + * 2. If environment variable LC_ALL is set, we try to parse its content and set locale according to its value if valid + * 3. If environment variable LANG is set, we try to parse its content and set locale according to its value if valid + * 4. Otherwise we use default locale * * @author pesse */ @@ -21,7 +22,12 @@ class LocaleInitializer { * */ static void initLocale() { - if ( !setDefaultLocale(System.getenv("LC_ALL"))) + + boolean localeChanged = setDefaultLocale(System.getenv("NLS_LANG")); + + if ( !localeChanged ) + localeChanged = setDefaultLocale(System.getenv("LC_ALL")); + if ( !localeChanged ) setDefaultLocale(System.getenv("LANG")); }