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