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

时间:2012-01-10 19:13:29

标签: java localization globalization

我想将我的JVM的默认语言环境设置为fr_CA。有什么可能的选择呢?

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

7 个答案:

答案 0 :(得分:139)

来自Oracle Reference

  

应用程序的默认语言环境以三种方式确定。   首先,除非你明确更改了默认值,否则   Locale.getDefault()方法返回最初确定的语言环境   由Java虚拟机(JVM)首次加载时。那就是   JVM确定主机环境的默认语言环境。主人   环境的语言环境由主机操作系统决定   在该系统上建立的用户首选项。

     

其次,在某些Java运行时实现上,应用程序用户可以   通过提供此信息来覆盖主机的默认语言环境   命令行设置user.languageuser.country和   user.variant系统属性。

     

第三,您的应用程序可以调用Locale.setDefault(Locale)   方法。 setDefault(Locale aLocale)方法允许您的应用程序   设置系统范围的(实际上是VM范围的)资源。使用此设置默认语言环境后   方法,后续调用Locale.getDefault()将返回新的   设置语言环境。

答案 1 :(得分:134)

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

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

有关详细信息,请查看Internationalization: Understanding Locale in the Java Platform - Using Locale

答案 2 :(得分:36)

您可以使用JVM args

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

答案 3 :(得分:24)

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

  • 以编程方式,使用Locale.setDefault()(在我的情况下,这是解决方案,因为我不想要求用户执行任何操作):

    Locale.setDefault(new Locale("pt", "BR"));
    
  • 通过JVM的参数:

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

但是,作为参考,我想要注意的是,在Windows上,还有一种更改JRE使用的语言环境的方法,如文档here:更改系统范围的语言。

  

注意:您必须使用具有管理权限的帐户登录。

     
      
  1. 点击开始>控制面板

  2.   
  3. Windows 7和Vista:点击时钟,语言和区域> 地区和语言

         

    Windows XP:双击区域和语言选项   图标。

         

    出现区域和语言选项对话框。

  4.   
  5. Windows 7:点击管理标签。

         

    Windows XP和Vista:点击高级标签。

         

    (如果没有“高级”选项卡,则表示您尚未登录   行政特权。)

  6.   
  7. 非Unicode程序的语言部分下,从下拉菜单中选择所需的语言。

  8.   
  9. 点击确定

         

    系统显示一个对话框,询问是否使用现有的   文件或从操作系统CD安装。确保你有   CD准备就绪。

  10.   
  11. 按照指导说明安装文件。

  12.   
  13. 安装完成后重启计算机。

  14.   

当然,在Linux上,JRE还使用系统设置来确定要使用的语言环境,但是使用说明来设置从发行版到发行版的系统范围语言。

答案 4 :(得分:1)

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

答案 5 :(得分:1)

您可以这样做:

enter image description here

enter image description here

并捕获语言环境。您可以这样做:

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

答案 6 :(得分:1)

如果您不想更改系统语言环境,但可以更改JVM,那么还有另一件事。您可以设置系统(或用户)环境变量JAVA_TOOL_OPTIONS并将其值设置为-Duser.language=en-US或所需的任何其他语言区域。

相关问题