2013-07-21 243 views
5

當我鍵入Maven的默認語言環境不一樣與操作系統語言環境

mvn --version 

在命令提示符下我明白了:

默認區域:EN_US

但是我的系統區域tr_TR

當我啓動一個沒有maven的Java SE項目並運行Locale.getDefault()tr_TR回報良好。但是,當我運行Maven項目,然後Locale.getDefault()它返回我不喜歡的en_US。

如何告訴maven我的默認語言環境是TR?

回答

9

您可以使用此命令

set MAVEN_OPTS= -Duser.language=tr 

但無論如何,最好的解決辦法是把這些信息在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> 

配置第二個問題: 如果我可以,我跑在我的語言環境的web應用程序,但它支持的另一種問題,可以說,德國,英語..並且您的系統語言環境是DE。我可以從您的請求中獲取系統區域設置嗎?或者,也許你的瀏覽器喜歡的語言?

您可以從請求中獲取這些信息。這是一個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); 
    } 
} 
+0

但是如果我將該項目部署到某個其他語言環境的計算機上呢?有沒有辦法讓Maven直接使用OS Locale? –

+0

我需要進行一些搜索。我從來沒有遇到過這個問題。 – Oscerd

+0

我已使用Google搜索,但無法找到信息。我認爲沒有辦法告訴Maven直接使用OS語言環境。 – Oscerd