summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.5-sdk/frontend/framework/source/class/qx/locale/Manager.js
blob: 0b30e9456fe65612f748d54040745a4f453ca5cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2004-2007 1&1 Internet AG, Germany, http://www.1and1.org

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     EPL: http://www.eclipse.org/org/documents/epl-v10.php
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Sebastian Werner (wpbasti)
     * Andreas Ecker (ecker)
     * Fabian Jakobs (fjakobs)

************************************************************************ */


/**
 * Create a new instance of qx.locale.Manager
 */
qx.OO.defineClass("qx.locale.Manager", qx.manager.object.ObjectManager,
function() {
  qx.manager.object.ObjectManager.call(this);

  this._translationCatalog = {};
  this.setLocale(qx.core.Client.getInstance().getLocale() || this._defaultLocale);
});


/** current locale. locale is an language code like de, de_AT, en, en_GB, fr, ... */
qx.OO.addProperty({ name: "locale"});

qx.Proto._defaultLocale = "C";


/**
 * Get the language code of the currnt locale
 *
 * This is the first part of a locale definition. The language for "de_DE" would be "de"
 *
 * @return {String} language code
 */
qx.Proto.getLanguage = function() {
  return this._language;
};


/**
 * Get the territory code of the currnt locale
 *
 * This is the second part of a locale definition. The territory for "de_DE" would be "DE"
 *
 * @return {String} territory code
 */
qx.Proto.getTerritory = function() {
  return this.getLocale().split("_")[1] || "";
}


/**
 * Return the available application locales
 *
 * This corresponds to the Makefile APPLICATION_LOCALES setting
 *
 * @return {String[]} array of available locales
 */
qx.Proto.getAvailableLocales = function() {
  var locales = [];
  for (var locale in this._translationCatalog) {
    if (locale != this._defaultLocale) {
      locales.push(locale);
    }
  }
  return locales;
};


/**
 * Extract the language part from a locale.
 *
 * @param locale {String} locale to be used
 * @return {String} language
 */
qx.Proto._extractLanguage = function(locale) {
  var language;
  var pos = locale.indexOf("_");
  if (pos == -1) {
    language = locale;
  } else {
    language = locale.substring(0, pos);
  }
  return language;
};


qx.Proto._modifyLocale = function(propValue, propOldValue, propData) {
  this._locale = propValue;

  var pos = propValue.indexOf("_");
  this._language = this._extractLanguage(propValue);

  return true;
};


/**
 * Add a translation to the translation manager
 *
 * @param languageCode {String} language code of the translation like de, de_AT, en, en_GB, fr, ...
 * @param translationMap {Map} mapping of message identifiers (english text) to the target language
 */
qx.Proto.addTranslation = function(languageCode, translationMap) {

  if (this._translationCatalog[languageCode])
  {
    for (var key in translationMap) {
      this._translationCatalog[languageCode][key] = translationMap[key];
    }
  }
  else
  {
    this._translationCatalog[languageCode] = translationMap;
  }
};


/**
 * Translate a message
 * @see qx.lang.String.format
 *
 * @param messageId {String} message id (may contain format strings)
 * @param varargs {Object} variable number of argumes applied to the format string
 * @return {qx.locale.LocalizedString}
 */
qx.Class.tr = function(messageId, varargs)
{
  var args = qx.lang.Array.fromArguments(arguments);
  args.splice(0, 1);

  return new qx.locale.LocalizedString(messageId, args);
};


/**
 * Translate a plural message
 *
 * Depending on the third argument the plursl or the singular form is chosen.
 *
 * @see qx.lang.String.format
 *
 * @param singularMessageId {String} message id of the singular form (may contain format strings)
 * @param pluralMessageId {String} message id of the plural form (may contain format strings)
 * @param count {Integer} if greater than 1 the plural form otherwhise the singular form is returned.
 * @param varargs {Object} variable number of argumes applied to the format string
 * @return {qx.locale.LocalizedString}
 */
qx.Class.trn = function(singularMessageId, pluralMessageId, count, varargs)
{
  var args = qx.lang.Array.fromArguments(arguments);
  args.splice(0, 3);

  if (count > 1)
  {
    return new qx.locale.LocalizedString(pluralMessageId, args);
  }
  else
  {
    return new qx.locale.LocalizedString(singularMessageId, args);
  }
};


/**
 * Translate a message with translation hint
 *
 * Depending on the third argument the plursl or the singular form is chosen.
 *
 * @see qx.lang.String.format
 *
 * @param hint {String} hint for the translator of the message. Will be included in the .pot file.
 * @param messageId {String} message id (may contain format strings)
 * @param varargs {Object} variable number of argumes applied to the format string
 * @return {qx.locale.LocalizedString}
 */
qx.Class.trc = function(hint, messageId, varargs)
{
  var args = qx.lang.Array.fromArguments(arguments);
  args.splice(0, 2);

  return new qx.locale.LocalizedString(messageId, args);
}


/**
 * Mark the message for translation but return the original message.
 *
 * @param messageId {String} the message ID
 * @return {String} messageId
 */
qx.Class.marktr = function(messageId) {
  return messageId;
};


/**
 * Translate a message using the current locale and apply format string to the arguments.
 *
 * @param messageId {String} message id (may contain format strings)
 * @param args {Object[]} array of objects, which are inserted into the format string.
 * @param locale {String} optional locale to be used for translation
 * @return {String} translated message.
 */
qx.Proto.translate = function(messageId, args, locale)
{
  var txt;

  if (locale) {
    var language = this._extractLanguage(locale);
  } else {
    locale = this._locale;
    language = this._language;
  }

  if (!txt && this._translationCatalog[locale]) {
    txt = this._translationCatalog[locale][messageId];
  }

  if (!txt && this._translationCatalog[language]) {
    txt = this._translationCatalog[language][messageId];
  }

  if (!txt && this._translationCatalog[this._defaultLocale]) {
    txt = this._translationCatalog[this._defaultLocale][messageId];
  }

  if (!txt) {
    txt = messageId;
  }

  if (args.length > 0) {
    txt = qx.lang.String.format(txt, args)
  }
  return txt;
};


/*
---------------------------------------------------------------------------
  DEFER SINGLETON INSTANCE
---------------------------------------------------------------------------
*/

/**
 * Singleton Instance Getter
 */
qx.Class.getInstance = qx.lang.Function.returnInstance;