字体差异可能会迫使您在不同的平台上使用不同的字符。然后,您可以使用variant代码定义Locale对象以识别这些差异。该variant代码不符合任何标准。它们是任意的,并且特定于您的应用程序。如果使用variant代码创建Locale对象,则只有您的应用程序才能知道如何处理它们。
在此示例中,我们没有演示为不同的平台使用不同的字体,而是将其简化为仅打印不同的消息。我们为每个平台创建了三个不同的资源包MessagesBundle_fr_FR_UNIX.properties,MessagesBundle_fr_FR_MAC.properties和MessagesBundle_fr_FR_WIN.properties。这些文件将为每个平台包含不同的消息。
package org.nhooo.example.util;
import java.util.Locale;
import java.util.ResourceBundle;
public class LocalePlatform {
public static void main(String[] args) {
ResourceBundle res;
String language = "fr";
String country = "FR";
//根据语言,国家/地区和变体构造语言环境。哪里变体
// 可以是变体供应商和浏览器特定的代码。
Locale unix = new Locale(language, country, "UNIX");
res = ResourceBundle.getBundle("MessagesBundle", unix);
System.out.println("UNIX: " + res.getString("birthday"));
Locale mac = new Locale(language, country, "MAC");
res = ResourceBundle.getBundle("MessagesBundle", mac);
System.out.println("MAC : " + res.getString("birthday"));
Locale windows = new Locale(language, country, "WIN");
res = ResourceBundle.getBundle("MessagesBundle", windows);
System.out.println("WIN : " + res.getString("birthday"));
}
}以下是资源束文件的内容:
MessagesBundle_fr_FR_UNIX.properties
birthday=Unix, Joyeux anniversaire à vous!
MessagesBundle_fr_FR_MAC.properties
birthday=Mac, Joyeux anniversaire à vous!
MessagesBundle_fr_FR_WIN.properties
birthday=Windows, Joyeux anniversaire à vous!