Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: howto write i18n string shorter

In Python/C++, I normally use _("string") for i18n string text.

for Java, I use bundle.getString("string"). Obviously, it is uglier than Python/C++.

How to write such code shorter?

like image 238
zhongshu Avatar asked Mar 10 '11 10:03

zhongshu


2 Answers

Create your own method:

public String _(String key){
        return bundle.getString(key);
}

Or something similar. Underscore is a valid method name in Java. Of course, you can use any other single character, if you prefer, say l like localize.

So, now you can call it the same way as in Python.

like image 186
Goran Jovic Avatar answered Oct 28 '22 23:10

Goran Jovic


Thats the way Java is, you can call it ugly, though.

I would pretty much stick with the Java convention and use the bundle.getString(...) version. :)

like image 30
Adeel Ansari Avatar answered Oct 29 '22 00:10

Adeel Ansari