Where to place i18n key strings in Java

前端 未结 8 2817
不思量自难忘°
不思量自难忘° 2021-02-20 04:10

When doing internationalization in Java, you assign a string key to each message. What\'s the best practice, on where to place those string keys. Goal is to allow easy refactori

8条回答
  •  青春惊慌失措
    2021-02-20 04:26

    String constants are the way to go. Where you define them, it really depends on your code structure and usage of keys. For example:

    • If you only use the keys in one class it's best to put them there.
    • If you re-use the same keys across your code, it's best to put them in a helper class (a global or per-package class depending on key usage and number of keys)

    From refactoring point of view it's a bit more complicated (requires more changes) to move the constants from one class to another than to rename them or to change their value.

    When changing their value you have no way of automatically change the defined resource.

提交回复
热议问题