java Maven 默认语言环境与操作系统语言环境不同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17774331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:56:52  来源:igfitidea点击:

Maven default locale not same with OS locale

javamavenlocalization

提问by Koray Tugay

When I type

当我打字

mvn --version

in command prompt I see:

在命令提示符中我看到:

Default Locale : en_US

默认语言环境:en_US

However my System Locale is tr_TR

但是我的系统区域设置是 tr_TR

When I start a Java SE Project without maven and run Locale.getDefault() tr_TR returns fine. But when I run a Maven project and then Locale.getDefault() it returns en_US which I do not like.

当我在没有 maven 的情况下启动 Java SE 项目并运行 Locale.getDefault() 时,tr_TR 返回正常。但是当我运行一个 Maven 项目然后 Locale.getDefault() 它返回我不喜欢的 en_US 。

How can I tell maven that my default locale is TR ?

我如何告诉 Maven 我的默认语言环境是 TR ?

回答by Oscerd

You can use this command

你可以使用这个命令

set MAVEN_OPTS= -Duser.language=tr

Anyway the best solution is to put these informations in the POM file and never by command line. In particular you have to deal with the configuration of Maven-Surefire-Plugin

无论如何,最好的解决方案是将这些信息放在 POM 文件中,而不是通过命令行。特别是你必须处理Maven-Surefire-Plugin的配置

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.9</version>
        <configuration>
            <systemPropertyVariables>
                <user.language>tr</user.language>
                <user.region>TR</user.region>
            </systemPropertyVariables>
        </configuration> 
    </plugin>

Second Question:Another question if I may, I am running a web app in my locale but it supports lets say german, english.. And your system locale is DE. Can I get your system locale from your request? Or maybe the language you prefer by your browser?

第二个问题:另一个问题,如果可以的话,我正在我的语言环境中运行一个网络应用程序,但它支持让我们说德语,英语......你的系统语言环境是 DE。我可以从您的请求中获取您的系统区域设置吗?或者也许是您的浏览器更喜欢的语言?

You can take these informations from the request. Here is an example in a servlet.

您可以从请求中获取这些信息。这是 servlet 中的一个示例。

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;

public class GetLocale extends HttpServlet{

  public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
  {
      Locale locale = request.getLocale();
      String language = locale.getLanguage();
      String country = locale.getCountry();

      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      out.println(language + ":" + country);
  }
}

回答by Sergio Lema

You can also use

你也可以使用

<properties>
    <argLine>-Duser.language=tr</argLine>
</properties>

This case works better when the argument is needed at the start of the JVM, as using maven-surefire-plugin, your JVM is already running and the arguments won't be reloaded (that was my case using some initialization with @RunWith(SpringRunner.class)and @SpringBootTestto initialize a MockMvc).

这种情况在 JVM 启动时需要参数时效果更好,因为使用maven-surefire-plugin,您的 JVM 已经在运行并且不会重新加载参数(这是我使用一些初始化@RunWith(SpringRunner.class)@SpringBootTest初始化 a 的情况MockMvc)。

回答by Mike Reiche

Defining the property allows you to pass them as command line argument.

定义属性允许您将它们作为命令行参数传递。

This works for me:

这对我有用:

<project>
    <properties>
        <user.language>de</user.language>
        <argLine>-Duser.language=${user.language}</argLine>
    </properties>
</project>
mvn -Duser.language=tr