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