我想将我的JVM的默认语言环境设置为fr_CA。有什么可能的选择呢?
我知道只有一个选项Locale.setDefault()
应用程序的默认语言环境以三种方式确定。首先,除非您明确更改了缺省值,否则
Locale.getDefault()
方法将返回Java虚拟机(JVM)在首次加载时最初确定的区域设置。也就是说,JVM从主机环境确定默认语言环境。主机环境的区域设置由主机操作系统和在该系统上建立的用户首选项确定。其次,在某些Java运行时实现上,应用程序用户可以通过设置
user.language
,user.country
和user.variant
系统属性在命令行上提供此信息来覆盖主机的默认语言环境。第三,您的应用程序可以调用
Locale.setDefault(Locale)
方法。 setDefault(Locale aLocale)方法允许您的应用程序设置系统范围(实际上是VM范围的)资源。使用此方法设置默认语言环境后,对Locale.getDefault()的后续调用将返回新设置的语言环境。
您可以通过JVM参数在命令行上进行设置:
java -Duser.country=CA -Duser.language=fr ... com.x.Main
有关更多信息,请查看Internationalization: Understanding Locale in the Java Platform - Using Locale
您可以使用JVM args
java -Duser.country=ES -Duser.language=es -Duser.variant=Traditional_WIN
在这里的答案中,到目前为止,我们找到了两种更改JRE语言环境设置的方法:
Locale.setDefault(new Locale("pt", "BR"));
但是,作为参考,我想要注意的是,在Windows上,还有一种方法可以更改JRE使用的语言环境,如java -jar anApp.jar -Duser.language=pt-BR
所述:更改系统范围的语言。
注意:您必须使用具有管理权限的帐户登录。
- 单击开始>控制面板。
- Windows 7和Vista:单击时钟,语言和区域>区域和语言。 Windows XP:双击“区域和语言选项”图标。 将出现“区域和语言选项”对话框。
- Windows 7:单击“管理”选项卡。 Windows XP和Vista:单击“高级”选项卡。 (如果没有“高级”选项卡,则表示您未使用管理权限登录。)
- 在“非Unicode程序的语言”部分下,从下拉菜单中选择所需的语言。
- 单击确定。 系统显示一个对话框,询问是使用现有文件还是从操作系统CD安装。确保准备好CD。
- 按照指导说明安装文件。
- 安装完成后重新启动计算机。
当然,在Linux上,JRE还使用系统设置来确定要使用的语言环境,但是使用说明来设置从发行版到发行版的系统范围语言更改。
您可以使用以下代码为here文件强制执行VM参数:
JAR
它使用如下:
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());
}
}
}
如果您不想更改系统区域设置而是更改JVM,那么还有另一种选择。您可以设置系统(或用户)环境变量private static final String LOCALE = LocaleContextHolder.getLocale().getLanguage()
+ "-" + LocaleContextHolder.getLocale().getCountry();
并将其值设置为JAVA_TOOL_OPTIONS
或您想要的任何其他语言区域。