]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/I18N.js
simple (minded?) dojo NLS page translator
[Evergreen.git] / Open-ILS / web / js / dojo / openils / I18N.js
1 /* ---------------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * Copyright (C) 2008  Equinox Software, Inc
4  * Mike Rylander <miker@esilibrary.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * ---------------------------------------------------------------------------
16  */
17
18 if(!dojo._hasResource["openils.I18N"]) {
19
20     dojo._hasResource["openils.I18N"] = true;
21     dojo.provide("openils.I18N");
22     dojo.require("fieldmapper.dojoData");
23     dojo.require("DojoSRF");
24         dojo.require("dojo.data.ItemFileWriteStore");
25
26     dojo.declare('openils.I18N', null, {});
27
28         openils.I18N.BaseLocales = fieldmapper.standardRequest( [ 'open-ils.fielder', 'open-ils.fielder.i18n_l.atomic'], [ { query : { code : { '!=' :  null }  } } ] );
29         openils.I18N.localeStore = new dojo.data.ItemFileWriteStore( { data : {identifier : 'locale', label : 'label', items : [] } } );
30         openils.I18N.BaseLocales = openils.I18N.BaseLocales.sort(
31         function(a, b) {
32             if(a.name > b.name) return 1;
33             if(a.name < b.name) return -1;
34             return 0;
35         }
36     );
37
38         for (var i in openils.I18N.BaseLocales) {
39                 openils.I18N.localeStore.newItem({ locale : openils.I18N.BaseLocales[i].code, label : openils.I18N.BaseLocales[i].name });
40         }
41
42         openils.I18N.getTranslations = function ( obj /* Fieldmapper object */,  field /* Field to translate */, locale /* optional locale */) {
43                 var classname = obj.classname;
44
45                 // XXX need to derive identity field from IDL...
46                 var ident_field = fieldmapper[classname].Identifier;
47                 var ident_value = obj[ident_field]();
48
49                 var fielder_args = { query : { fq_field : classname + '.' + field, identity_value : ident_value } };
50                 if (locale) fielder_args.translation = locale;
51
52                 var hash_list = fieldmapper.standardRequest( [ 'open-ils.fielder', 'open-ils.fielder.i18n.atomic'], [ fielder_args ] );
53                 var obj_list = dojo.map( hash_list, function (t) { return new fieldmapper.i18n().fromHash( t ) } );
54
55                 if (locale) return obj_list[0];
56                 return obj_list;
57         }
58
59     openils.I18N.translatePage = function () {
60
61         dojo.require('dojo.query');
62
63         var elements = dojo.query('*[i18n]');
64         if (!elements.length) return null;
65
66         dojo.forEach(elements, function(e){
67
68             var what = e.getAttribute('i18n');
69             var parts = what.match(/^(.+)\.([^.]+)$/);
70             var app = parts[0]; var bundle = parts[1];
71             if (!app || !bundle) return null;
72
73             if (!openils.I18N.translatePage.NLSCache[app][bundle]) {
74                 dojo.requireLocalization(app,bundle);
75                 openils.I18N.translatePage.NLSCache[app][bundle] = dojo.i18n.getLocalization(app,bundle);
76
77                 if (!openils.I18N.translatePage.NLSCache[app][bundle]) return null;
78             }
79
80             dojo.require('dojo.string');
81
82             var template = e.innerHTML;
83             var finalHTML = dojo.string.substitute( template, openils.I18N.translatePage.NLSCache[app][bundle] );
84
85             if (template == finalHTML) { // no subsititution occurred
86                 dojo.require("dojox.jsonPath");
87                 var transString = e.getAttribute('string') || template;
88                 finalHTML = dojox.jsonPath.query(
89                     openils.I18N.translatePage.NLSCache[app][bundle],
90                     '$.'+transString,
91                     {evalType:"RESULT"}
92                 );
93             }
94
95             if (finalHTML) e.innerHTML = finalHTML;
96
97         });
98     }
99     openils.I18N.translatePage.NLSCache = {}; // stash this on the function .. WHEEEE
100
101 }
102
103