2010-04-12

Java 的 Internationalization 與 Localization (Internationalization and Localization in Java)

在 Test.java 中編寫
Create a file call Test.java and type

import java.util.ResourceBundle;
import java.util.Locale;

public class Test {
    public static void main(String[] args){
        System.out.println(ResourceBundle.getBundle("messages").getString("greetings"));
        System.out.println(ResourceBundle.getBundle("messages", new Locale("a", "b")).getString("greetings"));
    }
}

編寫 messages.properties 檔案
greetings=hello, world
Create a messages.properties file
and type greetings=hello, world

編寫 messages_a_b.properties 檔案
greetings=\u4f60\u597d\uff0c\u4e16\u754c
Create a messages_a_b.properties file
and type greetings=\u4f60\u597d\uff0c\u4e16\u754c

Java 的 I18N 編程有某程度上的製作限制, 必須要與 properties 檔案合拼使用
I18N of Java must build with properties file

properties 檔案的編寫方法相當簡單:
properties 檔案在 Windows 中必須以 ANSI 或不含 BOM 的 UTF-8 編碼編寫
= 號左右兩邊不能有任何空白字元, 必須與屬性名稱及為屬性資料相連
左邊為屬性名稱, 必須於每行的第一列開始, 屬性名稱宣告限制與 Java 相同
右邊為屬性資料, 特殊字元及非 ASCII 字元必須以 \uxxxx 這種十六進制格式
在 NetBeans 中編制 properties 檔案時, 會自動將非 ASCII 字元轉成 \uxxxx 格式
properties file is easy to create:
In Windows, properties file must create with ANSI or UTF-8 without BOM
the = without any whitespace
the left hand side of = is the name of attribute, it must not have any whitespace at the beginning
the right hand side of = is the value of attribute, all special characters and non-ASCII characters must type with \uxxxx hexadecimal format
In NetBeans, build up a properties file will convert all special characters and non-ASCII characters automatically

Java 方面
ResourceBundle.getBundle 所接收的字串是指 properties 檔案的起首字
如這裡所表示的 messages 其實是指 messages.properties 的 messages
getString 所接收的字串是指 properties 檔案的屬性名稱
如這裡所表示的 greetings 其實是指 messages.properties 的 greetings 屬性
Locale 所接收的兩組字串其實是指語言及地區
如這裡所表示的 a b 其實提指 messages_a_b.properties 的 a 及 b
編制時必須要提供預設語言集, 如此例子的 messages.properties
之後便可以製作其他語言,例如:
messages_a_b.properties
messages_c_d.properties
messages_e_f.properties
......
製作一堆語言集後, 只要使 new Locale() 選擇合適的語言便可輸出對應的文字
需要添加新語言集時只需要製作新的 .properties 檔案而不需要修改 Java
In Java
ResourceBundle.getBundle can retrieve all attributes in properties file
The example "messages" that means the "message" of messages.properties
getString retrieve the specific name of attribute
The example "greetings" that means the attribute named "greetings" in messages.properties
The Constructor of Locale to retrieve language and locale
The example "a" and "b" is the "a" and "b" of messages_a_b.properties
You must create a default language, for example messages.properties
then you can create other language, such as
messages_a_b.properties
messages_c_d.properties
messages_e_f.properties
......
After create a list of language, You can use new Locale() to select a suitable language
If you would like add more languages, just create another language .properties files, and you don't need to modify your Java source code

但若沒有 NetBeans 可利用下面所提供的 XHTML + JavaScript 程式來轉換
這程式是符合 W3C 的標準, 相信可以在任何瀏覽器中使用
If you don't have NetBeans, you can copy the source code below which is a XHTML + JavaScript program
This program validate with W3C standard, it could run in any web browsers

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" />
    <title>Character Converter</title>
    <script type = "text/javascript">
<!--
function convert(){
    inputValue = document.getElementById("input").value;
    outputValue = "";
    for (i = 0; i < inputValue.length; i++){
        outputValue += "\\u" + padZeroes(inputValue.charCodeAt(i).toString(16));
    }
    document.getElementById("output").innerHTML = outputValue;
}

function padZeroes(string){
    while (string.length < 4){
        string = "0" + string;
    }
    return string;
}
// -->
    </script>
    </head>
    <body>
        <p><textarea id = "input" rows = "10" cols = "60"></textarea></p>
        <p><input type = "button" value = "Convert" onclick = "convert();" /></p>
        <div id = "output" style = "width: 100%; height: 600px; overflow: auto"></div>
    </body>
</html>

沒有留言 :

張貼留言