You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LocaleService.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import java.util.logging.Logger;
  22. import com.vaadin.shared.ui.ui.UIState.LocaleData;
  23. /**
  24. * Date / time etc. localization service for all widgets. Caches all loaded
  25. * locales as JSONObjects.
  26. *
  27. * @author Vaadin Ltd.
  28. *
  29. */
  30. public class LocaleService {
  31. private static Map<String, LocaleData> cache = new HashMap<>();
  32. private static String defaultLocale;
  33. public static void addLocale(LocaleData localeData) {
  34. final String key = localeData.name;
  35. if (cache.containsKey(key)) {
  36. cache.remove(key);
  37. }
  38. getLogger().fine("Received locale data for " + key);
  39. cache.put(key, localeData);
  40. if (cache.size() == 1) {
  41. setDefaultLocale(key);
  42. }
  43. }
  44. public static void setDefaultLocale(String locale) {
  45. defaultLocale = locale;
  46. }
  47. public static String getDefaultLocale() {
  48. return defaultLocale;
  49. }
  50. public static Set<String> getAvailableLocales() {
  51. return cache.keySet();
  52. }
  53. public static String[] getMonthNames(String locale)
  54. throws LocaleNotLoadedException {
  55. if (cache.containsKey(locale)) {
  56. return cache.get(locale).monthNames;
  57. }
  58. throw new LocaleNotLoadedException(locale);
  59. }
  60. public static String[] getShortMonthNames(String locale)
  61. throws LocaleNotLoadedException {
  62. if (cache.containsKey(locale)) {
  63. return cache.get(locale).shortMonthNames;
  64. }
  65. throw new LocaleNotLoadedException(locale);
  66. }
  67. public static String[] getDayNames(String locale)
  68. throws LocaleNotLoadedException {
  69. if (cache.containsKey(locale)) {
  70. return cache.get(locale).dayNames;
  71. }
  72. throw new LocaleNotLoadedException(locale);
  73. }
  74. public static String[] getShortDayNames(String locale)
  75. throws LocaleNotLoadedException {
  76. if (cache.containsKey(locale)) {
  77. return cache.get(locale).shortDayNames;
  78. }
  79. throw new LocaleNotLoadedException(locale);
  80. }
  81. public static int getFirstDayOfWeek(String locale)
  82. throws LocaleNotLoadedException {
  83. if (cache.containsKey(locale)) {
  84. return cache.get(locale).firstDayOfWeek;
  85. }
  86. throw new LocaleNotLoadedException(locale);
  87. }
  88. public static String getDateFormat(String locale)
  89. throws LocaleNotLoadedException {
  90. if (cache.containsKey(locale)) {
  91. return cache.get(locale).dateFormat;
  92. }
  93. throw new LocaleNotLoadedException(locale);
  94. }
  95. public static boolean isTwelveHourClock(String locale)
  96. throws LocaleNotLoadedException {
  97. if (cache.containsKey(locale)) {
  98. return cache.get(locale).twelveHourClock;
  99. }
  100. throw new LocaleNotLoadedException(locale);
  101. }
  102. public static String getClockDelimiter(String locale)
  103. throws LocaleNotLoadedException {
  104. if (cache.containsKey(locale)) {
  105. return cache.get(locale).hourMinuteDelimiter;
  106. }
  107. throw new LocaleNotLoadedException(locale);
  108. }
  109. public static String[] getAmPmStrings(String locale)
  110. throws LocaleNotLoadedException {
  111. if (cache.containsKey(locale)) {
  112. return new String[] { cache.get(locale).am, cache.get(locale).pm };
  113. }
  114. throw new LocaleNotLoadedException(locale);
  115. }
  116. public static void addLocales(List<LocaleData> localeDatas) {
  117. for (LocaleData localeData : localeDatas) {
  118. addLocale(localeData);
  119. }
  120. }
  121. private static Logger getLogger() {
  122. return Logger.getLogger(LocaleService.class.getName());
  123. }
  124. }