com.vaadin.client.LocaleService.java Source code

Java tutorial

Introduction

Here is the source code for com.vaadin.client.LocaleService.java

Source

/*
 * Copyright 2000-2018 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.vaadin.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import com.vaadin.shared.ui.ui.UIState.LocaleData;

/**
 * Date / time etc. localization service for all widgets. Caches all loaded
 * locales as JSONObjects.
 *
 * @author Vaadin Ltd.
 *
 */
public class LocaleService {

    private static Map<String, LocaleData> cache = new HashMap<>();

    private static String defaultLocale;

    public static void addLocale(LocaleData localeData) {
        final String key = localeData.name;
        if (cache.containsKey(key)) {
            cache.remove(key);
        }
        getLogger().fine("Received locale data for " + key);
        cache.put(key, localeData);
        if (cache.size() == 1) {
            setDefaultLocale(key);
        }
    }

    public static void setDefaultLocale(String locale) {
        defaultLocale = locale;
    }

    public static String getDefaultLocale() {
        return defaultLocale;
    }

    public static Set<String> getAvailableLocales() {
        return cache.keySet();
    }

    public static String[] getMonthNames(String locale) throws LocaleNotLoadedException {
        if (cache.containsKey(locale)) {
            return cache.get(locale).monthNames;
        }
        throw new LocaleNotLoadedException(locale);
    }

    public static String[] getShortMonthNames(String locale) throws LocaleNotLoadedException {
        if (cache.containsKey(locale)) {
            return cache.get(locale).shortMonthNames;
        }
        throw new LocaleNotLoadedException(locale);
    }

    public static String[] getDayNames(String locale) throws LocaleNotLoadedException {
        if (cache.containsKey(locale)) {
            return cache.get(locale).dayNames;
        }
        throw new LocaleNotLoadedException(locale);
    }

    public static String[] getShortDayNames(String locale) throws LocaleNotLoadedException {
        if (cache.containsKey(locale)) {
            return cache.get(locale).shortDayNames;
        }
        throw new LocaleNotLoadedException(locale);
    }

    public static int getFirstDayOfWeek(String locale) throws LocaleNotLoadedException {
        if (cache.containsKey(locale)) {
            return cache.get(locale).firstDayOfWeek;
        }
        throw new LocaleNotLoadedException(locale);
    }

    public static String getDateFormat(String locale) throws LocaleNotLoadedException {
        if (cache.containsKey(locale)) {
            return cache.get(locale).dateFormat;
        }
        throw new LocaleNotLoadedException(locale);
    }

    public static boolean isTwelveHourClock(String locale) throws LocaleNotLoadedException {
        if (cache.containsKey(locale)) {
            return cache.get(locale).twelveHourClock;
        }
        throw new LocaleNotLoadedException(locale);
    }

    public static String getClockDelimiter(String locale) throws LocaleNotLoadedException {
        if (cache.containsKey(locale)) {
            return cache.get(locale).hourMinuteDelimiter;
        }
        throw new LocaleNotLoadedException(locale);
    }

    public static String[] getAmPmStrings(String locale) throws LocaleNotLoadedException {
        if (cache.containsKey(locale)) {
            return new String[] { cache.get(locale).am, cache.get(locale).pm };
        }
        throw new LocaleNotLoadedException(locale);
    }

    public static void addLocales(List<LocaleData> localeDatas) {
        for (LocaleData localeData : localeDatas) {
            addLocale(localeData);
        }
    }

    private static Logger getLogger() {
        return Logger.getLogger(LocaleService.class.getName());
    }
}