I am developing an application in java having Search page as my application starting page.I have a drop down in search page that contain languages. if i select any one of the language from the drop down then it need to change my entire application to the respective language.How can i change it ? Is there any apl for that.
2 Answers
I do not know of any API for that, but I use JQuery to change the text for each language, here is an example from a simple webpage that can be translated in English and German:
function lang_en(){
$(document).prop('title', 'Todo-List');
$("#autoremove_label").text("Remove immediately?");
$("#task").css('width', '142');
$("#h1_title").html("Todo-List");
$("#autoremove").prop('title', 'Remove immediately?');
$("#task").prop('title', 'Name of the enty.');
$('#autoremove_label').prop('title', 'With that checkbox you can toggle if an element you select will immediatly disappear or stay selected until you enable that checkbox again.');
saveLang("en");
}
function lang_de(){
$(document).prop('title', 'Merkliste');
$("#h1_title").html("Merkliste");
$("#autoremove_label").text("Sofort entfernen?");
$("#task").css('width', '175');
$("#autoremove").prop('title', 'Sofort entfernen?');
$("#task").prop('title', 'Name des Eintrags.');
$('#autoremove_label').prop('title', 'Wenn sie es anschließend wieder einschalten werden alle markierten Einträge auf einmal entfernt.');
saveLang("de");
}
function saveLang(input){
localStorage.setItem("lang", input);
}
And then to restore the language the user has selcted after reload or when he comes back to the site I use that:
$(document).ready(function() {
if(localStorage.getItem("lang") != null){
if(localStorage.getItem("lang") == "de"){
lang_de();
}
else if (localStorage.getItem("lang") == "en") {
lang_en();
}
}
I know it's not a perfect (and a very work intensive solution for pages with a lot of text), but it's working and the closest you can get without using services like translate the site with Google, which is sadly pretty bad at translating sites in my opinion.
EDIT: Depending on which elements text you want to change you might need different JQuery code: for example for a label:
$("#idOfLabel").text("Translated text");
or for a h1:
$("#h1_id").html("Translated text");
and this is for the website title:
$(document).prop('title', 'Translated text');
Whats cool about that is that you can for example change the width of button, too, so it can contain longer and shorter strings, which would otherwise probably mess up your site.

- 5,800
- 6
- 28
- 69
You can use Java's internationalization (I18N), to allow applications to support text for multiple languages. This is done by using the Locale
and ResourceBundle
classes to add text specific to a certain language:
View the oracle docs tutorial here.
Simply, you create a Locale
, based on the language (as a 2 character code such as en
) and the country (another 2 character code such as US
). You can then use a ResourceBundle
from the Locale
and use property files to store the text.
Example:
String language = "en";
String country = "US";
Locale currentLocale = new Locale(language, country);
ResourceBundle messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
System.out.println(messages.getString("your_string_here"));
You would then have a properties file; in this example, MessagesBundle.properties
:
your_string_here = Hello world!
your_other_string = Something else
For another language, you would have another MessagesBundle file under the correct localisation. For example, for French, the file would be called MessagesBundle_fr_FR.properties
:
your_string_here = Bonjour!
your_other_string = Something else in French

- 383
- 1
- 3
- 9