Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven default locale not same with OS locale

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 ?

like image 655
Koray Tugay Avatar asked Jul 21 '13 16:07

Koray Tugay


People also ask

How does JVM determine default locale?

That is, the JVM determines the default locale from the host environment. The host environment's locale is determined by the host operating system and the user preferences established on that system.

How do I change the default locale in JVM?

In Java, we can use Locale. setDefault() to change the JVM default locale. Alternatively, in the command line, we can configure the user. country and user.

What is root locale?

The root locale is the locale whose language, country, and variant are empty ("") strings. This is regarded as the base locale of all locales, and is used as the language/country neutral locale for the locale sensitive operations.


2 Answers

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);
  }
}
like image 103
Oscerd Avatar answered Oct 17 '22 02:10

Oscerd


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).

like image 1
Sergio Lema Avatar answered Oct 17 '22 02:10

Sergio Lema