2017-06-17 4 views
0

Dans mon application JavaFX, l'utilisateur peut choisir la langue anglaise ou hindi à des fins d'affichage. Si je passe continuellement de l'anglais à l'hindi, à un moment donné, les espaces ou les caractères spéciaux du texte hindi sont remplacés par des lignes horizontales comme indiqué sur la capture d'écran. J'ai essayé de définir le texte de l'étiquette dans un fil séparé, mais pas d'utilisation. Est-ce dû à un problème de mise en cache? J'ai essayé d'effacer le texte de l'étiquette avant de le régler, mais je suis toujours confronté au même problème. En outre, la propriété de cache de l'étiquette est désactivée. Aucune suggestion?Problème lors de la modification continue du texte de l'étiquette lors de la modification des paramètres régionaux

Je fais référence à la «classe d'utilitaires I18N» du blog [Link] [1]. Cette classe définit les paramètres régionaux chaque fois que la langue est sélectionnée par l'utilisateur et déclenche textproperty() des étiquettes.

Et voici l'extrait de code:

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

/////////////////// classe i18N: /////// ! /////////

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

} 

[Capture d'écran] [2]

+0

S'il vous plaît modifier votre question pour inclure un [mcve] qui présente le problème que vous illustrez. – trashgod

+0

@ trashgod..Thnx pour votre suggestion ... j'ai ajouté le code pour la référence – k129

+0

@trashgod ... J'ai également ajouté le fichier java que j'utilise pour le changement dynamique ... Des suggestions maintenant? Si je remplace l'hindi par différentes langues (germa, chinois, japonais), je ne suis pas confronté à un tel problème avec eux .. – k129

Répondre

0

Problème résolu:

fontstyle actuelle était caus erreur ING .. Je l'ai changé en 'Arial' fontstyle maintenant il fonctionne correctement :)