j2ee國際化數據方式

靜態國際化數據,在msg(定義數據名)_zh(語言縮寫)_CN(國家縮寫).propertices文件中自定義相關數據java

而後進行調用jsp

  /*
     * 靜態國際化
     */
    @Test
    public void testInternational_static() throws Exception {
        //當前語言環境
        // Locale locale = Locale.CHINA;
        Locale locale = Locale.US;
        //建立工具類對象ResourceBundle
        ResourceBundle resourceBundle = ResourceBundle.getBundle("com.xinzhi.test.msg"
                , locale);
        String string = resourceBundle.getString("hello");
        System.out.println(string);
        
    }

下面是其餘類型數據的國際化方法工具

  /*
     * 數字國際化
     */
    @Test
    public void testInternational_number() throws Exception {
        // Locale locale = Locale.CHINA;
        Locale locale = Locale.US;
        NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
        String format = numberFormat.format(1000000000);
        System.out.println(format);
    }

    /*
     * 貨幣數字國際化
     */
    @Test
    public void testInternational_currency() throws Exception {
        // Locale locale = Locale.CHINA;
        Locale locale = Locale.US;
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
        String format = numberFormat.format(100);
        System.out.println(format);
    }

    /*
     * 貨幣數字計算$100*10
     */
    @Test
    public void testInternational_currency2() throws Exception {
        // Locale locale = Locale.CHINA;
        Locale locale = Locale.US;
        String currencyString = "$100";
        int num = 10;
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
        Number parse = numberFormat.parse(currencyString);
        System.out.println(parse.intValue() * num);
    }

    /*
     * 時間國際化
     */
    @Test
    public void testInternational_time() throws Exception {
        // Locale locale = Locale.CHINA;
        Locale locale = Locale.US;
        int dateStyle = DateFormat.FULL;
        int timeStyle = DateFormat.SHORT;
        DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(dateStyle,
                timeStyle, locale);
        String format = dateTimeInstance.format(new Date());
        System.out.println(format);
    }

 在jsp中使用國際化:spa

將資源文件在msg(定義數據名)_zh(語言縮寫)_CN(國家縮寫).propertices處理完成後3d

在須要國際化的jsp界面中插入以下代碼:code

<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>//用於加載jstl
<fmt:setLocale value="${pageContext.request.locale }"/>//設置使用的語言場景(在請求域中獲取)
<fmt:setBundle basename="com.xinzhi.utils.msg" var="varbundle"/>//拿到資源文件路徑,定義資源變量
<fmt:message key="title" bundle="${varbundle}"></fmt:message>//根據鍵來獲取相應的值

/**
* jsp中數字國際化,保留兩位有效數字,沒有的用0填充,用#.##則表示保留兩位數字可是不保留0
*/
<fmt:formatNumber pattern="0.00" value="100"></fmt:formatNumber>
/**
* jsp中日期國際化
*/
<fmt:formatDate value="2017-05-13" pattern="yyyy-MM-dd"/>
相關文章
相關標籤/搜索