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