《JAVA WEB 的 I18N國際化》

1. I18N的概念

  • I18N 國際化(internationalization):軟件支持多語言,軟件會根據當前的語言使用環境,自動切換
  • I18N 由來:i和n 中間有18個字符

2. 資源文件準備

  • 使用properties文件
  • 配置文件的命名必須:基名_語言_國家. properties
  • 例如: system_zh_CN.properties、system_en_US.properties
注意:    1.基名隨便定義
         2.語言:代表不同國家的語言,比如en英文、zh中文
         3.國家使用大寫字母
  • 配置文件中寫上對應的key value
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述

3. java代碼中使用

        //Locale.getDefault() 獲取本地語言環境
        String username = ResourceBundle
                         .getBundle("system", Locale.getDefault())
                         .getString("username");
        System.out.println("username = " + username);
        
        //實際可根據以下判斷,決定向前端輸出那種語言
        boolean equals = Locale.getDefault().equals(Locale.CHINA);
        System.out.println("equals = " + equals);

        String string = ResourceBundle
                       .getBundle("system", Locale.US)
                       .getString("username");
        System.out.println("string = " + string);

        String ss = ResourceBundle
                   .getBundle("system", Locale.CHINA)
                   .getString("username");
        System.out.println("ss = " + ss);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章