Help with multilingual website through Java
I'm trying to create a multilingual website the way i used to do on PHP, but i'm unable to achieve the final aspect of the structuring... Here's how it works:
I have the language class which will set the chosen website language for the user... The string Lang below holds the package name/structure on the project so that it can determine which package to access:
Code:
package Language;
public class Language {
private Integer UserLang;
public String[] Lang = new String[3];
public Language(){
this.Lang[0] = "enUS";
this.Lang[1] = "ptBR";
this.Lang[2] = "esES";
}
}
Then, i have all the three packages similar to this (as you can see, the same structuring and same Java file name):
Code:
package Language.enUS;
public class gamerslustPortal {
public String test = "test from inside the Language.enUS";
}
Code:
package Language.ptBR;
public class gamerslustPortal {
public String test = "test from inside the Language.ptBR";
}
Code:
package Language.esES;
public class gamerslustPortal {
public String test = "test from inside the Language.esES";
}
From the main portal then, i'm trying to access the specific class from the package:
Code:
Language lang = new Language();
page import="Language."+Lang.lang[0]+".gamerslustPortal"; // enUS
Since the Java file from the 3 different languages have the same name, i can then create a language system that simply swaps the package folder and read the same string name on the same Java file name, but on a different package.
The problem is that the "page import" won't work the way i want it to.
Any ideas on how to do this? Perhaps another way?
Thanks in advance!
Re: Help with multilingual website through Java
Quote:
The problem is that the "page import" won't work the way i want it to.
Can you show what the code does and explain why it is not the way you want it to work?
And explain what is the way you want it to work.
Have you looked at the ResourceBundle class?
Re: Help with multilingual website through Java
Hmmmm, i'm not sure as how to go on explaining it better...
Basically i want to dinamically read a different Java file with the localized string inside:
Code:
Language lang = new Language();
page import="Language."+Lang.lang[0]+".gamerslustPortal"; // enUS
page import="Language."+Lang.lang[1]+".gamerslustPortal"; // ptBR
page import="Language."+Lang.lang[2]+".gamerslustPortal"; // esES
I'm going to have a look at this ResourceBundle class you mentioned too.
Re: Help with multilingual website through Java
What is the datatype of page? The stuff to the right of the = looks like a String.
Also import is a keyword and can't be used as a variable name.
Re: Help with multilingual website through Java
Thank you! After some research i managed to do what i wanted with ResourceBundle Class!
Solved! Thanks! Here's the code for further search reference:
gamerslustPortal.properties (file located under Language.enUS package)
Code:
test = test from inside the .property file
Language.java (class created to make a dynamic chance on the website language.
Code:
package Language;
public class Language {
private Integer UserLang;
public String[] Lang = new String[3];
public Language(){
this.Lang[0] = "enUS";
this.Lang[1] = "ptBR";
this.Lang[2] = "esES";
}
}
index.jsp
Code:
<%@ page import="java.util.ResourceBundle"%>
Language lang = new Language();
String MESSAGES_PROPERTIES = "Language."+lang.Lang[0]+".gamerslustPortal";
ResourceBundle labels = ResourceBundle.getBundle(MESSAGES_PROPERTIES);
String value = labels.getString("test");
out.print(value);