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