jsp multi-language 多國語言

Posted in :

jsp 多國語言似乎滿簡單的。


地區標籤

https://openhome.cc/Gossip/Encoding/LocaleTag.html

先看看Java SE中,使用ResourceBundle時如何根據基礎名稱取得對應的訊息檔案:

  1. 使用指定的Locale物件取得訊息檔案
  2. 使用Locale.getDefault()取得的物件取得訊息檔案
  3. 使用基礎名稱取得訊息檔案

在JSTL中則略有不同,簡單地說,JSTL的i18n相容性標籤,會嘗試從屬性範圍中取得javax.servlet.jsp.jstl.fmt.LocalizationContext物件,藉以決定資源包與地區資訊,具體來說,決定訊息檔案的順序如下:

  1. 使用指定的Locale物件取得訊息檔案
  2. 根據瀏覽器Accept-Language標頭指定的偏好地區(Prefered locale)順序,這可以使用HttpServletRequest的getLocales()來取得
  3. 根據後備地區(fallback locale)資訊取得訊息檔案
  4. 使用基礎名稱取得訊息檔案

例如,訊息標籤 的範例並沒有指定Locale,而瀏覽器指定的偏好地區為”zh_TW”,所以會嘗試尋找messages3_zh_TW.properties檔案,結果沒有找到,而範例並沒有設置偏好地區,所以才尋找messages.properties檔案。


訊息標籤

https://openhome.cc/Gossip/Encoding/MessageTag.html

要使用JSTL的i18n相容格式標籤庫,必須在JSP網頁上使用taglib指示元素定義前置名稱與uri參考,慣例上使用i18n相容格式標籤庫時,會使用fmt作為前置名稱,JSTL 1.1格式標籤庫的uri參考則為http://java.sun.com/jsp/jstl/fmt。例如:
<%@taglib prefix=”fmt” uri=”http://java.sun.com/jsp/jstl/fmt”%>

首先來看到最基本的<fmt:bundle>、<fmt:message>如何使用。


Step 1:

準備了一個messages.properties,檔案必須放在Web應用程式的/WEB-INF/classes,檔案內容如下:

app.Configuration=Configuration

Step 2:

JSP檔案:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd">
<fmt:bundle basename="messages" />
<html>
    <head>
        <meta http-equiv="Content-Type"
               content="text/html; charset=UTF-8">
        <title><fmt:message key="app.Configuration" /></title>
    </head>
</html>

這樣就可以套到多國語言的字串。請服用上方紅色的部份。


如果訊息中有些部份必須動態決定,則可以使用佔位字符先代替,例如:

app.title=Hello
app.forUser=Hi! {0}! It is {1, date, long} and {2, time ,full}. 

在上面的訊息檔案中,粗體字部份就是佔位字符,號碼從0開始,分別代表第幾個佔位字符,在指定時可以指定型態與格式,所使用的格式是由java.text.MessageFormat所定義,可參考java.text.MessageFormat的API文件說明。

 

如果需要使用 java 來取得,請服用這一個block:

You need to use I18N and L18N in your JSP code :

<%@ page import="java.io.*,java.util.Locale" %>

Get the Locale from the request:

 Locale locale = request.getLocale();

And apply the DateFormat based on Locale.

DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
String formattedDate = df.format(yourDate);

To get the actual format for a Locale , you can try something like this :

SimpleDateFormat sf = (SimpleDateFormat) df;
String pattern = sf.toLocalizedPattern();

Using JSTL’s formatDate :

When you use JSTL format tag <fmt:formatDate> and <fmt:formatNumber>, JSTL automatically takes care of locale resolution. Depending on the browser’s locale setting JSTL will display the date and numbers.

<fmt:formatDate value="${date}" type="both" dateStyle="full" timeStyle="short" />

 


圖文教學:

https://blog.cedric.ws/multiple-languages-for-your-java-application

Creating a Language Controller

We will create a new java class in a new package and call it LanguagesController.java. This class will include one method: getWord.


package be.tiwi.vop.team11.properties;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

/**
 *
 * @author cedric
 */
public class LanguagesController {

    private Map supportedLanguages;
    private ResourceBundle translation;

    public LanguagesController(String language){
        Locale Dutch = new Locale("Dutch","België","nl");
        supportedLanguages = new HashMap();
        supportedLanguages.put("French",Locale.FRENCH);
        supportedLanguages.put("Dutch", Dutch);
        supportedLanguages.put("English",Locale.ENGLISH); 
        translation = ResourceBundle.getBundle("languages", supportedLanguages.get(language));
    }

    public String getWord(String keyword)
    {
        return translation.getString(keyword);
    }

}

Next we can create a start up class that will select the keyword from the selected properties file.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.tiwi.vop.team11.properties;

/**
 *
 * @author cedric
 */
public class LanguagesStartup {

    public static void main(String args[]){
        LanguagesController langController_nl = new LanguagesController("Dutch");
        System.out.println(langController_nl.getWord("Cedric"));
        LanguagesController langController_en = new LanguagesController("English");
        System.out.println(langController_en.getWord("Cedric"));
    }

}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *