Locale
设置为fr_CA
,有哪些可能的选项来完成这个任务?我只知道一种选项
Locale.setDefault()
。Locale
设置为fr_CA
,有哪些可能的选项来完成这个任务?Locale.setDefault()
。java -Duser.country=CA -Duser.language=fr ... com.x.Main
来自Oracle参考文档:
应用程序的默认区域设置是通过三种方式确定的。 首先,除非您明确更改了默认设置,否则
Locale.getDefault()
方法将返回最初由Java虚拟机(JVM)在首次加载时确定的区域设置。 也就是说,JVM从主机环境中确定默认的区域设置。主机环境的区域设置是由主机操作系统和在该系统上建立的用户首选项确定的。其次,在某些Java运行时实现中,应用程序用户可以通过在命令行上设置
user.language
、user.country
和user.variant
系统属性来覆盖主机的默认区域设置。第三,您的应用程序可以调用
Locale.setDefault(Locale)
方法。 setDefault(Locale aLocale)方法使您的应用程序设置一个系统范围的(实际上是VM范围的)资源。使用此方法设置默认区域设置后,对Locale.getDefault()的后续调用将返回新设置的区域设置。
至今为止,在这里的答案中,我们发现更改JRE区域设置的两种方法:
通过编程方式,使用Locale.setDefault()(在我的情况下,这是解决方案,因为我不想要求用户采取任何操作):
Locale.setDefault(new Locale("pt", "BR"));
通过JVM参数:
java -jar anApp.jar -Duser.language=pt-BR
但是,仅供参考,我想指出,在Windows上,还有一种改变JRE使用的区域设置的方法,如此处所述:更改系统范围的语言。
注意:您必须使用具有管理员权限的帐户登录。
单击开始>控制面板。
Windows 7和Vista:单击“时钟、语言和区域”>区域和语言。
Windows XP:双击区域和语言选项图标。
弹出区域和语言选项对话框。
Windows 7:单击管理选项卡。
Windows XP和Vista:单击高级选项卡。
(如果没有高级选项卡,则您没有使用管理员特权登录。)
在非Unicode程序的语言部分中,从下拉菜单中选择所需的语言。
单击确定。
系统会显示一个对话框,询问是否使用现有文件还是从操作系统CD安装。请确保您已准备好CD。
按照指示进行文件安装。
安装完成后重新启动计算机。
当然,在Linux上,JRE也使用系统设置来确定要使用哪个区域设置,但设置系统范围的语言的指令因发行版而异。
你可以使用JVM参数
java -Duser.country=ES -Duser.language=es -Duser.variant=Traditional_WIN
JAR
文件强制执行VM参数:import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
public class JVMArgumentEnforcer
{
private String argument;
public JVMArgumentEnforcer(String argument)
{
this.argument = argument;
}
public static long getTotalPhysicalMemory()
{
com.sun.management.OperatingSystemMXBean bean =
(com.sun.management.OperatingSystemMXBean)
java.lang.management.ManagementFactory.getOperatingSystemMXBean();
return bean.getTotalPhysicalMemorySize();
}
public static boolean isUsing64BitJavaInstallation()
{
String bitVersion = System.getProperty("sun.arch.data.model");
return bitVersion.equals("64");
}
private boolean hasTargetArgument()
{
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
List<String> inputArguments = runtimeMXBean.getInputArguments();
return inputArguments.contains(argument);
}
public void forceArgument() throws Exception
{
if (!hasTargetArgument())
{
// This won't work from IDEs
if (JARUtilities.isRunningFromJARFile())
{
// Supply the desired argument
restartApplication();
} else
{
throw new IllegalStateException("Please supply the VM argument with your IDE: " + argument);
}
}
}
private void restartApplication() throws Exception
{
String javaBinary = getJavaBinaryPath();
ArrayList<String> command = new ArrayList<>();
command.add(javaBinary);
command.add("-jar");
command.add(argument);
String currentJARFilePath = JARUtilities.getCurrentJARFilePath();
command.add(currentJARFilePath);
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.start();
// Kill the current process
System.exit(0);
}
private String getJavaBinaryPath()
{
return System.getProperty("java.home")
+ File.separator + "bin"
+ File.separator + "java";
}
public static class JARUtilities
{
static boolean isRunningFromJARFile() throws URISyntaxException
{
File currentJarFile = getCurrentJARFile();
return currentJarFile.getName().endsWith(".jar");
}
static String getCurrentJARFilePath() throws URISyntaxException
{
File currentJarFile = getCurrentJARFile();
return currentJarFile.getPath();
}
private static File getCurrentJARFile() throws URISyntaxException
{
return new File(JVMArgumentEnforcer.class.getProtectionDomain().getCodeSource().getLocation().toURI());
}
}
}
JVMArgumentEnforcer jvmArgumentEnforcer = new JVMArgumentEnforcer("-Duser.language=pt-BR"); // For example
jvmArgumentEnforcer.forceArgument();