PageRenderTime 25ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/web-app/qooxdoo/framework/source/class/qx/locale/Manager.js

https://github.com/flomotlik/grails-qooxdoo
JavaScript | 450 lines | 220 code | 79 blank | 151 comment | 47 complexity | 7fb84c405a37e9c596386e628cc95509 MD5 | raw file
Possible License(s): Unlicense, CC-BY-SA-3.0
  1. /* ************************************************************************
  2. qooxdoo - the new era of web development
  3. http://qooxdoo.org
  4. Copyright:
  5. 2004-2008 1&1 Internet AG, Germany, http://www.1und1.de
  6. License:
  7. LGPL: http://www.gnu.org/licenses/lgpl.html
  8. EPL: http://www.eclipse.org/org/documents/epl-v10.php
  9. See the LICENSE file in the project's top-level directory for details.
  10. Authors:
  11. * Sebastian Werner (wpbasti)
  12. * Andreas Ecker (ecker)
  13. * Fabian Jakobs (fjakobs)
  14. ************************************************************************ */
  15. /*
  16. #require(qx.event.dispatch.Direct)
  17. #require(qx.bom.client.Locale)
  18. #require(qx.locale.LocalizedString)
  19. */
  20. /**
  21. * The qx.locale.Manager provides static translation methods (like tr()) and
  22. * general locale information.
  23. */
  24. qx.Class.define("qx.locale.Manager",
  25. {
  26. type : "singleton",
  27. extend : qx.core.Object,
  28. /*
  29. *****************************************************************************
  30. CONSTRUCTOR
  31. *****************************************************************************
  32. */
  33. construct : function()
  34. {
  35. this.base(arguments);
  36. this.__translations = qx.$$translations || {};
  37. this.__locales = qx.$$locales || {};
  38. var clazz = qx.bom.client.Locale;
  39. var locale = clazz.LOCALE;
  40. var variant = clazz.VARIANT;
  41. if (variant !== "") {
  42. locale += "_" + variant;
  43. }
  44. this.setLocale(locale || this.__defaultLocale);
  45. },
  46. /*
  47. *****************************************************************************
  48. STATICS
  49. *****************************************************************************
  50. */
  51. statics :
  52. {
  53. /**
  54. * Translate a message
  55. *
  56. * @param messageId {String} message id (may contain format strings)
  57. * @param varargs {Object} variable number of arguments applied to the format string
  58. * @return {String} The translated string
  59. * @see qx.lang.String.format
  60. */
  61. tr : function(messageId, varargs)
  62. {
  63. var args = qx.lang.Array.fromArguments(arguments);
  64. args.splice(0, 1);
  65. return qx.locale.Manager.getInstance().translate(messageId, args);
  66. },
  67. /**
  68. * Translate a plural message
  69. *
  70. * Depending on the third argument the plural or the singular form is chosen.
  71. *
  72. * @param singularMessageId {String} message id of the singular form (may contain format strings)
  73. * @param pluralMessageId {String} message id of the plural form (may contain format strings)
  74. * @param count {Integer} singular form if equals 1, otherwise plural
  75. * @param varargs {Object} variable number of arguments applied to the format string
  76. * @return {String} The translated string
  77. * @see qx.lang.String.format
  78. */
  79. trn : function(singularMessageId, pluralMessageId, count, varargs)
  80. {
  81. var args = qx.lang.Array.fromArguments(arguments);
  82. args.splice(0, 3);
  83. // assumes "Two forms, singular used for one only" (seems to be the most common form)
  84. // (http://www.gnu.org/software/gettext/manual/html_node/gettext_150.html#Plural-forms)
  85. // closely related with bug #745
  86. if (count != 1) {
  87. return qx.locale.Manager.getInstance().translate(pluralMessageId, args);
  88. } else {
  89. return qx.locale.Manager.getInstance().translate(singularMessageId, args);
  90. }
  91. },
  92. /**
  93. * Translate a message with translation hint
  94. *
  95. * @param hint {String} hint for the translator of the message. Will be included in the .po file.
  96. * @param messageId {String} message id (may contain format strings)
  97. * @param varargs {Object} variable number of arguments applied to the format string
  98. * @return {String} The translated string
  99. * @see qx.lang.String.format
  100. */
  101. trc : function(hint, messageId, varargs)
  102. {
  103. var args = qx.lang.Array.fromArguments(arguments);
  104. args.splice(0, 2);
  105. return qx.locale.Manager.getInstance().translate(messageId, args);
  106. },
  107. /**
  108. * Mark the message for translation but return the original message.
  109. *
  110. * @param messageId {String} the message ID
  111. * @return {String} messageId
  112. */
  113. marktr : function(messageId) {
  114. return messageId;
  115. }
  116. },
  117. /*
  118. *****************************************************************************
  119. PROPERTIES
  120. *****************************************************************************
  121. */
  122. properties :
  123. {
  124. /** current locale. locale is an language code like de, de_AT, en, en_GB, fr, ... */
  125. locale :
  126. {
  127. check : "String",
  128. nullable : true,
  129. apply : "_applyLocale",
  130. event : "changeLocale"
  131. }
  132. },
  133. /*
  134. *****************************************************************************
  135. MEMBERS
  136. *****************************************************************************
  137. */
  138. members :
  139. {
  140. __defaultLocale : "C",
  141. __locale : null,
  142. __language : null,
  143. __translations : null,
  144. __locales : null,
  145. /**
  146. * Get the language code of the current locale
  147. *
  148. * This is the first part of a locale definition. The language for "de_DE" would be "de"
  149. *
  150. * @return {String} language code
  151. */
  152. getLanguage : function() {
  153. return this.__language;
  154. },
  155. /**
  156. * Get the territory code of the current locale
  157. *
  158. * This is the second part of a locale definition. The territory for "de_DE" would be "DE"
  159. *
  160. * @return {String} territory code
  161. */
  162. getTerritory : function() {
  163. return this.getLocale().split("_")[1] || "";
  164. },
  165. /**
  166. * Return the available application locales
  167. *
  168. * This corresponds to the Makefile APPLICATION_LOCALES setting
  169. *
  170. * @return {String[]} array of available locales
  171. */
  172. getAvailableLocales : function()
  173. {
  174. var locales = [];
  175. for (var locale in this.__locales)
  176. {
  177. if (locale != this.__defaultLocale) {
  178. locales.push(locale);
  179. }
  180. }
  181. return locales;
  182. },
  183. /**
  184. * Extract the language part from a locale.
  185. *
  186. * @param locale {String} locale to be used
  187. * @return {String} language
  188. */
  189. __extractLanguage : function(locale)
  190. {
  191. var language;
  192. var pos = locale.indexOf("_");
  193. if (pos == -1) {
  194. language = locale;
  195. } else {
  196. language = locale.substring(0, pos);
  197. }
  198. return language;
  199. },
  200. // property apply
  201. _applyLocale : function(value, old)
  202. {
  203. this.__locale = value;
  204. this.__language = this.__extractLanguage(value);
  205. },
  206. /**
  207. * Add a translation to the translation manager
  208. *
  209. * @param languageCode {String} language code of the translation like de, de_AT, en, en_GB, fr, ...
  210. * @param translationMap {Map} mapping of message identifiers (english text) to the target language
  211. * @return {void}
  212. */
  213. addTranslation : function(languageCode, translationMap)
  214. {
  215. var catalog = this.__translations;
  216. if (catalog[languageCode])
  217. {
  218. for (var key in translationMap) {
  219. catalog[languageCode][key] = translationMap[key];
  220. }
  221. }
  222. else
  223. {
  224. catalog[languageCode] = translationMap;
  225. }
  226. },
  227. /**
  228. * Add a localization to the localization manager
  229. *
  230. * @param localeCode {String} locale code of the translation like de, de_AT, en, en_GB, fr, ...
  231. * @param translationMap {Map} mapping of locale keys to the target locale
  232. * @return {void}
  233. */
  234. addLocale : function(localeCode, translationMap)
  235. {
  236. var catalog = this.__locales;
  237. if (catalog[localeCode])
  238. {
  239. for (var key in translationMap) {
  240. catalog[localeCode][key] = translationMap[key];
  241. }
  242. }
  243. else
  244. {
  245. catalog[localeCode] = translationMap;
  246. }
  247. },
  248. /**
  249. * Translate a message using the current locale and apply format string to the arguments.
  250. *
  251. * @param messageId {String} message id (may contain format strings)
  252. * @param args {Object[]} array of objects, which are inserted into the format string.
  253. * @param locale {String} optional locale to be used for translation
  254. * @return {String} translated message.
  255. */
  256. translate : function(messageId, args, locale)
  257. {
  258. var txt;
  259. var catalog = this.__translations;
  260. if (!catalog) {
  261. return messageId;
  262. }
  263. if (locale) {
  264. var language = this.__extractLanguage(locale);
  265. }
  266. else
  267. {
  268. locale = this.__locale;
  269. language = this.__language;
  270. }
  271. if (!txt && catalog[locale]) {
  272. txt = catalog[locale][messageId];
  273. }
  274. if (!txt && catalog[language]) {
  275. txt = catalog[language][messageId];
  276. }
  277. if (!txt && catalog[this.__defaultLocale]) {
  278. txt = catalog[this.__defaultLocale][messageId];
  279. }
  280. if (!txt) {
  281. txt = messageId;
  282. }
  283. if (args.length > 0)
  284. {
  285. var translatedArgs = [];
  286. for ( var i = 0; i < args.length; i++)
  287. {
  288. var arg = args[i];
  289. if (arg && arg.translate) {
  290. translatedArgs[i] = arg.translate();
  291. } else {
  292. translatedArgs[i] = arg;
  293. }
  294. }
  295. txt = qx.lang.String.format(txt, translatedArgs);
  296. }
  297. if (qx.core.Variant.isSet("qx.dynlocale", "on")) {
  298. txt = new qx.locale.LocalizedString(txt, messageId, args);
  299. }
  300. return txt;
  301. },
  302. /**
  303. * Provide localisation (CLDR) data
  304. *
  305. * @param messageId {String} message id (may contain format strings)
  306. * @param args {Object[]} array of objects, which are inserted into the format string.
  307. * @param locale {String} optional locale to be used for translation
  308. * @return {String} translated message.
  309. */
  310. localize : function(messageId, args, locale)
  311. {
  312. var txt;
  313. var catalog = this.__locales;
  314. if (!catalog) {
  315. return messageId;
  316. }
  317. if (locale) {
  318. var language = this.__extractLanguage(locale);
  319. }
  320. else
  321. {
  322. locale = this.__locale;
  323. language = this.__language;
  324. }
  325. if (!txt && catalog[locale]) {
  326. txt = catalog[locale][messageId];
  327. }
  328. if (!txt && catalog[language]) {
  329. txt = catalog[language][messageId];
  330. }
  331. if (!txt && catalog[this.__defaultLocale]) {
  332. txt = catalog[this.__defaultLocale][messageId];
  333. }
  334. if (!txt) {
  335. txt = messageId;
  336. }
  337. if (args.length > 0)
  338. {
  339. var translatedArgs = [];
  340. for ( var i = 0; i < args.length; i++)
  341. {
  342. var arg = args[i];
  343. if (arg.translate) {
  344. translatedArgs[i] = arg.translate();
  345. } else {
  346. translatedArgs[i] = arg;
  347. }
  348. }
  349. txt = qx.lang.String.format(txt, translatedArgs);
  350. }
  351. if (qx.core.Variant.isSet("qx.dynlocale", "on")) {
  352. txt = new qx.locale.LocalizedString(txt, messageId, args);
  353. }
  354. return txt;
  355. }
  356. },
  357. /*
  358. *****************************************************************************
  359. DESTRUCTOR
  360. *****************************************************************************
  361. */
  362. destruct : function() {
  363. this.__translations = this.__locales = null;
  364. }
  365. });