Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package dhbw.clippinggorilla.utilities.language; import com.vaadin.server.VaadinSession; import com.vaadin.ui.Button; import com.vaadin.ui.Component; import com.vaadin.ui.Label; import com.vaadin.ui.TabSheet.Tab; import com.vaadin.ui.TextField; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; /** * This class is used to manage the Language dependent parts of Clipping * Gorilla. This class is also the main point for storing the references to the * languages. * * @author frank */ public abstract class Language { //Static Part--------------------------------------------------------------- private static final HashMap<Locale, Language> LANGUAGES = new HashMap<>(); public static final Language ENGLISH = new English(Locale.ENGLISH); public static final Language GERMAN = new German(Locale.GERMAN); //ADD NEW LANGUAGES HERE /** * Returns the requested Word in the Requested Language * * @param word The word to be returned * @param language Language * @return */ public static final String get(Word word, Language language) { return language.words.get(word); } /** * Returns all Languages in a HashMap ordered ba the Java Locale * * @return Returns all languages in a hashmap */ public static HashMap<Locale, Language> getAllLanguages() { return LANGUAGES; } //Part to be inherited by Language classes---------------------------------- private final Map<Word, String> words = new HashMap<>(); public Language(Locale locale) { LANGUAGES.put(locale, this); } protected final void add(Word word, String translation) { words.put(word, translation); } //User-specific Part-------------------------------------------------------- /** * Returns a word in the language og the user User * {@link #set(dhbw.clippinggorilla.languages.Word, com.vaadin.ui.Component)} * to set a specific Components Text<br> * It is deprecated because changing the Language doesnt automatically * change * * @param word the word to be returned * @return The requested word in the user language */ public static final String get(Word word) { if (VaadinSession.getCurrent() == null || VaadinSession.getCurrent().getLocale() == null) { return Language.ENGLISH.words.getOrDefault(word, word.name()); } return LANGUAGES.getOrDefault(VaadinSession.getCurrent().getLocale(), Language.ENGLISH).words .getOrDefault(word, word.name()); } private static final HashMap<VaadinSession, Map<Component, Word>> COMPONENTS = new HashMap<>(); private static final HashMap<VaadinSession, List<Custom>> CUSTOMS = new HashMap<>(); /** * Automatically sets the value of the Vaadin Component, this is useful, if * the user changes the language, all components are changed * * @param word the word to be returned * @param component */ public static final void set(Word word, Component component) { Map<Component, Word> components = COMPONENTS.getOrDefault(VaadinSession.getCurrent(), new HashMap<>()); components.put(component, word); COMPONENTS.put(VaadinSession.getCurrent(), components); changeComponentTo(LANGUAGES.getOrDefault(VaadinSession.getCurrent().getLocale(), ENGLISH), component, word); } /** * Sets a custom text to a custom object<br><br> * * If the User changes the Language:<br><br> * 1. The word is being translated<br> * 3. The translated word enters the ValueSetter which sets the * value<br><br> * * Example:<br> * <pre> * {@code * Textfield field = ... ; * Language.setCustom(Word.HELLO, v -> field.setPlaceholder(v)); * } * </pre> In this Example you have a Textfield.<br> * You want to set a specific Text as Placeholder (If you just want to use * the Standard field.setValue(...) you can use the * {@link #set(Word, Component)} for that).<br> * You set the translated string as placeholder for your textfield as * example. * * @param word the word to be returned * @param setter */ public static void setCustom(Word word, ValueSetter setter) { setCustom(word, null, setter); } /** * Sets a custom text to a custom object<br><br> * * If the User changes the Language:<br><br> * 1. The word is being translated<br> * 2. The translated word enters the Stringmodifier<br> * 3. The potentially modified word enters the ValueSetter which sets the * value<br><br> * * The modifier can be null if you dont want to change the word.<br><br> * * Example:<br> * <pre> * {@code * Textfield field = ... ; * Language.setCustom(Word.TODAY_IS, s -> s + LocalDate.now().toString(), v -> field.setPlaceholder(v)); * } * </pre> In this Example you have a Textfield.<br> * You want to set a specific Text as Placeholder (If you dont want to * modify the word AND just want to use the Standard field.setValue(...) you * can use the {@link #set(Word, Component)} for that).<br> * You do this by modifying the string s by appending the local date as * example.<br> * In the end you set the modified string as placeholder for your textfield * as example. * * @param word the word to be returned * @param modifier * @param setter */ public static void setCustom(Word word, StringModifier modifier, ValueSetter setter) { List<Custom> customs = CUSTOMS.getOrDefault(VaadinSession.getCurrent(), new ArrayList<>()); customs.add(new Custom(word, modifier, setter)); CUSTOMS.put(VaadinSession.getCurrent(), customs); changeCustomTo(LANGUAGES.getOrDefault(VaadinSession.getCurrent().getLocale(), ENGLISH), word, modifier, setter); } public static final void setLanguage(Locale Language) { VaadinSession.getCurrent().setLocale(Language); changeAllComponentsTo(LANGUAGES.getOrDefault(Language, ENGLISH)); changeAllCustomsTo(LANGUAGES.getOrDefault(Language, ENGLISH)); } private static void changeAllCustomsTo(Language languageToBeChangedTo) { CUSTOMS.get(VaadinSession.getCurrent()).forEach((Custom c) -> { changeCustomTo(languageToBeChangedTo, c.word, c.modifier, c.setter); }); } private static void changeAllComponentsTo(Language languageToBeChangedTo) { COMPONENTS.get(VaadinSession.getCurrent()).forEach((Component k, Word v) -> { changeComponentTo(languageToBeChangedTo, k, v); }); } private static void changeCustomTo(Language languageToBeChangedTo, Word word, StringModifier modifier, ValueSetter setter) { String translated = word == null ? "" : languageToBeChangedTo.words.getOrDefault(word, word.name()); translated = modifier != null ? modifier.modifyString(translated) : translated; setter.setValue(translated); } private static void changeComponentTo(Language languageToBeChangedTo, Component comp, Word word) { String value = languageToBeChangedTo.words.getOrDefault(word, word.name()); if (comp instanceof Label) { ((Label) comp).setValue(value); } else if (comp instanceof Button) { ((Button) comp).setCaption(value); } else if (comp instanceof Tab) { ((Tab) comp).setCaption(value); } else if (comp instanceof TextField) { ((TextField) comp).setCaption(value); } } private static class Custom { Word word; StringModifier modifier; ValueSetter setter; public Custom(Word word, StringModifier modifier, ValueSetter setter) { this.word = word; this.modifier = modifier; this.setter = setter; } } }