I need to change my Java locale to another language system-wide for work purposes, but there seems to be no easy way to do it by default. I do development for CJK applications, but changing my actual system locale to match also renames my home folders, meaning if my input method decides to stop working I would have to reboot my entire system.
I've tried setting JVM arguments (-Duser.language=ja -Duser.locale=JP) on
{$project_dir}/.mvn/jvm.config (not ideal)JAVA_ARGS (under /etc/environment)MAVEN_OPTSbut none of them work. Pretty much at my wits end here.
To be clear I'm looking for a solution with the following criteria:
.mvnen_US)So basically some kind of environment variable.
The JVM maintains its own current default locale. This behavior is required by the Java specifications.
Typically a JVM implementation detects the host OS default when the JVM starts up, and uses that as its own default. Later changing the host OS’ current default has no effect on the JVM. This behavior is not specified in the Java specs.
You can typically override that behavior by specifying a default locale on the command-line used to launch the JVM. You said you tried this, but did not explain why this is not a solution. This behavior is not specified in the Java specs.
You could externally monitor and manipulate the locale within Java by opening some communication path.
JMX springs to mind.
When your app starts, it could look in an external source for an idea of what locale to use, then make that a singleton within your app.
An LDAP server is one such external place to hold such values. You would use JNDI in Java to access the server and retrieve the value.
Locale.setDefaultCalling Locale.setDefault immediately affects all code in all apps within the JVM. But this does not persist. You must call again when relaunching your app or JVM.
Any other code can call this as well as your code, making it unreliable. I do not recommend this approach.
You can hard-code the desired Locale by declaring a static class variable.
static final public Locale LOCALE = Locale.ITALY ;
In the lifecycle code called when your app launches, call Locale.setDefault( SomeClass.LOCALE ) ;.
Locale object in your code➥ I suggest always passing Locale explicitly as the optional argument in the various places you care about.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
Locale locale = Locale.CANADA_FRENCH ; // Pass explicitly your desired/expected `Locale`.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( locale ) ;
String output = today.format( f ) ;
mardi 11 juin 2019
Depending on the JVM’s current default locale is unreliable. As a programmer you are depending on externalities you cannot control.
Locale.setDefault immediately affects your code. By the way, same goes for time zone, ZoneId. Better to always pass explicitly your desired/expected time zone.
Also, if the locale or time zone is crucial, best to confirm with the user.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With