All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.lightoze.gwt.i18n.client.LocaleFactory Maven / Gradle / Ivy

There is a newer version: 0.26
Show newest version
package net.lightoze.gwt.i18n.client;

import com.google.gwt.i18n.client.LocalizableResource;
import com.google.gwt.i18n.client.Messages;
import net.lightoze.gwt.i18n.LocaleFactoryProvider;

import java.util.HashMap;
import java.util.Map;

/**
 * Factory class for obtaining localization objects.
 *
 * @author Vladimir Kulev
 */
public class LocaleFactory {

    public static final String ENCODER = "ENCODER";

    private static final LocaleFactoryProvider provider = new LocaleFactoryProvider();
    private static final Map> cache = provider.createClassCache();

    /**
     * Get localization object for current locale.
     * 

* On server side current locale is determined dynamically by {@link net.lightoze.gwt.i18n.server.LocaleProvider} and can change in runtime for the same object. * * @param cls localization interface class * @param localization interface class * @return object implementing specified class */ public static T get(Class cls) { return get(cls, null); } /** * Get encoding localization object, which will encode all requests so that they can be decoded later by {@link net.lightoze.gwt.i18n.server.LocaleProxy#decode}. *

* The purpose is to separate complex (e.g. template-based) text generation and its localization for particular locale into two separate phases. *

* Supported only on server side. * * @param cls localization interface class * @param localization interface class * @return object implementing specified class */ public static T getEncoder(Class cls) { return get(cls, ENCODER); } /** * Get localization object for the specified locale. * * @param cls localization interface class * @param locale locale string * @param localization interface class * @return object implementing specified class */ @SuppressWarnings({"unchecked"}) public static T get(Class cls, String locale) { Map localeCache = getLocaleCache(cls); T m = (T) localeCache.get(locale); if (m != null) { return m; } synchronized (cache) { m = (T) localeCache.get(locale); if (m != null) { return m; } m = provider.create(cls, locale); put(cls, locale, m); return m; } } /** * Populate localization object cache for current locale. * * @param cls localization interface class * @param m localization object * @param localization interface class */ public static void put(Class cls, T m) { put(cls, null, m); } /** * Populate localization object cache for the specified locale. * * @param cls localization interface class * @param locale locale string * @param m localization object * @param localization interface class */ public static void put(Class cls, String locale, T m) { Map localeCache = getLocaleCache(cls); synchronized (cache) { localeCache.put(locale, m); } } private static Map getLocaleCache(Class cls) { Map map = cache.get(cls); if (map != null) { return map; } synchronized (cache) { map = cache.get(cls); if (map != null) { return map; } map = new HashMap(); cache.put(cls, map); return map; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy