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.toLowerCase()) + ' (' +  this.toName('c', cc.toUpperCase()) + ')';
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             return true;
64         });
65         return ret;
66         
67     },
68     /**
69      * List to Objects
70      * zh_HK,en to [ { code=zh_HK, title=Chinese }, .... ]
71      * @arg type type (c = country, l = lang)
72      * @arg codes list of languages
73      */
74     listToObjects: function (type, codes)
75     {
76         var ret = [];
77         var _this = this;
78         if (!codes.length) {
79             return ret;
80         };
81         var cl = codes.split(',');
82         Roo.each(cl , function(c) {
83             ret.push({
84                 code : c,
85                 title : _this.toName(type,c)
86             })
87         });
88         return ret;
89     },
90     
91     
92     
93     reader :   { // std. reader for i18n items.
94         root : 'data',
95         totalProperty : 'total',
96         id : 'code',
97         xtype : 'JsonReader',
98         fields : [
99             'code',
100             'title'
101         ]
102         },
103     
104     
105     
106     /**
107      * dataToProxy
108      * return proxy data for a pulldown.
109      * @param {String} type  eg. l,c,m (lang/country/money)
110      *    
111      * usage:
112      {
113       xtype: 'Store',
114       xns: Roo.data,
115       reader : Pman.I18n.reader,
116       proxy : {
117          xtype : 'MemoryProxy',
118          xns : Roo.data,
119          data : Pman.I18n.dataToProxy('l'), // eg. language
120          remoteSort : false,
121          sortInfo : { field : 'title' , direction : 'ASC' } 
122       }
123          * 
124          *}
125      * 
126      * 
127      */
128     
129     dataToProxy : function(type)
130     {
131         var lang = Pman.Login.authUser.lang || 'en';
132         return Pman.I18n.Data[lang][type];
133     },
134     
135     /**
136      * simpleStoreData:
137      * return a simplestore to be used by country/language combos
138      * eg.
139      * store: (function() {
140             return Pman.I18n.simpleStoreData('c');
141         })(),
142      *
143      * @param {Char} type (c,l,m)
144      * @param {function} (optional) filter language list
145      *     called with object { code: xxx , title: xxx }
146      *     if it exists then returning false will hide the entry.
147      */
148     
149     simpleStoreData : function(type, filter)
150     {
151         filter = typeof(filter) == 'undefined' ? false : filter;
152         var lang =  'en';
153         try {
154             lang = Pman.Login.authUser.lang;
155         } catch (E) {};
156         lang = lang || 'en';
157         var ret = [];
158         Roo.each(Pman.I18n.Data[lang][type], function (o) {
159             if (filter !== false && filter(o) === false) {
160                 return;
161             }
162             ret.push([ o.code, o.title ]);
163         });
164         
165          ret = ret.sort(function(a,b) {
166             if (a[0] == '**') return 1; // other always at end..
167             if (b[0] == '**') return -1; // other always at end..
168             return a[1]  > b[1] ? 1 : -1;
169         });
170         
171         return ret;
172     },
173     // DEPRECIATED... -- see dataToProxy
174     countryStore : function() { return {
175         
176         // load using HTTP
177         xtype: 'Store',
178         proxy: {
179             xtype: 'HttpProxy',
180             url: baseURL + '/Core/I18n/Country.html',
181             method: 'GET'
182         },
183         
184         reader: Pman.I18n.reader,
185         listeners : {
186              
187             loadexception : Pman.loadException
188
189         },
190         remoteSort: false,
191         sortInfo: {
192             field: 'title', direction: 'ASC'
193         }
194               
195     }},
196       // DEPRECIATED...
197     languageStore: function() {return{
198         // load using HTTP
199         xtype: 'Store',
200         proxy: {
201             xtype: 'HttpProxy',
202             url: baseURL + '/Core/I18n/Lang.html',
203             method: 'GET'
204         },
205         
206         reader: Pman.I18n.reader,
207         listeners : {
208              
209             loadexception : Pman.loadException
210     
211         },
212         remoteSort: false,
213         sortInfo: {
214             field: 'title', direction: 'ASC'
215         }
216     }},
217       // DEPRECIATED...
218     currencyStore: function() {return{
219         // load using HTTP
220         xtype: 'Store',
221         proxy: {
222             xtype: 'HttpProxy',
223             url: baseURL + '/Core/I18n/Currency.html',
224             method: 'GET'
225         },
226         
227         reader: Pman.I18n.reader,
228         listeners : {
229              
230             loadexception : Pman.loadException
231     
232         },
233         remoteSort: false,
234         sortInfo: {
235             field: 'title', direction: 'ASC'
236         }
237     }},
238       // DEPRECIATED...
239     country: function(cfg) {
240         var _this = this;
241         cfg = cfg || {};
242         return Roo.apply({
243                 // things that might need chnaging
244                 name : 'country_title',
245                 hiddenName : 'country',
246                 width : 290,
247                 listWidth : 300,
248                 fieldLabel : "Country",
249                 allowBlank : false,
250                 
251                 // less likely
252                 qtip : "Select Country",
253                 
254                 value : '',
255                 // very unlinkly
256                 xtype : 'ComboBox',   
257                 store: this.countryStore(),
258                 displayField:'title',
259                 valueField : 'code',
260                 typeAhead: false,
261                 editable: false,
262                 //mode: 'local',
263                 triggerAction: 'all',
264                 //emptyText:'Select a state...',
265                 selectOnFocus:true 
266                  
267             }, cfg);
268     },
269       // DEPRECIATED...
270     language: function(cfg) {
271                var _this = this;
272         cfg = cfg || {};
273         return Roo.apply({
274                 // things that might need chnaging
275                 
276                 name : 'language_title',
277                 hiddenName : 'language',
278                 width : 290,
279                 listWidth : 300,
280                 fieldLabel : "Language",
281                 allowBlank : false,
282                 
283                 // less likely
284                 qtip : "Select Language",
285                 
286                 value : '',
287                 // very unlinkly
288                 xtype : 'ComboBox',   
289                 store: this.languageStore(),
290                 displayField:'title',
291                 valueField : 'code',
292                 
293                 typeAhead: false,
294                 editable: false,
295                 //mode: 'local',
296                 triggerAction: 'all',
297                 //emptyText:'Select a state...',
298                 selectOnFocus:true 
299                 
300             }, cfg);
301     },
302          // DEPRECIATED...
303     currency: function(cfg) {
304         var _this = this;
305         cfg = cfg || {};
306         return Roo.apply({
307                 // things that might need chnaging
308                 name : 'currency_title',
309                 hiddenName : 'currency',
310                 width : 290,
311                 listWidth : 300,
312                 fieldLabel : "Currency",
313                 allowBlank : false,
314                 
315                 // less likely
316                 qtip : "Select Currency",
317                 
318                 value : '',
319                 // very unlinkly
320                 xtype : 'ComboBox',   
321                 store: this.currencyStore(),
322                 displayField:'code',
323                 valueField : 'code',
324                 typeAhead: false,
325                 editable: false,
326                 //mode: 'local',
327                 triggerAction: 'all',
328                 //emptyText:'Select a state...',
329                 selectOnFocus:true,
330                    tpl: new Ext.Template(
331                     '<div class="x-grid-cell-text x-btn button">',
332                         '{title} ({code})</b>',
333                     '</div>'
334                 ) 
335                  
336             }, cfg);
337     },
338       // DEPRECIATED...
339     languageList : function(cfg) {
340         cfg = cfg || {};
341          
342         return Roo.apply({
343                 
344                 name : 'language',
345                 //hiddenListName
346                 fieldLabel : "Language(s)",
347                 idField : 'code',
348                 nameField: 'title',
349                 renderer : function(d) {
350                     return String.format('{0}',  d.title );
351                 },
352                 
353                 
354                 xtype: 'ComboBoxLister',
355                 displayField:'title',
356                 value : '',
357                
358                 qtip : "Select a language to add.",
359                 selectOnFocus:true,
360                 allowBlank : true,
361                 width: 150,
362                 boxWidth: 300,
363                  
364                 store:  this.languageStore(),
365                
366                 editable: false,
367                 //typeAhead: true,
368                 forceSelection: true,
369                 //mode: 'local',
370                 triggerAction: 'all',
371                 tpl: new Ext.Template(
372                     '<div class="x-grid-cell-text x-btn button">',
373                         '{title}</b>',
374                     '</div>'
375                 ),
376                 queryParam: 'query[name]',
377                 loadingText: "Searching...",
378                 listWidth: 400,
379                
380                 minChars: 2,
381                // pageSize:20,
382                 setList : function(ar) {
383                     var _this = this;
384                     Roo.each(ar, function(a) {
385                         _this.addItem(a);
386                     });
387                 },
388                 toList : function() {
389                     var ret = [];
390                     this.items.each(function(a) {
391                         ret.push(a.data);
392                     });
393                     return ret;
394                 }
395                 
396                  
397             }, cfg);
398     },
399       // DEPRECIATED...
400     countryList : function(cfg) {
401         cfg = cfg || {};
402          
403          
404         return Roo.apply({
405                 
406                 name : 'countries',
407                 fieldLabel : "Country(s)",
408                 idField : 'code',
409                 nameField: 'title',
410                 renderer : function(d) {
411                     return String.format('{0}',  d.title );
412                 },
413                 
414                 
415                 xtype: 'ComboBoxLister',
416                 displayField:'title',
417                 value : '',
418                
419                 qtip : "Select a country to add.",
420                 selectOnFocus:true,
421                 allowBlank : true,
422                 width: 150,
423                 boxWidth: 300,
424                  
425                 store:  this.countryStore(), 
426                
427                 editable: false,
428                 //typeAhead: true,
429                 forceSelection: true,
430                 //mode: 'local',
431                 triggerAction: 'all',
432                 tpl: new Ext.Template(
433                     '<div class="x-grid-cell-text x-btn button">',
434                         '{title}</b>',
435                     '</div>'
436                 ),
437                 queryParam: 'query[name]',
438                 loadingText: "Searching...",
439                 listWidth: 400,
440                
441                 minChars: 2,
442                // pageSize:20,
443                 setList : function(ar) {
444                     var _this = this;
445                     Roo.each(ar, function(a) {
446                         _this.addItem(a);
447                     });
448                 },
449                 toList : function() {
450                     var ret = [];
451                     this.items.each(function(a) {
452                         ret.push(a.data);
453                     });
454                     return ret;
455                 }
456                 
457                  
458             }, cfg);
459     }
460      
461      
462     
463 };
464