Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Locale;
- import java.util.ResourceBundle;
- public class MerLocalizedEnum {
- /**In this class, I'm going to localize an enum.
- * Place MerLocBundle.properties and MerLocBundle_sp_MX.properties in base directory of
- * source.
- * @author Meredith
- */
- public MerLocalizedEnum() {
- Locale.setDefault(new Locale("sp","MX"));
- for(AnimalEnum ae:AnimalEnum.values()){
- System.out.println(ae.toString());
- }
- Locale.setDefault(new Locale("en","US"));
- for(AnimalEnum ae:AnimalEnum.values()){
- System.out.println(ae.toString());
- }
- }
- public static void main(String[] args)
- {
- MerLocalizedEnum mle = new MerLocalizedEnum();
- }
- enum AnimalEnum{
- DOG{
- @Override
- public String getStringRep(ResourceBundle animalNames){
- return animalNames.getString("DOG");
- }
- },
- CAT{
- @Override
- public String getStringRep(ResourceBundle animalNames){
- return animalNames.getString("CAT");
- }
- },
- BIRD{
- @Override
- public String getStringRep(ResourceBundle animalNames){
- return animalNames.getString("BIRD");
- }
- };
- private AnimalEnum()
- {
- }
- public abstract String getStringRep(ResourceBundle animalNames);
- @Override
- public String toString()
- {
- ResourceBundle animNames = ResourceBundle.getBundle("MerLocBundle",Locale.getDefault());
- return this.getStringRep(animNames);
- }
- }
- }
- /*Sample output:
- "Perro"
- "Gato"
- "Pájaro"
- "Dog"
- "Cat"
- "Bird"
- */
- /*Properties files:
- MerLocBundle.properties:
- DOG="Dog"
- CAT="Cat"
- BIRD="Bird"
- MerLocBundle_sp_MX.properties:
- DOG="Perro"
- CAT="Gato"
- BIRD="Pájaro"
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement