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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import com.google.gwt.core.client.JsArray;
  9. /**
  10. * Date / time etc. localisation service for all widgets. Caches all loaded
  11. * locales as JSONObjects.
  12. *
  13. * @author IT Mill Ltd.
  14. *
  15. */
  16. public class LocaleService {
  17. private static Map<String, ValueMap> cache = new HashMap<String, ValueMap>();
  18. private static String defaultLocale;
  19. public static void addLocale(ValueMap valueMap) {
  20. final String key = valueMap.getString("name");
  21. if (cache.containsKey(key)) {
  22. cache.remove(key);
  23. }
  24. cache.put(key, valueMap);
  25. if (cache.size() == 1) {
  26. setDefaultLocale(key);
  27. }
  28. }
  29. public static void setDefaultLocale(String locale) {
  30. defaultLocale = locale;
  31. }
  32. public static String getDefaultLocale() {
  33. return defaultLocale;
  34. }
  35. public static Set getAvailableLocales() {
  36. return cache.keySet();
  37. }
  38. public static String[] getMonthNames(String locale)
  39. throws LocaleNotLoadedException {
  40. if (cache.containsKey(locale)) {
  41. final ValueMap l = cache.get(locale);
  42. return l.getStringArray("mn");
  43. } else {
  44. throw new LocaleNotLoadedException(locale);
  45. }
  46. }
  47. public static String[] getShortMonthNames(String locale)
  48. throws LocaleNotLoadedException {
  49. if (cache.containsKey(locale)) {
  50. final ValueMap l = cache.get(locale);
  51. return l.getStringArray("smn");
  52. } else {
  53. throw new LocaleNotLoadedException(locale);
  54. }
  55. }
  56. public static String[] getDayNames(String locale)
  57. throws LocaleNotLoadedException {
  58. if (cache.containsKey(locale)) {
  59. final ValueMap l = cache.get(locale);
  60. return l.getStringArray("dn");
  61. } else {
  62. throw new LocaleNotLoadedException(locale);
  63. }
  64. }
  65. public static String[] getShortDayNames(String locale)
  66. throws LocaleNotLoadedException {
  67. if (cache.containsKey(locale)) {
  68. final ValueMap l = cache.get(locale);
  69. return l.getStringArray("sdn");
  70. } else {
  71. throw new LocaleNotLoadedException(locale);
  72. }
  73. }
  74. public static int getFirstDayOfWeek(String locale)
  75. throws LocaleNotLoadedException {
  76. if (cache.containsKey(locale)) {
  77. final ValueMap l = cache.get(locale);
  78. return l.getInt("fdow");
  79. } else {
  80. throw new LocaleNotLoadedException(locale);
  81. }
  82. }
  83. public static String getDateFormat(String locale)
  84. throws LocaleNotLoadedException {
  85. if (cache.containsKey(locale)) {
  86. final ValueMap l = cache.get(locale);
  87. return l.getString("df");
  88. } else {
  89. throw new LocaleNotLoadedException(locale);
  90. }
  91. }
  92. public static boolean isTwelveHourClock(String locale)
  93. throws LocaleNotLoadedException {
  94. if (cache.containsKey(locale)) {
  95. final ValueMap l = cache.get(locale);
  96. return l.getBoolean("thc");
  97. } else {
  98. throw new LocaleNotLoadedException(locale);
  99. }
  100. }
  101. public static String getClockDelimiter(String locale)
  102. throws LocaleNotLoadedException {
  103. if (cache.containsKey(locale)) {
  104. final ValueMap l = cache.get(locale);
  105. return l.getString("hmd");
  106. } else {
  107. throw new LocaleNotLoadedException(locale);
  108. }
  109. }
  110. public static String[] getAmPmStrings(String locale)
  111. throws LocaleNotLoadedException {
  112. if (cache.containsKey(locale)) {
  113. final ValueMap l = cache.get(locale);
  114. return l.getStringArray("ampm");
  115. } else {
  116. throw new LocaleNotLoadedException(locale);
  117. }
  118. }
  119. public static void addLocales(JsArray<ValueMap> valueMapArray) {
  120. for (int i = 0; i < valueMapArray.length(); i++) {
  121. addLocale(valueMapArray.get(i));
  122. }
  123. }
  124. }