Where to place i18n key strings in Java

前端 未结 8 2788
不思量自难忘°
不思量自难忘° 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

    i always using for such stuff an interface where my keys are listed. The name of the Interace is mostly DESC=Issue/short describtion/topic and the keys values. This way you can make some nice Interface and some general Interface e.g. for OK or Abort keys.

    // other (better?) way, put all keys in neighbor class
    public interface DESCMessage {
      public static final String HELLO_KEY = "hello_key";
    }
    
    public class Foo {
      ...
      messages.getString(DESCMessage.HELLO_KEY);
    }
    

提交回复
热议问题