Reputation: 129
I have a Java program and I have more than one languages, I'm using i18n with this structure:
package languages;
import languages.it.ItLanguage;
enum ListLanguages {
IT,
EN,
FR
}
public class Languages {
ItLanguage language;
public Languages(ListLanguages lan) {
switch (lan) {
case IT: {
this.language = new ItLanguage();
break;
}
case EN: {
// this.language = new EnLanguage();
break;
}
}
}
public String[] getStrings(String item) {
return this.language.getStringArray(item);
}
public String getString(String item) {
return this.language.getString(item);
}
}
I wish to call the constructor Languages
passing the language to use and after to call this object to have the string, like in following way:
Languages lan = new Languages(it);
lan.getString("s1");
[...]
Languages lan = new Languages(fr);
lan.getString("s2");
But I cannot find a solution to avoid the switch inside the methods like getString
:
switch(languageChosen) {
case("it"): return this.itLanguage.getString(item);
case("en"): return this.enLanguage.getString(item);
}
is there a solution to avoid this switch`?
The other class (for more information) is:
package it.bper.languages.it;
import java.util.ListResourceBundle;
public class ItLanguage extends ListResourceBundle {
@Override
protected Object[][] getContents() {
return new Object[][] {
{"name", "Pippo"},
{
"ourfeatures", new String[]
{
"Б, Г, Д, ё, Ж, П, Ф, И, й, Л, Ц, Ш, Щ, Э, e Ю Я",
"Null",
"Empty"
}
}
};
}
}
Upvotes: 1
Views: 44
Reputation: 129
I have found a solution (thanks to PHolzwarth):
package testi18nv1;
public class Main {
public static void main(String[] args) {
Languages lan = new Languages(ListLanguages.IT);
System.out.println(lan.getString("P"));
}
}
Class Languages:
package testi18nv1;
public class Languages {
private Language language;
public Languages(ListLanguages lan) {
switch (lan) {
case IT: {
this.language = new ItLanguage();
break;
}
case EN: {
// this.language = new EnLanguage();
break;
}
}
}
public String getString(String item) {
return this.language.getStringX(item);
}
}
package testi18nv1;
import java.util.ListResourceBundle;
class ItLanguage extends ListResourceBundle implements Language {
@Override
protected Object[][] getContents() {
return new Object[][]{
{"PETERS", "Peter parker"},
{
"ourfeatures", new String[]
{
"Charlie",
"Localization Workflow Management",
"Localization Process Automation"
}
}
};
}
public String getStringX(String item) {
return this.getString(item);
}
}
package testi18nv1;
enum ListLanguages {
IT,
EN,
FR
}
interface Language {
String getStringX(String item);
}
Upvotes: 0
Reputation: 357
As Java supports polymorphism, you would use an interface here: Provide interface Language
defining the getString
and getStrings
methods, and implement them in LanguageIt and in similar classes.
In general, defining classes like LanguageIt
makes most sense if they provide language-specific behavior. If they only return different data, you may also have only one Language
class and in there place the data in a Map
, using the enum as the key. The getString()
style methods can then simply lookup the data by that enum key.
This is the interface
variant:
enum ListLanguages {
IT,
EN,
FR
}
public class Languages {
private Language language;
public Languages(ListLanguages lan) {
switch (lan) {
case IT: {
this.language = new ItLanguage();
break;
}
case EN: {
// this.language = new EnLanguage();
break;
}
}
}
public String getString(String item) {
return this.language.getString(item);
}
public static void main(String[] args) {
Languages lan = new Languages(ListLanguages.IT);
System.out.println(lan.getString("Peter"));
}
}
interface Language {
String getString(String item);
}
class ItLanguage implements Language {
public String getString(String item) {
return "Grazie "+item+"!";
}
}
Upvotes: 1