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