如何在 JVM 中设置默认语言环境?

我想将 JVM 的默认 Locale设置为 fr_CA。有哪些可能的选项可以做到这一点?

我知道只有一个选择 Locale.setDefault()

296578 次浏览

您可以通过 JVM 参数在命令行上设置它:

java -Duser.country=CA -Duser.language=fr ... com.x.Main

有关更多信息,请参见 < em > 国际化: 了解 Java 平台中的区域设置-使用区域设置

您可以使用 JVM args

java -Duser.country=ES -Duser.language=es -Duser.variant=Traditional_WIN

在这里的答案中,到目前为止,我们找到了两种改变 JRE 区域设置的方法:

  • 通过编程,使用 SetDefault ()(在我的例子中,这是解决方案,因为我不想要求用户的任何操作) :

    Locale.setDefault(new Locale("pt", "BR"));
    
  • Via arguments to the JVM:

    java -jar anApp.jar -Duser.language=pt-BR
    

But, just as reference, I want to note that, on Windows, there is one more way of changing the locale used by the JRE, as documented here: changing the system-wide language.

Note: You must be logged in with an account that has Administrative Privileges.

  1. Click Start > Control Panel.

  2. Windows 7 and Vista: Click Clock, Language and Region > Region and Language.

    Windows XP: Double click the Regional and Language Options icon.

    The Regional and Language Options dialog box appears.

  3. Windows 7: Click the Administrative tab.

    Windows XP and Vista: Click the Advanced tab.

    (If there is no Advanced tab, then you are not logged in with administrative privileges.)

  4. Under the Language for non-Unicode programs section, select the desired language from the drop down menu.

  5. Click OK.

    The system displays a dialog box asking whether to use existing files or to install from the operating system CD. Ensure that you have the CD ready.

  6. Follow the guided instructions to install the files.

  7. Restart the computer after the installation is complete.

Certainly on Linux the JRE also uses the system settings to determine which locale to use, but the instructions to set the system-wide language change from distro to distro.

您可以使用以下代码强制执行 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();

你可以这样做:

enter image description here

enter image description here

然后捕捉现场,你可以这样做:

private static final String LOCALE = LocaleContextHolder.getLocale().getLanguage()
+ "-" + LocaleContextHolder.getLocale().getCountry();

如果您不喜欢更改 System locale 而是更改 JVM,那么还有另一种方法。你可以设置一个系统(或用户)环境变量 JAVA_TOOL_OPTIONS,并将它的值设置为 -Duser.language=en-US或任何其他语言——区域。