Resource Bundle是什麼
可以直接通過官方的描述來了解 https://www.jetbrains.com/hel...
Resource bundle is a set of properties files that have same base name with different language-specific suffixes. A resource bundle contains at least two properties files with similar base name, for example file_en.properties and file_de.properties.
官方的描述是一堆有着相同前綴名稱但有着不同語言後綴名稱的屬性文件的集合,且至少包含2個有着相似前綴名稱的屬性文件,例如file_en.properties
和file_de.properties
其實從字面上理解就是資源包,爲了方便統一管理繁多的國際化文件
Resource Bundle的目錄結構
只是在Intellij IDEA內顯示上多了一層名爲Resources的Resource Bundle目錄,但在實際物理目錄下Resources*.properties
等文件仍在java-calculator目錄下
創建Resource Bundle
-
Alt + 1
打開項目工具窗口,選中要創建Resource Bundle的目錄 -
Alt + Insert
- Resource Bundle
或者主菜單 - File - New - Resource Bundle
或者當前目錄右鍵 - New - Resource Bundle
來打開創建Resource Bundle的對話框 - 填寫Resource Bundle的基礎名稱
勾選User XML-based properties files則會創建XML格式的屬性文件
Project locale表示項目裏已經存在的區域
Locales to add表示添加相應的區域,添加右邊的+
號即可添加,多個區域用,
隔開 - 以創建一個基礎名稱爲demo的Resource Bundle爲例
點擊+
號添加多個區域,這裏以添加zh和en爲例
添加完成後,可以在Locales to add看到已經添加的區域
點擊OK生成Resource Bundle
合併或拆分Resource Bundle
- 添加新的屬性文件:直接在Resource Bundle 'demo'目錄右鍵 -
Add Property Files to Resource Bundle
,點擊+
添加新的區域即可生成新的屬性文件 - 拆分:如果不想使用Resource Bundle管理屬性文件,可以在Resource Bundle目錄右鍵 -
Dissociate Resource Bundle 'demo'
- 合併:在同一個目錄下創建多個符合相同前綴、不同語言後綴名稱的屬性文件時,Intellij IDEA會自動創建Resource Bundle管理這些文件
也可以手動組合屬性文件,按住Ctrl + 鼠標左鍵
選中多個屬性文件後右鍵 -Combine to Resource Bundle