Example usage for com.vaadin.ui HasComponents getLocale

List of usage examples for com.vaadin.ui HasComponents getLocale

Introduction

In this page you can find the example usage for com.vaadin.ui HasComponents getLocale.

Prototype

public Locale getLocale();

Source Link

Document

Gets the locale of the component.

Usage

From source file:com.validation.manager.core.VaadinUtils.java

License:Apache License

/**
 * Change all {@code Locale} dependant properties of the
 * {@code com.vaadin.ui.Component}s within of the given component container
 * (typically an {@link UI} or other top level layout component). If the
 * specified {@code Locale} is the same as the current {@code Locale} of the
 * component container, this method does nothing. Otherwise it'll go thru
 * the components searching for it's component id. If it is in the resource
 * bundle, it'll set it's caption to the right translated string.
 *
 * <p>/*from   w ww . java 2 s  .com*/
 * To use this method, do something like:
 * <pre>
 * public class MyUI extends UI {
 *
 *     {@literal @}Override
 *     public void init(final VaadinRequest request) {
 *         // ... usual code
 *         // somewhere in the UI the user can change the "Form Locale". This code must
 *         // call myUI#setLocale(newLocale);
 *     }
 *
 *     // ...
 *
 * }
 *
 *      String key = "demo.tab.message";
 *      VerticalLayout vl = new VerticalLayout();
 *      Button b = new Button(key);
 *      vl.addComponent(b);
 *      ResourceBundle rb = ResourceBundle.getBundle(
 *              "resource.bundle",
 *              new Locale("es"));
 *      VaadinUtils.updateLocale(vl, new Locale("es"), rb);
 *
 *      It also works with components implementing Property:
 *
 *      VerticalLayout vl = new VerticalLayout();
 *      Label l = new Label(key);
 *      vl.addComponent(l);
 *      ResourceBundle rb = ResourceBundle.getBundle(
 *              "resource.bundle",
 *              new Locale("es"));
 *      VaadinUtils.updateLocale(vl, new Locale("es"), rb);
 * </pre>
 *
 * @param ui The component container for which the {@code Locale} dependent
 * component properties must be changed, never {@code null}
 * @param locale The new {@code Locale}, never {@code null}
 * @param rb The {@code ResourceBundle} for the specified {@code Locale},
 * never {@code null}
 */
public static void updateLocale(final HasComponents ui, final Locale locale, final ResourceBundle rb) {

    // locale may not be null, however the current UI Locale may be null!
    if (locale.equals(ui.getLocale())) {
        return;
    }
    final long time = System.currentTimeMillis();
    walkComponentTree(ui, (Component c) -> {
        String id = c.getId();
        String caption;
        if (c instanceof Property && ((Property) c).getValue() instanceof String) {
            caption = (String) ((Property) c).getValue();
        } else {
            caption = c.getCaption();
        }
        if (id != null && !id.trim().isEmpty()) {
            if (rb.containsKey(id)) {
                try {
                    c.setCaption(new String(rb.getString(id).getBytes("ISO-8859-1"), "UTF-8"));
                } catch (UnsupportedEncodingException ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }
            }
        } else if (caption != null && !caption.trim().isEmpty()) {
            /**
             * This is a more complex scenario where the caption uses more
             * than one key for translation. Sort the keys in reverse so
             * substitutions are correct.
             */
            final SortedSet<String> ss = new TreeSet<>(Collections.reverseOrder());
            for (Enumeration<String> e = rb.getKeys(); e.hasMoreElements();) {
                try {
                    String key = e.nextElement();
                    ss.add(key);
                    caption = caption.replaceAll(key,
                            new String(rb.getString(key).getBytes("ISO-8859-1"), "UTF-8"));
                } catch (UnsupportedEncodingException ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }
            }
            if (c instanceof Property) {
                ((Property) c).setValue(caption);
            } else {
                c.setCaption(caption);
            }
        }
    });
    LOG.log(Level.FINE, "Locale updated: {0} -> {1} in {2} ms.",
            new Object[] { ui.getLocale(), locale, System.currentTimeMillis() - time });
}