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