It appears that Java enums are not of much use when Internationalization (I18n) is a concern. The values are hardcoded and can't be changed without recompiling. While variables could be added to include the I18n information in the enum class, all related methods and variables would have to be re-coded for each enum since enums can't be subclassed. Can anyone suggest a way around this problem? If not, I guess I'll just have to create my own class with static final values. Hayward Hulick
|
|
0
|
|
|
|
Reply
|
Hayward
|
7/12/2007 3:45:50 PM |
|
In article <f75iba06d9@enews1.newsguy.com>,Hayward Hulick <hayward@ispwest.com> wrote:> It appears that Java enums are not of much use when Internationalization >(I18n) is a concern. The values are hardcoded and can't be changed without >recompiling. While variables could be added to include the I18n information >in the enum class, all related methods and variables would have to be >re-coded for each enum since enums can't be subclassed. Can anyone suggest >a way around this problem? If not, I guess I'll just have to create my own >class with static final values.It's not entirely clear why i18n conflicts with enums in your case,but if I were to guess I would say that your problems may be solved byoverriding toString() in the enum class to ensure that it supportsi18n.I am assuming that what you are trying to internationalize is programoutput rather than the source code itself.Cheers Bent D-- Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd powered by emacs
|
|
0
|
|
|
|
Reply
|
bcd
|
7/12/2007 4:42:25 PM
|
|
Hayward Hulick wrote:> It appears that Java enums are not of much use when Internationalization > (I18n) is a concern. The values are hardcoded and can't be changed without > recompiling. While variables could be added to include the I18n information > in the enum class, all related methods and variables would have to be > re-coded for each enum since enums can't be subclassed. Can anyone suggest > a way around this problem? If not, I guess I'll just have to create my own > class with static final values.> I suggest to override your enum's toString() method, so that it returns a locale-dependent String.There is more info in the API docs of Enum#toString, ResourceBundle#getBundle and PropertyResourceBundle.Below is a simple example for this approach.When I run it (on my german system) I get an output with german Strings: MERCURY: Merkur VENUS: Venus EARTH: Erde MARS: Mars// File de/tfritsch/Planet.javapackage de.tfritsch;import java.util.ResourceBundle;public enum Planet { MERCURY, VENUS, EARTH, MARS; /** Resources for the default locale */ private static final ResourceBundle res = ResourceBundle.getBundle("de.tfritsch.Res"); /** @return the locale-dependent name of the planet */ public String toString() { return res.getString(name() + ".string"); } /** Tests the toString() method for all constants. */ public static void main(String[] args) { for (Planet value : values()) { System.out.println(value.name() + ": " + value); } }}# File de/tfritsch/Res.properties# default language (english) resourcesMERCURY.string=MercuryVENUS.string=VenusEARTH.string=EarthMARS.string=Mars# File de/tfritsch/Res_de.properties# german language resourcesMERCURY.string=MerkurVENUS.string=VenusEARTH.string=ErdeMARS.string=Mars.... (and so for French, Spanish, Russian, ...)-- Thomas
|
|
0
|
|
|
|
Reply
|
Thomas
|
7/12/2007 5:04:19 PM
|
|
Hayward Hulick wrote :
> It appears that Java enums are not of much use when Internationalization
> (I18n) is a concern. The values are hardcoded and can't be changed without
> recompiling. While variables could be added to include the I18n information
> in the enum class, all related methods and variables would have to be
> re-coded for each enum since enums can't be subclassed. Can anyone suggest a
> way around this problem? If not, I guess I'll just have to create my own
> class with static final values.
>
> Hayward Hulick
public SomeClass
{
public enum MyEnum
{
VALUE_ONE("I18N_Key.for.one"),
VALUE_TWO("I18N_Key.for.two");
private String i18NKey;
private MyEnum(String i18N)
{
i18NKey = i18n;
}
public static String getI18NKey()
{
return i18NKey;
}
}
}
.....
String key = MyEnum.VALUE_ONE.getI18NKey();
.....
--
Wojtek :-)
|
|
0
|
|
|
|
Reply
|
Wojtek
|
7/12/2007 7:45:37 PM
|
|
Hayward Hulick wrote :> It appears that Java enums are not of much use when Internationalization > (I18n) is a concern. The values are hardcoded and can't be changed without > recompiling. While variables could be added to include the I18n > information in the enum class, all related methods and variables would have > to be re-coded for each enum since enums can't be subclassed. Can anyone > suggest a way around this problem? If not, I guess I'll just have to > create my own class with static final values.>> Hayward Hulickpublic SomeClass{ public enum MyEnum { VALUE_ONE("I18N_Key.for.one"), VALUE_TWO("I18N_Key.for.two"); private String i18NKey; private MyEnum(String i18N) { i18NKey = i18n; } public String getI18NKey() { return i18NKey; } }}.....String key = MyEnum.VALUE_ONE.getI18NKey();.....-- Wojtek :-)
|
|
0
|
|
|
|
Reply
|
Wojtek
|
7/12/2007 8:27:48 PM
|
|
Wojtek wrote:> public SomeClass> {> public enum MyEnum> {> VALUE_ONE("I18N_Key.for.one"),> VALUE_TWO("I18N_Key.for.two");> > private String i18NKey;> > private MyEnum(String i18N)> {> i18NKey = i18n;> }> > public String getI18NKey()> {> return i18NKey;> }> }> }> > > ....> > String key = MyEnum.VALUE_ONE.getI18NKey();> ....> There is no need to introduce an additional variable (i18NKey) as key.You can use the programmatic name ("VALUE_ON", "VALUE_TWO") as key: String key = MyEnum.VALUE_ONE.name();-- Thomas
|
|
0
|
|
|
|
Reply
|
Thomas
|
7/13/2007 8:51:28 AM
|
|
"Thomas Fritsch" <i.dont.like.spam@invalid.com> wrote in message news:newscache$0rs2lj$cva$1@news.ops.de...> Hayward Hulick wrote:>> It appears that Java enums are not of much use when >> Internationalization (I18n) is a concern. The values are hardcoded and >> can't be changed without recompiling. While variables could be added to >> include the I18n information in the enum class, all related methods and >> variables would have to be re-coded for each enum since enums can't be >> subclassed. Can anyone suggest a way around this problem? If not, I >> guess I'll just have to create my own class with static final values.>>> I suggest to override your enum's toString() method, so that it returns a > locale-dependent String.> There is more info in the API docs of Enum#toString, > ResourceBundle#getBundle and PropertyResourceBundle.> Below is a simple example for this approach.> When I run it (on my german system) I get an output with german Strings:> MERCURY: Merkur> VENUS: Venus> EARTH: Erde> MARS: Mars>> // File de/tfritsch/Planet.java> package de.tfritsch;>> import java.util.ResourceBundle;>> public enum Planet {> MERCURY, VENUS, EARTH, MARS;>> /** Resources for the default locale */> private static final ResourceBundle res => ResourceBundle.getBundle("de.tfritsch.Res");>> /** @return the locale-dependent name of the planet */> public String toString() {> return res.getString(name() + ".string");> }>> /** Tests the toString() method for all constants. */> public static void main(String[] args) {> for (Planet value : values()) {> System.out.println(value.name() + ": " + value);> }> }> }>>> # File de/tfritsch/Res.properties> # default language (english) resources> MERCURY.string=Mercury> VENUS.string=Venus> EARTH.string=Earth> MARS.string=Mars>>> # File de/tfritsch/Res_de.properties> # german language resources> MERCURY.string=Merkur> VENUS.string=Venus> EARTH.string=Erde> MARS.string=Mars>> ... (and so for French, Spanish, Russian, ...)> -- > Thomas Thank you. Your solution is a lot simpler than the one I came up with, and doesn't involve a lot of extra code to be repeated in every enum. Thanks again. Hayward
|
|
0
|
|
|
|
Reply
|
Hayward
|
7/13/2007 3:39:10 PM
|
|
On Thu, 12 Jul 2007 11:45:50 -0400, "Hayward Hulick"<hayward@ispwest.com> wrote, quoted or indirectly quoted someone whosaid :> It appears that Java enums are not of much use when Internationalization >(I18n) is a concern. The values are hardcoded and can't be changed without >recompiling. While variables could be added to include the I18n information >in the enum class, all related methods and variables would have to be >re-coded for each enum since enums can't be subclassed. Can anyone suggest >a way around this problem? If not, I guess I'll just have to create my own >class with static final values. the enum name appears only inside code. It is merely a default forexternal use. You can write your own internationalised enum toStringor valueOf--Roedy Green Canadian Mind ProductsThe Java Glossaryhttp://mindprod.com
|
|
0
|
|
|
|
Reply
|
Roedy
|
7/13/2007 4:52:58 PM
|
|
Thomas Fritsch wrote :> Wojtek wrote:>> public SomeClass>> {>> public enum MyEnum>> {>> VALUE_ONE("I18N_Key.for.one"),>> VALUE_TWO("I18N_Key.for.two");>> >> private String i18NKey;>> >> private MyEnum(String i18N)>> {>> i18NKey = i18n;>> }>> >> public String getI18NKey()>> {>> return i18NKey;>> }>> }>> }>> >> >> ....>> >> String key = MyEnum.VALUE_ONE.getI18NKey();>> ....>> > There is no need to introduce an additional variable (i18NKey) as key.> You can use the programmatic name ("VALUE_ON", "VALUE_TWO") as key:> String key = MyEnum.VALUE_ONE.name();Not if you organize your I18N.Mine looks a lot like:report.finance.yearly.totalandsystem.admin.control.blockLogin-- Wojtek :-)
|
|
0
|
|
|
|
Reply
|
Wojtek
|
7/16/2007 3:35:16 PM
|
|
|
8 Replies
212 Views
(page loaded in 0.122 seconds)
|