12

When I type

mvn --version

in command prompt I see:

Default Locale : en_US

However my System Locale is 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.

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

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

3 Answers3

21

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

    <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?

You can take these informations from the request. Here is an example in a 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);
  }
}
Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
Oscerd
  • 1,616
  • 11
  • 14
  • But what if I deploy the project to some other computer with some other Locale? Is there no way to tell Maven to use to OS Locale directly? – Koray Tugay Jul 21 '13 at 17:12
  • I need to make some searches. I've never faced this problem. – Oscerd Jul 21 '13 at 17:31
  • I've googled but I can't find informations. I think there is no way to tell Maven to use OS locale directly. – Oscerd Jul 21 '13 at 18:30
  • 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? – Koray Tugay Jul 21 '13 at 18:42
  • Note: I had to specify user.timezone along with language and region to get the full settings for the locale. – Raj Feb 24 '19 at 07:22
  • For me `systemPropertyVariables` works only when surefire is configured to run test in fork mode. When run in non-fork mode, `Locale` class is already initialized with "default" locale during JVM startup and cannot be overriden. – bedla.czech Sep 24 '20 at 11:16
1

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 @SpringBootTest to initialize a MockMvc).

Sergio Lema
  • 1,491
  • 1
  • 14
  • 25
  • Thanks! I'd like to add that I used _argLine_ inside of **configuration**. The docs has a special section for that: [Special VM Properties](https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html). – Marcelo Barros Jun 24 '19 at 22:28
0

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
Mike Reiche
  • 382
  • 3
  • 12