2017-06-17 7 views
0

JavaFXアプリケーションでは、表示目的で英語またはヒンディー語のいずれかを選択できます。私が英語とヒンディー語を絶えず切り替えている場合、ヒンディー語のテキストのスペースや特殊文字は、スクリーンショットのようにいくつかの水平線に置き換えられます。 別のスレッドでラベルテキストを設定しようとしましたが、使用しませんでした。 キャッシュに問題がありますか?私はそれを設定する前にラベルのテキストをクリアしようとしましたが、それでも同じ問題に直面しています。また、labelのcacheプロパティは無効です。助言がありますか?ロケール変更時にラベルテキストを継続的に設定する際の問題

私はblog [Link] [1]から 'I18N utility class'を参照しています。このクラスは、ユーザーが言語を選択するときにロケールを設定し、ラベルのtextproperty()をトリガーします。

そしてここでは、コードのスニペットです:

public class example implements Initializable { 
    @FXML 
    private Label dateLbl; 

    public void initialize(URL url, ResourceBundle rb) { 
      engBtn.setOnAction((evt) -> switchLanguage(Locale.ENGLISH)); 
      hnBtn.setOnAction((evt) -> switchLanguage(new Locale("hi","IN"))); 
      dateLbl.textProperty().bind(createStringBinding(() -> 
       I18N.changeDate())); //DATE LABEL WHICH IS SHOWING WEIRD BEHAVIOUR 
    } 

    private void switchLanguage(Locale locale) { 
     I18N.setLocale(locale); 
    } 

/////////////////// クラスの国際化: /////// /////////

import java.io.UnsupportedEncodingException; 
import javafx.beans.binding.Bindings; 
import javafx.beans.binding.StringBinding; 
import javafx.beans.property.ObjectProperty; 
import javafx.beans.property.SimpleObjectProperty; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.Tooltip; 

import java.text.MessageFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Date; 
import java.util.List; 
import java.util.Locale; 
import java.util.ResourceBundle; 
import java.util.concurrent.Callable; 


public final class i18N { 
    /** the current selected Locale. */ 
    private static final ObjectProperty<Locale> locale; 
    static { 
     locale = new SimpleObjectProperty<>(getDefaultLocale()); 
     locale.addListener((observable, oldValue, newValue) -> Locale.setDefault(newValue)); 
    } 

    /** 
    * get the supported Locales. 
    * 
    * @return List of Locale objects. 
    */ 
    public static List<Locale> getSupportedLocales() { 
return new ArrayList<>(Arrays.asList(Locale.ENGLISH, new Locale("hi","IN")));   
//return new ArrayList<>(Arrays.asList(Locale.ENGLISH, new Locale("hi","IN"))); 
    } 

    /** 
    * get the default locale. This is the systems default if contained in the supported locales, english otherwise. 
    * 
    * @return 
    */ 
    public static Locale getDefaultLocale() { 
     Locale sysDefault = Locale.getDefault(); 
     return getSupportedLocales().contains(sysDefault) ? sysDefault : Locale.ENGLISH; 
    } 

    public static Locale getLocale() { 
     return locale.get(); 
    } 

    public static void setLocale(Locale locale) { 
     localeProperty().set(locale); 
     Locale.setDefault(locale); 
    } 

    public static ObjectProperty<Locale> localeProperty() { 
     return locale; 
    } 

    /** 
    * gets the string with the given key from the resource bundle for the current locale and uses it as first argument 
    * to MessageFormat.format, passing in the optional args and returning the result. 
    * 
    * @param key 
    *   message key 
    * @param args 
    *   optional arguments for the message 
    * @return localized formatted string 
    */ 
    public static String get(final String key, final Object... args) { 
     /*temp++; 
     if(temp%2==0){ 
     } 
     else { 

     }*/ 
     ResourceBundle bundle = ResourceBundle.getBundle("bundles.lang", getLocale()); 
     return MessageFormat.format(bundle.getString(key), args); 
    } 


    public static String changeDate() throws UnsupportedEncodingException { 

     SimpleDateFormat dateFormat; 
     dateFormat = new SimpleDateFormat("dd-MMM-yyyy E HH:mm a",getLocale()); 
     Date date = new Date(); 
     System.out.println(dateFormat.format(date)); 
     return dateFormat.format(date); 

    } 

    /** 
    * creates a String binding to a localized String for the given message bundle key 
    * 
    * @param key 
    *   key 
    * @return String binding 
    */ 
    public static StringBinding createStringBinding(final String key, Object... args) { 
     return Bindings.createStringBinding(() -> get(key, args), locale); 
    } 

    /** 
    * creates a String Binding to a localized String that is computed by calling the given func 
    * 
    * @param func 
    *   function called on every change 
    * @return StringBinding 
    */ 
    public static StringBinding createStringBinding(Callable<String> func) { 
     return Bindings.createStringBinding(func, locale); 
    } 

    /** 
    * creates a bound Label whose value is computed on language change. 
    * 
    * @param func 
    *   the function to compute the value 
    * @return Label 
    */ 
    public static Label labelForValue(Callable<String> func) { 
     Label label = new Label(); 
     label.textProperty().bind(createStringBinding(func)); 
     return label; 
    } 

    /** 
    * creates a bound Button for the given resourcebundle key 
    * 
    * @param key 
    *   ResourceBundle key 
    * @param args 
    *   optional arguments for the message 
    * @return Button 
    */ 
    public static Button buttonForKey(final String key, final Object... args) { 
     Button button = new Button(); 
     button.textProperty().bind(createStringBinding(key, args)); 
     return button; 
    } 

    /** 
    * creates a bound Tooltip for the given resourcebundle key 
    * 
    * @param key 
    *   ResourceBundle key 
    * @param args 
    *   optional arguments for the message 
    * @return Label 
    */ 
    public static Tooltip tooltipForKey(final String key, final Object... args) { 
     Tooltip tooltip = new Tooltip(); 
     tooltip.textProperty().bind(createStringBinding(key, args)); 
     return tooltip; 
    } 

} 

[スクリーンショット]固定[2]

+0

問題を示す[mcve]を含むように質問を編集してください。 – trashgod

+0

@ trashgod..Thnxを参考にしてください...参考のためにコードを追加しました – k129

+0

@trashgod ...動的変更のために使用しているJavaファイルも追加しました。 ヒンディー語を別の言語(germa、chinese、japanese)に置き換えた場合、そのような問題は発生しません。 – k129

答えて

0

問題:!

現在のfontStyleはcausました私は 'Arial'フォントスタイルに変更しました。これは正常に動作しています:)

関連する問題