Pman.I18n.js
[Pman.Core] / Pman.I18n.js
1 //<script type="text/javascript">
2
3
4 /**
5 * A few usefull tools to convert language info...
6
7 * Our login details contain the available translation data..
8
9
10 * Languages are included in the main application
11 * using <script src="baseURL/Core/I18N/Data.js"></script>
12 * which set's up. Pman.I18n.data 
13
14
15
16 * includes standard pulldowns.
17 */
18
19 Pman.I18n = {
20     
21     /**
22      * turn zh_HK,en  => into Chinese(HK) , English
23      * @arg type type (c = country, l = lang)
24      * @arg codes list of languages
25      */
26     listToNames: function (type, codes)
27     {
28         var ret = [];
29         var _this = this;
30         var cl = codes.split(',');
31         Roo.each(cl , function(c) {
32             ret.push(_this.toName(type, c));
33         });
34         return ret.join(', ');
35     },
36     /**
37      * 
38      * turns zh_HK into a Chinese(HK)
39      * @arg type type (c = country, l = lang)
40      * @arg langcode language code (eg. zh_HK, UK etc.)
41      * 
42      */
43     toName: function(type, code) 
44     {
45         var ret = code;
46         var lang = Pman.Login.authUser.lang || 'en';
47         if (code.indexOf('_') > -1) {
48             var clang = code.split('_').shift();
49             var cc = code.split('_').pop();
50             return this.toName('l', clang) + ' (' +  cc + ')';
51         }
52         
53         
54         Roo.each(Pman.I18n.Data[lang][type], function(d) {
55             if (d.code == code) {
56                 ret = d.title;
57                 return false; // stop!
58             }
59         });
60         return ret;
61         
62     },
63     /**
64      * List to Objects
65      * zh_HK,en to [ { code=zh_HK, title=Chinese }, .... ]
66      * @arg type type (c = country, l = lang)
67      * @arg codes list of languages
68      */
69     listToObjects: function (type, codes)
70     {
71         var ret = [];
72         var _this = this;
73         if (!codes.length) {
74             return ret;
75         };
76         var cl = codes.split(',');
77         Roo.each(cl , function(c) {
78             ret.push({
79                 code : c,
80                 title : _this.toName(type,c)
81             })
82         });
83         return ret;
84     },
85     
86     
87     
88     reader :   { // std. reader for i18n items.
89         root : 'data',
90         totalProperty : 'total',
91         id : 'code',
92         xtype : 'JsonReader',
93         fields : [
94             'code',
95             'title'
96         ]
97         },
98     
99     
100     
101     /**
102      * dataToProxy
103      * return proxy data for a pulldown.
104      * @param {String} type  eg. l,c,m (lang/country/money)
105      *    
106      * usage:
107      {
108       xtype: 'Store',
109       xns: Roo.data,
110       reader : Pman.I18n.reader,
111       proxy : {
112          xtype : 'MemoryProxy',
113          xns : Roo.data,
114          data : Pman.I18n.dataToProxy('l'), // eg. language
115          remoteSort : false,
116          sortInfo : { field : 'title' , direction : 'ASC' } 
117       }
118          * 
119          *}
120      * 
121      * 
122      */
123     
124     dataToProxy : function(type)
125     {
126         var lang = Pman.Login.authUser.lang || 'en';
127         return Pman.I18n.Data[lang][type];
128     },
129     
130     simpleStoreData : function(type)
131     {
132         var lang = Pman.Login.authUser.lang || 'en';
133         var ret = [];
134         Roo.each(Pman.I18n.Data[lang][type], function (o) {
135             ret.push([ o.code, o.title ]);
136         });
137         
138         var xret = ret.sort(function(a,b) {
139             if (a[0] == '**') return -1; // other always at end..
140             if (b[0] == '**') return 1; // other always at end..
141             return a[1]  > b[1] ? 1 : -1;
142         });
143         
144         return xret;
145     },
146     // DEPRECIATED... -- see dataToProxy
147     countryStore : function() { return {
148         
149         // load using HTTP
150         xtype: 'Store',
151         proxy: {
152             xtype: 'HttpProxy',
153             url: baseURL + '/I18N/Country.html',
154             method: 'GET'
155         },
156         
157         reader: Pman.I18n.reader,
158         listeners : {
159              
160             loadexception : Pman.loadException
161
162         },
163         remoteSort: false,
164         sortInfo: {
165             field: 'title', direction: 'ASC'
166         }
167               
168     }},
169       // DEPRECIATED...
170     languageStore: function() {return{
171         // load using HTTP
172         xtype: 'Store',
173         proxy: {
174             xtype: 'HttpProxy',
175             url: baseURL + '/I18N/Lang.html',
176             method: 'GET'
177         },
178         
179         reader: Pman.I18n.reader,
180         listeners : {
181              
182             loadexception : Pman.loadException
183     
184         },
185         remoteSort: false,
186         sortInfo: {
187             field: 'title', direction: 'ASC'
188         }
189     }},
190       // DEPRECIATED...
191     currencyStore: function() {return{
192         // load using HTTP
193         xtype: 'Store',
194         proxy: {
195             xtype: 'HttpProxy',
196             url: baseURL + '/I18N/Currency.html',
197             method: 'GET'
198         },
199         
200         reader: Pman.I18n.reader,
201         listeners : {
202              
203             loadexception : Pman.loadException
204     
205         },
206         remoteSort: false,
207         sortInfo: {
208             field: 'title', direction: 'ASC'
209         }
210     }},
211       // DEPRECIATED...
212     country: function(cfg) {
213         var _this = this;
214         cfg = cfg || {};
215         return Roo.apply({
216                 // things that might need chnaging
217                 name : 'country_title',
218                 hiddenName : 'country',
219                 width : 290,
220                 listWidth : 300,
221                 fieldLabel : "Country",
222                 allowBlank : false,
223                 
224                 // less likely
225                 qtip : "Select Country",
226                 
227                 value : '',
228                 // very unlinkly
229                 xtype : 'ComboBox',   
230                 store: this.countryStore(),
231                 displayField:'title',
232                 valueField : 'code',
233                 typeAhead: false,
234                 editable: false,
235                 //mode: 'local',
236                 triggerAction: 'all',
237                 //emptyText:'Select a state...',
238                 selectOnFocus:true 
239                  
240             }, cfg);
241     },
242       // DEPRECIATED...
243     language: function(cfg) {
244                var _this = this;
245         cfg = cfg || {};
246         return Roo.apply({
247                 // things that might need chnaging
248                 
249                 name : 'language_title',
250                 hiddenName : 'language',
251                 width : 290,
252                 listWidth : 300,
253                 fieldLabel : "Language",
254                 allowBlank : false,
255                 
256                 // less likely
257                 qtip : "Select Language",
258                 
259                 value : '',
260                 // very unlinkly
261                 xtype : 'ComboBox',   
262                 store: this.languageStore(),
263                 displayField:'title',
264                 valueField : 'code',
265                 
266                 typeAhead: false,
267                 editable: false,
268                 //mode: 'local',
269                 triggerAction: 'all',
270                 //emptyText:'Select a state...',
271                 selectOnFocus:true 
272                 
273             }, cfg);
274     },
275          // DEPRECIATED...
276     currency: function(cfg) {
277         var _this = this;
278         cfg = cfg || {};
279         return Roo.apply({
280                 // things that might need chnaging
281                 name : 'currency_title',
282                 hiddenName : 'currency',
283                 width : 290,
284                 listWidth : 300,
285                 fieldLabel : "Currency",
286                 allowBlank : false,
287                 
288                 // less likely
289                 qtip : "Select Currency",
290                 
291                 value : '',
292                 // very unlinkly
293                 xtype : 'ComboBox',   
294                 store: this.currencyStore(),
295                 displayField:'code',
296                 valueField : 'code',
297                 typeAhead: false,
298                 editable: false,
299                 //mode: 'local',
300                 triggerAction: 'all',
301                 //emptyText:'Select a state...',
302                 selectOnFocus:true,
303                    tpl: new Ext.Template(
304                     '<div class="x-grid-cell-text x-btn button">',
305                         '{title} ({code})</b>',
306                     '</div>'
307                 ) 
308                  
309             }, cfg);
310     },
311       // DEPRECIATED...
312     languageList : function(cfg) {
313         cfg = cfg || {};
314          
315         return Roo.apply({
316                 
317                 name : 'language',
318                 //hiddenListName
319                 fieldLabel : "Language(s)",
320                 idField : 'code',
321                 nameField: 'title',
322                 renderer : function(d) {
323                     return String.format('{0}',  d.title );
324                 },
325                 
326                 
327                 xtype: 'ComboBoxLister',
328                 displayField:'title',
329                 value : '',
330                
331                 qtip : "Select a language to add.",
332                 selectOnFocus:true,
333                 allowBlank : true,
334                 width: 150,
335                 boxWidth: 300,
336                  
337                 store:  this.languageStore(),
338                
339                 editable: false,
340                 //typeAhead: true,
341                 forceSelection: true,
342                 //mode: 'local',
343                 triggerAction: 'all',
344                 tpl: new Ext.Template(
345                     '<div class="x-grid-cell-text x-btn button">',
346                         '{title}</b>',
347                     '</div>'
348                 ),
349                 queryParam: 'query[name]',
350                 loadingText: "Searching...",
351                 listWidth: 400,
352                
353                 minChars: 2,
354                // pageSize:20,
355                 setList : function(ar) {
356                     var _this = this;
357                     Roo.each(ar, function(a) {
358                         _this.addItem(a);
359                     });
360                 },
361                 toList : function() {
362                     var ret = [];
363                     this.items.each(function(a) {
364                         ret.push(a.data);
365                     });
366                     return ret;
367                 }
368                 
369                  
370             }, cfg);
371     },
372       // DEPRECIATED...
373     countryList : function(cfg) {
374         cfg = cfg || {};
375          
376          
377         return Roo.apply({
378                 
379                 name : 'countries',
380                 fieldLabel : "Country(s)",
381                 idField : 'code',
382                 nameField: 'title',
383                 renderer : function(d) {
384                     return String.format('{0}',  d.title );
385                 },
386                 
387                 
388                 xtype: 'ComboBoxLister',
389                 displayField:'title',
390                 value : '',
391                
392                 qtip : "Select a country to add.",
393                 selectOnFocus:true,
394                 allowBlank : true,
395                 width: 150,
396                 boxWidth: 300,
397                  
398                 store:  this.countryStore(), 
399                
400                 editable: false,
401                 //typeAhead: true,
402                 forceSelection: true,
403                 //mode: 'local',
404                 triggerAction: 'all',
405                 tpl: new Ext.Template(
406                     '<div class="x-grid-cell-text x-btn button">',
407                         '{title}</b>',
408                     '</div>'
409                 ),
410                 queryParam: 'query[name]',
411                 loadingText: "Searching...",
412                 listWidth: 400,
413                
414                 minChars: 2,
415                // pageSize:20,
416                 setList : function(ar) {
417                     var _this = this;
418                     Roo.each(ar, function(a) {
419                         _this.addItem(a);
420                     });
421                 },
422                 toList : function() {
423                     var ret = [];
424                     this.items.each(function(a) {
425                         ret.push(a.data);
426                     });
427                     return ret;
428                 }
429                 
430                  
431             }, cfg);
432     }
433      
434      
435     
436 };
437
438