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     simpleStoreData : function(type)
135     {
136         var lang =  'en';
137         try {
138             lang = Pman.Login.authUser.lang;
139         } catch (E) {};
140         lang = lang || 'en';
141         var ret = [];
142         Roo.each(Pman.I18n.Data[lang][type], function (o) {
143             ret.push([ o.code, o.title ]);
144         });
145         
146          ret = ret.sort(function(a,b) {
147             if (a[0] == '**') return 1; // other always at end..
148             if (b[0] == '**') return -1; // other always at end..
149             return a[1]  > b[1] ? 1 : -1;
150         });
151         
152         return ret;
153     },
154     // DEPRECIATED... -- see dataToProxy
155     countryStore : function() { return {
156         
157         // load using HTTP
158         xtype: 'Store',
159         proxy: {
160             xtype: 'HttpProxy',
161             url: baseURL + '/I18N/Country.html',
162             method: 'GET'
163         },
164         
165         reader: Pman.I18n.reader,
166         listeners : {
167              
168             loadexception : Pman.loadException
169
170         },
171         remoteSort: false,
172         sortInfo: {
173             field: 'title', direction: 'ASC'
174         }
175               
176     }},
177       // DEPRECIATED...
178     languageStore: function() {return{
179         // load using HTTP
180         xtype: 'Store',
181         proxy: {
182             xtype: 'HttpProxy',
183             url: baseURL + '/I18N/Lang.html',
184             method: 'GET'
185         },
186         
187         reader: Pman.I18n.reader,
188         listeners : {
189              
190             loadexception : Pman.loadException
191     
192         },
193         remoteSort: false,
194         sortInfo: {
195             field: 'title', direction: 'ASC'
196         }
197     }},
198       // DEPRECIATED...
199     currencyStore: function() {return{
200         // load using HTTP
201         xtype: 'Store',
202         proxy: {
203             xtype: 'HttpProxy',
204             url: baseURL + '/I18N/Currency.html',
205             method: 'GET'
206         },
207         
208         reader: Pman.I18n.reader,
209         listeners : {
210              
211             loadexception : Pman.loadException
212     
213         },
214         remoteSort: false,
215         sortInfo: {
216             field: 'title', direction: 'ASC'
217         }
218     }},
219       // DEPRECIATED...
220     country: function(cfg) {
221         var _this = this;
222         cfg = cfg || {};
223         return Roo.apply({
224                 // things that might need chnaging
225                 name : 'country_title',
226                 hiddenName : 'country',
227                 width : 290,
228                 listWidth : 300,
229                 fieldLabel : "Country",
230                 allowBlank : false,
231                 
232                 // less likely
233                 qtip : "Select Country",
234                 
235                 value : '',
236                 // very unlinkly
237                 xtype : 'ComboBox',   
238                 store: this.countryStore(),
239                 displayField:'title',
240                 valueField : 'code',
241                 typeAhead: false,
242                 editable: false,
243                 //mode: 'local',
244                 triggerAction: 'all',
245                 //emptyText:'Select a state...',
246                 selectOnFocus:true 
247                  
248             }, cfg);
249     },
250       // DEPRECIATED...
251     language: function(cfg) {
252                var _this = this;
253         cfg = cfg || {};
254         return Roo.apply({
255                 // things that might need chnaging
256                 
257                 name : 'language_title',
258                 hiddenName : 'language',
259                 width : 290,
260                 listWidth : 300,
261                 fieldLabel : "Language",
262                 allowBlank : false,
263                 
264                 // less likely
265                 qtip : "Select Language",
266                 
267                 value : '',
268                 // very unlinkly
269                 xtype : 'ComboBox',   
270                 store: this.languageStore(),
271                 displayField:'title',
272                 valueField : 'code',
273                 
274                 typeAhead: false,
275                 editable: false,
276                 //mode: 'local',
277                 triggerAction: 'all',
278                 //emptyText:'Select a state...',
279                 selectOnFocus:true 
280                 
281             }, cfg);
282     },
283          // DEPRECIATED...
284     currency: function(cfg) {
285         var _this = this;
286         cfg = cfg || {};
287         return Roo.apply({
288                 // things that might need chnaging
289                 name : 'currency_title',
290                 hiddenName : 'currency',
291                 width : 290,
292                 listWidth : 300,
293                 fieldLabel : "Currency",
294                 allowBlank : false,
295                 
296                 // less likely
297                 qtip : "Select Currency",
298                 
299                 value : '',
300                 // very unlinkly
301                 xtype : 'ComboBox',   
302                 store: this.currencyStore(),
303                 displayField:'code',
304                 valueField : 'code',
305                 typeAhead: false,
306                 editable: false,
307                 //mode: 'local',
308                 triggerAction: 'all',
309                 //emptyText:'Select a state...',
310                 selectOnFocus:true,
311                    tpl: new Ext.Template(
312                     '<div class="x-grid-cell-text x-btn button">',
313                         '{title} ({code})</b>',
314                     '</div>'
315                 ) 
316                  
317             }, cfg);
318     },
319       // DEPRECIATED...
320     languageList : function(cfg) {
321         cfg = cfg || {};
322          
323         return Roo.apply({
324                 
325                 name : 'language',
326                 //hiddenListName
327                 fieldLabel : "Language(s)",
328                 idField : 'code',
329                 nameField: 'title',
330                 renderer : function(d) {
331                     return String.format('{0}',  d.title );
332                 },
333                 
334                 
335                 xtype: 'ComboBoxLister',
336                 displayField:'title',
337                 value : '',
338                
339                 qtip : "Select a language to add.",
340                 selectOnFocus:true,
341                 allowBlank : true,
342                 width: 150,
343                 boxWidth: 300,
344                  
345                 store:  this.languageStore(),
346                
347                 editable: false,
348                 //typeAhead: true,
349                 forceSelection: true,
350                 //mode: 'local',
351                 triggerAction: 'all',
352                 tpl: new Ext.Template(
353                     '<div class="x-grid-cell-text x-btn button">',
354                         '{title}</b>',
355                     '</div>'
356                 ),
357                 queryParam: 'query[name]',
358                 loadingText: "Searching...",
359                 listWidth: 400,
360                
361                 minChars: 2,
362                // pageSize:20,
363                 setList : function(ar) {
364                     var _this = this;
365                     Roo.each(ar, function(a) {
366                         _this.addItem(a);
367                     });
368                 },
369                 toList : function() {
370                     var ret = [];
371                     this.items.each(function(a) {
372                         ret.push(a.data);
373                     });
374                     return ret;
375                 }
376                 
377                  
378             }, cfg);
379     },
380       // DEPRECIATED...
381     countryList : function(cfg) {
382         cfg = cfg || {};
383          
384          
385         return Roo.apply({
386                 
387                 name : 'countries',
388                 fieldLabel : "Country(s)",
389                 idField : 'code',
390                 nameField: 'title',
391                 renderer : function(d) {
392                     return String.format('{0}',  d.title );
393                 },
394                 
395                 
396                 xtype: 'ComboBoxLister',
397                 displayField:'title',
398                 value : '',
399                
400                 qtip : "Select a country to add.",
401                 selectOnFocus:true,
402                 allowBlank : true,
403                 width: 150,
404                 boxWidth: 300,
405                  
406                 store:  this.countryStore(), 
407                
408                 editable: false,
409                 //typeAhead: true,
410                 forceSelection: true,
411                 //mode: 'local',
412                 triggerAction: 'all',
413                 tpl: new Ext.Template(
414                     '<div class="x-grid-cell-text x-btn button">',
415                         '{title}</b>',
416                     '</div>'
417                 ),
418                 queryParam: 'query[name]',
419                 loadingText: "Searching...",
420                 listWidth: 400,
421                
422                 minChars: 2,
423                // pageSize:20,
424                 setList : function(ar) {
425                     var _this = this;
426                     Roo.each(ar, function(a) {
427                         _this.addItem(a);
428                     });
429                 },
430                 toList : function() {
431                     var ret = [];
432                     this.items.each(function(a) {
433                         ret.push(a.data);
434                     });
435                     return ret;
436                 }
437                 
438                  
439             }, cfg);
440     }
441      
442      
443     
444 };
445