Getting localized strings

At this point, the LoginFormComponent can be internationalized. The next step is to pass the strings containing the captions in the correct language. Usually, the Locale and ResourceBundle standard Java classes are good enough to externalize localized messages. However, it is also a good idea to isolate string externalization logic into a separate class that allows clients to add resource bundles and get localized strings by name. Encapsulating this logic into a separate class allows you to change the underlying mechanism (for example, to read the messages from a database) and add features such as caching without affecting the rest of the application.

The following is an implementation of the Messages utility class used to encapsulate string externalization logic:

public class Messages { 
 
    private static final TreeSet<String> baseNames = 
            new TreeSet<>(); 
 
    public static void addBundle(String baseName) { 
        baseNames.add(baseName); 
    } 
 
    public static String get(String key) { 
        return baseNames.stream() 
                .map(baseName -> ResourceBundle.getBundle( 
                        baseName, UI.getCurrent().getLocale())) 
                .filter(bundle -> bundle.containsKey(key)) 
                .map(bundle -> bundle.getString(key)) 
                .findFirst().get(); 
    } 
 
} 

This class can be used to register a base name used internally by the standard ResourceBundle class. This base name should match the name of the properties files with the translations. For example, to add English and Spanish messages, you have to create two files, messages_en.properties and messages_es.properties. The messages part in the name of these files corresponds to the base name. You can load these resource bundles by calling Messages.addBundle("messages").

The Messages class is located in the Data-centric-Applications-with-Vaadin-8chapter-03 Maven project of the source code that accompanies this book. The class includes a method to get all the available languages that you can use to allow end users to change the language from the UI.

Supporting a new language is as easy (or complicated) as adding a new .properties file (in the resources directory) containing the translated properties. For example, a messages_en.properties file could define the following properties:

auth.username=Username
auth.password=Password
auth.login=Login
auth.rememberMe=Remember me
auth.logout=Logout
auth.bad.credentials=Wrong username or password

To support Spanish, for example, you would have to add a messages_es.properties file with the following content:

auth.username=Usuario
auth.password=Contraseu00f1a
auth.login=Acceder
auth.rememberMe=Recordarme
auth.logout=Salir
auth.bad.credentials=Usuario o contraseƱa incorrectos
Note that you have to use unicode scape syntax if you want to include special characters (like the Spanish n with a tilde in the example).

You can get a message in the language of the browser by calling Messages.get("property"). For example, the following snippet of code sets the correct labels for the components in the LoginFormComponent:

loginForm.setCaptions( 
        Messages.get("auth.username"), 
        Messages.get("auth.password"), 
        Messages.get("auth.login"), 
        Messages.get("auth.rememberMe")); 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset