123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- /*
- * 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. 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());
- }
- }
|