Multi Language Java Desktop Applications

I don’t like to work with the large files of Resource Bundles when I write multi-language desktop applications with Java Swing or JavaFX. So I wanted to use a resource file for each module in my rich client applications. I’ve written an util class which has an enum set, please see below. Now I can use this I18n helper in every module.

I18n enum with application modules:

package com.cemikta.customersfx.util;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Resource Bundle helper.
 *
 * <p>
 * Each module has its own resource bundle file for i18n strings. Always read
 * default locale from <code>Locale.getDefault()</code>.
 * </p>
 *
 * <pre>
 *      I18n.MODULE_NAME.getString("stringKey");
 *      I18n.MODULE_NAME.getString("stringKey", param);
 * </pre>
 *
 * @see ResourceBundle
 *
 * @author Cem Ikta
 */
public enum I18n {

    CONTROL("control"),
    COMMON("common"),
    CUSTOMER("customer"),
    SETTINGS("settings");

    private final static Logger logger = Logger.getLogger(I18n.class.getName());
    private static final String DEFAULT_LOCATION = "i18n.";
    private final ResourceBundle resourceBundle;

    I18n(String bundleFile) {
        resourceBundle = ResourceBundle.getBundle(DEFAULT_LOCATION + bundleFile);
    }

    /**
     * Gets a string for the given key from resource bundle.
     *
     * @param key the key for the desired string
     * @return the string for the given key
     */
    public String getString(String key) {
        try {
            return resourceBundle.getString(key);
        } catch (MissingResourceException ex) {
            logger.log(Level.SEVERE, null, ex);
            return "err#";
        }
    }
    
    /**
     * Gets a string for the given key from resource bundle and formats 
     * with parameters.
     *
     * @param key the key for the desired string
     * @param params parameters for message formats
     * @return the string for the given key
     */
    public String getString(String key, Object... params) {
        try {
            return MessageFormat.format(resourceBundle.getString(key), params);            
        } catch (MissingResourceException ex) {
            logger.log(Level.SEVERE, null, ex);
            return "err#";
        }
    }

}

Here is a sample use of I18n helper:

// set default locale english  
Locale.setDefault(new Locale("en", "EN"));  
// set default locale for German  
// Locale.setDefault(new Locale("de", "DE"));  
AppView appView = new AppView();  
appView.setTitle(I18n.COMMON.getString("app.title")); 

Do you need a working project with multi languages? I have implemented Customers Java Swing Application Demo with I18n helper. You can get full source code from BitBucket.

 

* Image credit: Designed by Freepik

Customers Java Swing Application Demo

I have developed the Customers Java Swing Application Demo. This application may be useful for developers who want to use Flamingo, Substance and SwingX.

Technology stack:

 

Model View Controller (MVC) vs Model View Presenter (MVP):

The Customers Java Swing Application Demo is built on MVC design pattern like Java Swing. MVP is also popular for Desktop and RIA. Which is best, there are many discussions. Martin Fowler has written very good explanations for GUI Architectures. You may like to read.

Models and Services:

I have written entities with NamedQueries in JPA and Hibernate. The services are very simple with Generic AbstractService.

You can get full source code and project resources from Bitbucket!

Original post is from my old devsniper.com blog!

 

* Customers Java Swing Application images and icons credits: iconsdb.com and icons8.com