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