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