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