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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2011 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.Map;
  19. import java.util.Set;
  20. import com.google.gwt.core.client.JsArray;
  21. /**
  22. * Date / time etc. localisation service for all widgets. Caches all loaded
  23. * locales as JSONObjects.
  24. *
  25. * @author Vaadin Ltd.
  26. *
  27. */
  28. public class LocaleService {
  29. private static Map<String, ValueMap> cache = new HashMap<String, ValueMap>();
  30. private static String defaultLocale;
  31. public static void addLocale(ValueMap valueMap) {
  32. final String key = valueMap.getString("name");
  33. if (cache.containsKey(key)) {
  34. cache.remove(key);
  35. }
  36. cache.put(key, valueMap);
  37. if (cache.size() == 1) {
  38. setDefaultLocale(key);
  39. }
  40. }
  41. public static void setDefaultLocale(String locale) {
  42. defaultLocale = locale;
  43. }
  44. public static String getDefaultLocale() {
  45. return defaultLocale;
  46. }
  47. public static Set<String> getAvailableLocales() {
  48. return cache.keySet();
  49. }
  50. public static String[] getMonthNames(String locale)
  51. throws LocaleNotLoadedException {
  52. if (cache.containsKey(locale)) {
  53. final ValueMap l = cache.get(locale);
  54. return l.getStringArray("mn");
  55. } else {
  56. throw new LocaleNotLoadedException(locale);
  57. }
  58. }
  59. public static String[] getShortMonthNames(String locale)
  60. throws LocaleNotLoadedException {
  61. if (cache.containsKey(locale)) {
  62. final ValueMap l = cache.get(locale);
  63. return l.getStringArray("smn");
  64. } else {
  65. throw new LocaleNotLoadedException(locale);
  66. }
  67. }
  68. public static String[] getDayNames(String locale)
  69. throws LocaleNotLoadedException {
  70. if (cache.containsKey(locale)) {
  71. final ValueMap l = cache.get(locale);
  72. return l.getStringArray("dn");
  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. final ValueMap l = cache.get(locale);
  81. return l.getStringArray("sdn");
  82. } else {
  83. throw new LocaleNotLoadedException(locale);
  84. }
  85. }
  86. public static int getFirstDayOfWeek(String locale)
  87. throws LocaleNotLoadedException {
  88. if (cache.containsKey(locale)) {
  89. final ValueMap l = cache.get(locale);
  90. return l.getInt("fdow");
  91. } else {
  92. throw new LocaleNotLoadedException(locale);
  93. }
  94. }
  95. public static String getDateFormat(String locale)
  96. throws LocaleNotLoadedException {
  97. if (cache.containsKey(locale)) {
  98. final ValueMap l = cache.get(locale);
  99. return l.getString("df");
  100. } else {
  101. throw new LocaleNotLoadedException(locale);
  102. }
  103. }
  104. public static boolean isTwelveHourClock(String locale)
  105. throws LocaleNotLoadedException {
  106. if (cache.containsKey(locale)) {
  107. final ValueMap l = cache.get(locale);
  108. return l.getBoolean("thc");
  109. } else {
  110. throw new LocaleNotLoadedException(locale);
  111. }
  112. }
  113. public static String getClockDelimiter(String locale)
  114. throws LocaleNotLoadedException {
  115. if (cache.containsKey(locale)) {
  116. final ValueMap l = cache.get(locale);
  117. return l.getString("hmd");
  118. } else {
  119. throw new LocaleNotLoadedException(locale);
  120. }
  121. }
  122. public static String[] getAmPmStrings(String locale)
  123. throws LocaleNotLoadedException {
  124. if (cache.containsKey(locale)) {
  125. final ValueMap l = cache.get(locale);
  126. return l.getStringArray("ampm");
  127. } else {
  128. throw new LocaleNotLoadedException(locale);
  129. }
  130. }
  131. public static void addLocales(JsArray<ValueMap> valueMapArray) {
  132. for (int i = 0; i < valueMapArray.length(); i++) {
  133. addLocale(valueMapArray.get(i));
  134. }
  135. }
  136. }