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