2016-05-02 10 views
0

私のアプリケーションの開始ページとして検索ページを持つjavaでアプリケーションを開発しています。言語を含む検索ページにドロップダウンがあります。ドロップダウンから言語のいずれかを選択すると、アプリケーション全体をそれぞれの言語に変更する必要があります。どのように変更できますか?そのためのaplはありますか?Web Applicationの言語を変更するにはどうすればいいですか?

答えて

1

私はそのための任意のAPIを知らないが、私は、各言語のテキストを変更するjQueryのを使用し、ここで英語とドイツ語で翻訳することができ、簡単なWebページからの例です:

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); 
} 

そして、ユーザは、リロード後selctedた言語を復元するか、彼は現場に戻ったとき、私はそれを使用する:私はそれが完璧ではないことを知っている(と非常に多くのとページの集中的な解決策を仕事

$(document).ready(function() { 
    if(localStorage.getItem("lang") != null){ 
    if(localStorage.getItem("lang") == "de"){ 
     lang_de(); 
    } 
    else if (localStorage.getItem("lang") == "en") { 
     lang_en(); 
    } 
    } 

テキスト)ですが、動作していて、tran私の意見でサイトを翻訳するのは悲しいことですが、Googleでサイトをスレートしてください。

EDIT: を例えばラベル用:

$("#idOfLabel").text("Translated text"); 

またはH1のために:あなたは別のjQueryのコードが必要になる場合があります変更したい要素のテキストに応じて、

$("#h1_id").html("Translated text"); 

と、この

$(document).prop('title', 'Translated text'); 

それについては、yたとえば、ボタンの幅も変更することができます。これにより、長い文字列や短い文字列が含まれる可能性があります。

1

Javaの国際化(I18N)を使用すると、アプリケーションが複数の言語のテキストをサポートできるようになります。これは、LocaleクラスとResourceBundleクラスを使用して、特定の言語に固有のテキストを追加することによって行われます。

oracle docsチュートリアルhereを参照してください。

言語(enなどの2文字コード)と国(USなどの別の2文字コード)に基づいて、Localeを作成します。 LocaleResourceBundleを使用し、プロパティファイルを使用してテキストを保存できます。

例:

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")); 

あなたはその後、プロパティファイルを持っているでしょう。この例では、MessagesBundle.properties:別の言語については

your_string_here = Hello world! 
your_other_string = Something else 

、あなたは正しいローカライズ下に別のMessagesBundleのファイルを持っているでしょう。たとえば、フランス語の場合、ファイルはMessagesBundle_fr_FR.propertiesとなります。

your_string_here = Bonjour! 
your_other_string = Something else in French 
関連する問題