java 国际化

package com.jeecms.test;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

public class Test1 {
public static void main(String[] args) {
        
        //返回所有已安装语言环境的数组
        Locale [] locals = Locale.getAvailableLocales();
        for (Locale locale : locals) {
            //国家
            System.out.println(locale.getDisplayCountry()+":"+locale.getCountry());
        }
        for (Locale locale : locals) {
            //语言
            System.out.println(locale.getDisplayLanguage()+":"+locale.getLanguage());
        }
        
        
        //Java 虚拟机实例的当前默认语言环境值
        //Locale local =  Locale.US;  //指定国家代替默认,读出就是英文的资源文件
        Locale local =  Locale.getDefault();
        System.out.println(local.getDisplayCountry()+":"+local.getCountry());
        
        
        //info为国际化文件名,装入本地local,国际化文件key是hello
        //info_en_US.properties   -> hello=hello
        //info_zh_CN.properties      -> hello=/u4F60/u597D
        ResourceBundle sources = ResourceBundle.getBundle("info", local);
        String value = sources.getString("hello");
        System.out.println(value);
        
        //hello=hello {0} ,内参数替换,重0开始  {0} -> 北京
        value = MessageFormat.format(value, new Object[]{"北京"});
        System.out.println(value);
    }
}

你可能感兴趣的:(java 国际化)