在
Java中进行国际化时,可以为每条消息分配一个字符串键.什么是最佳做法,在哪里放置这些字符串键.目标是允许轻松重构(例如键名更改),干净可读的代码,分离问题,但即使从代码的不同部分调用也不会重复键/消息.
//bad way,strings directly in code messages.getString("hello_key");
–
// better way,use String constants public static final String HELLO_KEY = "hello_key"; ... messages.getString(HELLO_KEY);
–
// other (better?) way,put all keys in one huge central class public class AllMessageKeys { public static final String HELLO_KEY = "hello_key"; ... } public class Foo { ... messages.getString(AllMessageKeys.HELLO_KEY); }
–
// other (better?) way,put all keys in neighbor class public class FooMessageKeys { public static final String HELLO_KEY = "hello_key"; } public class Foo { ... messages.getString(FooMessageKeys.HELLO_KEY); }
任何其他建议?哪个是最好的?我在Eclipse IDE上,如果这使得重构部分更清晰.
说明:在上面的例子中,“消息”的类型是ResourceBundle.