tags:

views:

68

answers:

3

I need to create interface MultiLingual, that allows to display object's data in different languages (not data itself, but introduction like "Author", "Title" etc.).

Printed data looks like this :

3 grudnia 1998

10th of June 1924

Autor: Tolkien
Tytul: LoTR
Wydawnictwo: Amazon 2010

Author: Mitch Albom
Title: Tuesdays with Morrie
Publishing House: Time Warner Books 2003   

37 360,45 PLN

5,850.70 GBP

3rd of December 1998

10th of June 1924

Author: Tolkien
Title: LoTR
Publishing House: Amazon 2010

Author: Mitch Albom
Title: Tuesdays with Morrie
Publishing House: Time Warner Books 2003

37,360.45 GBP

5,850.70 GBP

Test code looks like this :

public class Main {

public static void main(String[] args){

  MultiLingual gatecrasher[]={ new Data(3,12,1998),
                               new Data(10,6,1924,MultiLingual.ENG),
                               new Book("LoTR", "Tolkien", "Amazon", 2010),
                               new Book("Tuesdays with Morrie",
                                        "Mitch Albom", "Time Warner Books",2003,
                                        MultiLingual.ENG),
                               new Money(1232895/33.0,MultiLingual.PL),
                               new Money(134566/23.0,MultiLingual.ENG),
                             };

  for(int i=0;i < gatecrasher.length;i++)
    System.out.println(gatecrasher[i]+"\n");

  for(int i=0;i < gatecrasher.length;i++)
    System.out.println(gatecrasher[i].get(MultiLingual.ENG)+"\n");

}
}

So i need to introduce constants ENG, PL in MultiLingual interface, as well as method get(int language) :

public interface MultiLingual {

int ENG = 0;
int PL= 1;

String get(int lang);

}

And then I have class Book. Problem starts with the constructors. One of them needs to take MultiLingual.ENG as argument, but how to achieve that ? Is this the proper way? :

class Book implements MultiLingual {

private String title;
private String publisher;
private String author;

public Book(String t, String a, String p, int y, MultiLingual lang){      
}

Or should I treat this MultiLingual.ENG as int variable , that will just change automatically constants in interface?

Second constructor for book doesn't take MultLingual as argument, but following implementation is somehow wrong :

public Book(String t, String a, String p, int y){
    Book someBook = new Book(t, a, p, y, MultiLingual m);
}

I could just send int m in place of MultiLingual m but then I will have no control if language is set to PL or ENG.

And finally get() method for Boook but I think at least this should be working fine:

public String get(int lang){

    String data;
    if (lang == ENG){
        data = "Author: "+this.author+"\n"+
                "Title: "+this.title+"\n"+
                "Publisher: "+this.publisher+"\n";
    }
    else {
        data = "Autor: "+this.author+"\n"+
                "Tytul: "+this.title+"\n"+
                "Wydawca: "+this.publisher+"\n";            
    }

    return data;
}

@Override
public String toString(){
    return "";
}

}
+2  A: 

You'd probably be better using an enum to represent the different languages

public interface MultiLingual {

    enum Language { ENG, PL }

    String get(Language lang);

}

The constructor of your Book class can then take an instance of MultiLingual.Language

public Book(String t, String a, String p, int y, MultiLingual.Language lang) {      
}
Ben Lings
Instead of enums for languages you should use the class Locale, written for exact this purpose, to represent a language. As such many libraries already understand locales.
Dishayloo
+1  A: 

Inside your interface, the ints representing the languages should be statics, you don't want to accidentally start reassinging them while your code is running. Then have your book class hold an int for which language it is.

Book someBook = new Book(t, a, p, y, MultiLingual m); won't work as MultiLingual is an interface, it isn't anything there can be an instance of to pass in.

It needs to be an int. Which will be something like MultiLingual.ENG or .PL

AaronM
thanks, now I get it
mike_hornbeck
+1  A: 

Did you consider using ResourceBundle? That means, you could use property files for defining locale specific Data, making it very easy to add more locales at a later point. Your data object would return some key for the labels to translate, depending on your application, the guy could then resolve the right labels via the ResourceBundle.

Dominik
+1 - that's what ResourceBundles are for.
duffymo