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