com.vaadin.client.LocaleService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vaadin-client Show documentation
Show all versions of vaadin-client Show documentation
Vaadin is a web application framework for Rich Internet Applications (RIA).
Vaadin enables easy development and maintenance of fast and
secure rich web
applications with a stunning look and feel and a wide browser support.
It features a server-side architecture with the majority of the logic
running
on the server. Ajax technology is used at the browser-side to ensure a
rich
and interactive user experience.
/*
* Copyright 2000-2016 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. localisation service for all widgets. Caches all loaded
* locales as JSONObjects.
*
* @author Vaadin Ltd.
*
*/
public class LocaleService {
private static Map 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 " + localeData.name);
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 getAvailableLocales() {
return cache.keySet();
}
public static String[] getMonthNames(String locale)
throws LocaleNotLoadedException {
if (cache.containsKey(locale)) {
return cache.get(locale).monthNames;
} else {
throw new LocaleNotLoadedException(locale);
}
}
public static String[] getShortMonthNames(String locale)
throws LocaleNotLoadedException {
if (cache.containsKey(locale)) {
return cache.get(locale).shortMonthNames;
} else {
throw new LocaleNotLoadedException(locale);
}
}
public static String[] getDayNames(String locale)
throws LocaleNotLoadedException {
if (cache.containsKey(locale)) {
return cache.get(locale).dayNames;
} else {
throw new LocaleNotLoadedException(locale);
}
}
public static String[] getShortDayNames(String locale)
throws LocaleNotLoadedException {
if (cache.containsKey(locale)) {
return cache.get(locale).shortDayNames;
} else {
throw new LocaleNotLoadedException(locale);
}
}
public static int getFirstDayOfWeek(String locale)
throws LocaleNotLoadedException {
if (cache.containsKey(locale)) {
return cache.get(locale).firstDayOfWeek;
} else {
throw new LocaleNotLoadedException(locale);
}
}
public static String getDateFormat(String locale)
throws LocaleNotLoadedException {
if (cache.containsKey(locale)) {
return cache.get(locale).dateFormat;
} else {
throw new LocaleNotLoadedException(locale);
}
}
public static boolean isTwelveHourClock(String locale)
throws LocaleNotLoadedException {
if (cache.containsKey(locale)) {
return cache.get(locale).twelveHourClock;
} else {
throw new LocaleNotLoadedException(locale);
}
}
public static String getClockDelimiter(String locale)
throws LocaleNotLoadedException {
if (cache.containsKey(locale)) {
return cache.get(locale).hourMinuteDelimiter;
} else {
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 };
} else {
throw new LocaleNotLoadedException(locale);
}
}
public static void addLocales(List localeDatas) {
for (LocaleData localeData : localeDatas) {
addLocale(localeData);
}
}
private static Logger getLogger() {
return Logger.getLogger(LocaleService.class.getName());
}
}