Java 國際化(一)
1.Java對國際化的支援主要用3個類另外完成
java.util.Locale 對應一個特定的區域和語言環境
Locale類主要用來構建本地化應用,該類的主要包含,對地理區域的區域性特徵的封裝,Local提供了一些常量,可以使用這些常量為通常使用的語言環境建立Locale物件。例如,為中國應用環境建立一個Locale物件
Locale.CN.
java.util.ResourceBundle使用者載入資源包
Locale locale=new Locale("zn","CN","WIN");
ResourceBundle myResource=ResourceBundle.getBundle("Resources",locale);
java.test.MessageFormat 用於見訊息格式化。
如果資原始檔中包含動態內容,那麼動態內容就要需要使用佔位符表示,而載入資原始檔是,必須使用MessageFormat類,該類中共有一個靜態方法format(),呼叫方式入校:
MessageFormat.format(String pattern,Object values...);
其中 引數pattern是包含佔位符的字串,後面的多個引數將依次填充字串中的佔位符。
例項:
MessageFormat_zh_CN.properties
MessageFormat_en_US.properties
MessageFormatThree.java
1.使用類實現 國際化
注意命名
2.使用資原始檔 帶佔位符
注意命名
MessageFormat_en_US.properties
MessageFormat_zh_CN.properties
3.Loacle 中顯示java支援的 地區和語言
java.util.Locale 對應一個特定的區域和語言環境
Locale類主要用來構建本地化應用,該類的主要包含,對地理區域的區域性特徵的封裝,Local提供了一些常量,可以使用這些常量為通常使用的語言環境建立Locale物件。例如,為中國應用環境建立一個Locale物件
Locale.CN.
java.util.ResourceBundle使用者載入資源包
Locale locale=new Locale("zn","CN","WIN");
ResourceBundle myResource=ResourceBundle.getBundle("Resources",locale);
java.test.MessageFormat 用於見訊息格式化。
如果資原始檔中包含動態內容,那麼動態內容就要需要使用佔位符表示,而載入資原始檔是,必須使用MessageFormat類,該類中共有一個靜態方法format(),呼叫方式入校:
MessageFormat.format(String pattern,Object values...);
其中 引數pattern是包含佔位符的字串,後面的多個引數將依次填充字串中的佔位符。
例項:
MessageFormat_zh_CN.properties
MessageFormat_en_US.properties
MessageFormatThree.java
1.使用類實現 國際化
import java.util.Locale;
import java.util.ResourceBundle;
public class Secondi18n {
/**
* @param args
*/
public static void main(String[] args) {
Locale locale=null;
if(args.length==2){
locale=new Locale(args[0],args[1]);
}
else{
locale=Locale.getDefault();
}
//資源類 的命名 用Resources開頭
ResourceBundle bundle=ResourceBundle.getBundle("Resources", locale);
System.out.println(bundle.getString("userName"));
System.out.println(bundle.getString("userPassword"));
System.out.println(bundle.getString("userAge"));
}
}
注意命名
import java.util.ListResourceBundle;
public class Resources_en_US extends ListResourceBundle {
static final Object[][] contents=new String[][]{
{"userName","Name"},
{"userPassword","Password"},
{"userAge","age"}};
@Override
protected Object[][] getContents() {
// TODO Auto-generated method stub
return contents;
}
}
import java.util.ListResourceBundle;
public class Resources_zh_CN extends ListResourceBundle {
static final Object[][] contents=new String[][]{
{"userName","姓名"},
{"userPassword","密碼"},
{"userAge","年齡"}};
@Override
protected Object[][] getContents() {
// TODO Auto-generated method stub
return contents;
}
}
2.使用資原始檔 帶佔位符
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
public class MessageFormatThree {
/**
* @param args
*/
public static void main(String[] args) {
Locale locale=null;
if(args.length==2){
locale=new Locale(args[0],args[1]);
}
else{
locale=Locale.getDefault();
}
//資源屬性檔案 用 MessageFormat 開頭
ResourceBundle bundle=ResourceBundle.getBundle("MessageFormat", locale);
String userName=bundle.getString("userName2");
System.out.println(MessageFormat.format(userName, "xue",new Date()));
}
}
注意命名
MessageFormat_en_US.properties
userName=Name userPassword=Password userName2={0},Today is {1}
MessageFormat_zh_CN.properties
userName=姓名
userPassword=密碼
userName2={0},今天{1}
3.Loacle 中顯示java支援的 地區和語言
import java.util.Locale;
public class locale {
/**
* @param args
*/
public static void main(String[] args) {
Locale[] list = Locale.getAvailableLocales();
System.out.println("中文地區————地區簡寫——————語言中文——————語言簡寫");
for (int i = 0; i < list.length; i++) {
System.out.println(list[i].getDisplayCountry()+"————"+list[i].getCountry()+"——————"+list[i].getDisplayLanguage()+"——————"+list[i].getLanguage());
}
}
}
相關文章
- Java中的國際化Java
- 後端國際化(一): kiwi-java簡介後端Java
- Java模組化的國際化實現- GunnarJava
- 國際化
- Android國際化Android
- 國際化思考
- 【spring 國際化】springMVC、springboot國際化處理詳解SpringMVCSpring Boot
- Spring Webflux國際化SpringWebUX
- .net core國際化
- .Net Core 國際化
- 一種國際化Qt應用程式的方法QT
- Spring-boot國際化Springboot
- Grails國際化例項AI
- jeecg 國際化處理
- Yii2-國際化
- QT6 國際化QT
- SpringBoot專案國際化Spring Boot
- Webnovel 國際化實踐Web
- springboot頁面國際化Spring Boot
- WEB網站國際化的一種解決方案Web網站
- struts2 國際化(本地化)
- Django國際化與本地化指南Django
- vux UI 專案國際化UXUI
- ?HeyUI完成國際化支援啦????UI
- 對國際化 i18n 專案的一點思考
- 小程式國際化實現方式
- 國際化方案(1)- 多語言
- Flutter 國際化適配實戰Flutter
- SpringBoot-頁面國際化(五)Spring Boot
- 實用的 Flutter 國際化指南Flutter
- MasaFramework -- i18n (國際化)Framework
- 一步一步搭建Flutter開發架子-國際化,路由,本地化,響應式Flutter路由
- 記一次大型React專案的國際化方案探索React
- 精讀《國際化佈局 – Logical Properties》
- 微軟Cloud+AI國際化指南微軟CloudAI
- iOS國際化(多語言)漫談iOS
- Flutter實戰之主題、國際化篇Flutter
- 基於SpringBoot的國際化應用Spring Boot
- springboot、Thymeleaf、國際化的簡單使用Spring Boot