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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package com.itmill.toolkit.terminal.gwt.client;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Set;
  5. import com.google.gwt.json.client.JSONArray;
  6. import com.google.gwt.json.client.JSONBoolean;
  7. import com.google.gwt.json.client.JSONNumber;
  8. import com.google.gwt.json.client.JSONObject;
  9. import com.google.gwt.json.client.JSONString;
  10. /**
  11. * Date / time etc. localisation service for all widgets. Caches all loaded
  12. * locales as JSONObjects.
  13. *
  14. * @author IT Mill Ltd.
  15. *
  16. */
  17. public class LocaleService {
  18. private static Map cache = new HashMap();
  19. private static String defaultLocale;
  20. public static void addLocale(JSONObject json) {
  21. String key = ((JSONString) json.get("name")).stringValue();
  22. if (cache.containsKey(key))
  23. cache.remove(key);
  24. cache.put(key, json);
  25. if (cache.size() == 1)
  26. setDefaultLocale(key);
  27. }
  28. public static void setDefaultLocale(String locale) {
  29. defaultLocale = locale;
  30. }
  31. public static String getDefaultLocale() {
  32. return defaultLocale;
  33. }
  34. public static Set getAvailableLocales() {
  35. return cache.keySet();
  36. }
  37. public static String[] getMonthNames(String locale)
  38. throws LocaleNotLoadedException {
  39. if (cache.containsKey(locale)) {
  40. JSONObject l = (JSONObject) cache.get(locale);
  41. JSONArray mn = (JSONArray) l.get("mn");
  42. String[] temp = new String[12];
  43. temp[0] = ((JSONString) mn.get(0)).stringValue();
  44. temp[1] = ((JSONString) mn.get(1)).stringValue();
  45. temp[2] = ((JSONString) mn.get(2)).stringValue();
  46. temp[3] = ((JSONString) mn.get(3)).stringValue();
  47. temp[4] = ((JSONString) mn.get(4)).stringValue();
  48. temp[5] = ((JSONString) mn.get(5)).stringValue();
  49. temp[6] = ((JSONString) mn.get(6)).stringValue();
  50. temp[7] = ((JSONString) mn.get(7)).stringValue();
  51. temp[8] = ((JSONString) mn.get(8)).stringValue();
  52. temp[9] = ((JSONString) mn.get(9)).stringValue();
  53. temp[10] = ((JSONString) mn.get(10)).stringValue();
  54. temp[11] = ((JSONString) mn.get(11)).stringValue();
  55. return temp;
  56. } else
  57. throw new LocaleNotLoadedException(locale);
  58. }
  59. public static String[] getShortMonthNames(String locale)
  60. throws LocaleNotLoadedException {
  61. if (cache.containsKey(locale)) {
  62. JSONObject l = (JSONObject) cache.get(locale);
  63. JSONArray smn = (JSONArray) l.get("smn");
  64. String[] temp = new String[12];
  65. temp[0] = ((JSONString) smn.get(0)).stringValue();
  66. temp[1] = ((JSONString) smn.get(1)).stringValue();
  67. temp[2] = ((JSONString) smn.get(2)).stringValue();
  68. temp[3] = ((JSONString) smn.get(3)).stringValue();
  69. temp[4] = ((JSONString) smn.get(4)).stringValue();
  70. temp[5] = ((JSONString) smn.get(5)).stringValue();
  71. temp[6] = ((JSONString) smn.get(6)).stringValue();
  72. temp[7] = ((JSONString) smn.get(7)).stringValue();
  73. temp[8] = ((JSONString) smn.get(8)).stringValue();
  74. temp[9] = ((JSONString) smn.get(9)).stringValue();
  75. temp[10] = ((JSONString) smn.get(10)).stringValue();
  76. temp[11] = ((JSONString) smn.get(11)).stringValue();
  77. return temp;
  78. } else
  79. throw new LocaleNotLoadedException(locale);
  80. }
  81. public static String[] getDayNames(String locale)
  82. throws LocaleNotLoadedException {
  83. if (cache.containsKey(locale)) {
  84. JSONObject l = (JSONObject) cache.get(locale);
  85. JSONArray dn = (JSONArray) l.get("dn");
  86. String[] temp = new String[7];
  87. temp[0] = ((JSONString) dn.get(0)).stringValue();
  88. temp[1] = ((JSONString) dn.get(1)).stringValue();
  89. temp[2] = ((JSONString) dn.get(2)).stringValue();
  90. temp[3] = ((JSONString) dn.get(3)).stringValue();
  91. temp[4] = ((JSONString) dn.get(4)).stringValue();
  92. temp[5] = ((JSONString) dn.get(5)).stringValue();
  93. temp[6] = ((JSONString) dn.get(6)).stringValue();
  94. return temp;
  95. } else
  96. throw new LocaleNotLoadedException(locale);
  97. }
  98. public static String[] getShortDayNames(String locale)
  99. throws LocaleNotLoadedException {
  100. if (cache.containsKey(locale)) {
  101. JSONObject l = (JSONObject) cache.get(locale);
  102. JSONArray sdn = (JSONArray) l.get("sdn");
  103. String[] temp = new String[7];
  104. temp[0] = ((JSONString) sdn.get(0)).stringValue();
  105. temp[1] = ((JSONString) sdn.get(1)).stringValue();
  106. temp[2] = ((JSONString) sdn.get(2)).stringValue();
  107. temp[3] = ((JSONString) sdn.get(3)).stringValue();
  108. temp[4] = ((JSONString) sdn.get(4)).stringValue();
  109. temp[5] = ((JSONString) sdn.get(5)).stringValue();
  110. temp[6] = ((JSONString) sdn.get(6)).stringValue();
  111. return temp;
  112. } else
  113. throw new LocaleNotLoadedException(locale);
  114. }
  115. public static int getFirstDayOfWeek(String locale)
  116. throws LocaleNotLoadedException {
  117. if (cache.containsKey(locale)) {
  118. JSONObject l = (JSONObject) cache.get(locale);
  119. JSONNumber fdow = (JSONNumber) l.get("fdow");
  120. return (int) fdow.getValue();
  121. } else
  122. throw new LocaleNotLoadedException(locale);
  123. }
  124. public static String getDateFormat(String locale)
  125. throws LocaleNotLoadedException {
  126. if (cache.containsKey(locale)) {
  127. JSONObject l = (JSONObject) cache.get(locale);
  128. JSONString df = (JSONString) l.get("df");
  129. return df.stringValue();
  130. } else
  131. throw new LocaleNotLoadedException(locale);
  132. }
  133. public static boolean isTwelveHourClock(String locale)
  134. throws LocaleNotLoadedException {
  135. if (cache.containsKey(locale)) {
  136. JSONObject l = (JSONObject) cache.get(locale);
  137. JSONBoolean thc = (JSONBoolean) l.get("thc");
  138. return thc.booleanValue();
  139. } else
  140. throw new LocaleNotLoadedException(locale);
  141. }
  142. public static String getClockDelimiter(String locale)
  143. throws LocaleNotLoadedException {
  144. if (cache.containsKey(locale)) {
  145. JSONObject l = (JSONObject) cache.get(locale);
  146. JSONString hmd = (JSONString) l.get("hmd");
  147. return hmd.stringValue();
  148. } else
  149. throw new LocaleNotLoadedException(locale);
  150. }
  151. public static String[] getAmPmStrings(String locale)
  152. throws LocaleNotLoadedException {
  153. if (cache.containsKey(locale)) {
  154. JSONObject l = (JSONObject) cache.get(locale);
  155. JSONArray ampm = (JSONArray) l.get("ampm");
  156. String[] temp = new String[2];
  157. temp[0] = ((JSONString) ampm.get(0)).stringValue();
  158. temp[1] = ((JSONString) ampm.get(1)).stringValue();
  159. return temp;
  160. } else
  161. throw new LocaleNotLoadedException(locale);
  162. }
  163. }