Intellij IDEA Resource Bundle

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.propertiesfile_de.properties
其實從字面上理解就是資源包,爲了方便統一管理繁多的國際化文件

Resource Bundle的目錄結構
Resource Bundles目錄結構

只是在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爲例

創建Resource Bundle

點擊+號添加多個區域,這裏以添加zh和en爲例
添加區域

添加完成後,可以在Locales to add看到已經添加的區域
添加區域後

點擊OK生成Resource Bundle
生成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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章