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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. localisation 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<String, LocaleData>();
  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 " + localeData.name);
  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. } else {
  58. throw new LocaleNotLoadedException(locale);
  59. }
  60. }
  61. public static String[] getShortMonthNames(String locale)
  62. throws LocaleNotLoadedException {
  63. if (cache.containsKey(locale)) {
  64. return cache.get(locale).shortMonthNames;
  65. } else {
  66. throw new LocaleNotLoadedException(locale);
  67. }
  68. }
  69. public static String[] getDayNames(String locale)
  70. throws LocaleNotLoadedException {
  71. if (cache.containsKey(locale)) {
  72. return cache.get(locale).dayNames;
  73. } else {
  74. throw new LocaleNotLoadedException(locale);
  75. }
  76. }
  77. public static String[] getShortDayNames(String locale)
  78. throws LocaleNotLoadedException {
  79. if (cache.containsKey(locale)) {
  80. return cache.get(locale).shortDayNames;
  81. } else {
  82. throw new LocaleNotLoadedException(locale);
  83. }
  84. }
  85. public static int getFirstDayOfWeek(String locale)
  86. throws LocaleNotLoadedException {
  87. if (cache.containsKey(locale)) {
  88. return cache.get(locale).firstDayOfWeek;
  89. } else {
  90. throw new LocaleNotLoadedException(locale);
  91. }
  92. }
  93. public static String getDateFormat(String locale)
  94. throws LocaleNotLoadedException {
  95. if (cache.containsKey(locale)) {
  96. return cache.get(locale).dateFormat;
  97. } else {
  98. throw new LocaleNotLoadedException(locale);
  99. }
  100. }
  101. public static boolean isTwelveHourClock(String locale)
  102. throws LocaleNotLoadedException {
  103. if (cache.containsKey(locale)) {
  104. return cache.get(locale).twelveHourClock;
  105. } else {
  106. throw new LocaleNotLoadedException(locale);
  107. }
  108. }
  109. public static String getClockDelimiter(String locale)
  110. throws LocaleNotLoadedException {
  111. if (cache.containsKey(locale)) {
  112. return cache.get(locale).hourMinuteDelimiter;
  113. } else {
  114. throw new LocaleNotLoadedException(locale);
  115. }
  116. }
  117. public static String[] getAmPmStrings(String locale)
  118. throws LocaleNotLoadedException {
  119. if (cache.containsKey(locale)) {
  120. return new String[] { cache.get(locale).am, cache.get(locale).pm };
  121. } else {
  122. throw new LocaleNotLoadedException(locale);
  123. }
  124. }
  125. public static void addLocales(List<LocaleData> localeDatas) {
  126. for (LocaleData localeData : localeDatas) {
  127. addLocale(localeData);
  128. }
  129. }
  130. private static Logger getLogger() {
  131. return Logger.getLogger(LocaleService.class.getName());
  132. }
  133. }