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