source: Dev/branches/rest-dojo-ui/client/util/buildscripts/cldr/alias.js @ 273

Last change on this file since 273 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 10.9 KB
Line 
1/**
2 * There are basically two kinds of alias in CLDR:
3 * 1. locale alias e.g.
4 *    in xml, <alias source="locale" path="......"/>,
5 *    in gernated JSON bunle, xxxx@localeAlias:{'target':'xxx', 'bundle':'xxxx'}
6 * 2. other locale alias e.g.
7 *    in xml, currently only like <alias source="fr" path="//ldml"/>
8 * #1 is supported by this 'alias.js',
9 * #2 is covered by 'specialLocale.js' and may need enhancement for future CLDR versions.
10 */
11
12djConfig={baseUrl: "../../../dojo/"};
13
14load("../../../dojo/dojo.js");
15load("../jslib/logger.js");
16load("../jslib/fileUtil.js");
17load("cldrUtil.js");
18
19dojo.require("dojo.i18n");
20
21var dir/*String*/ = arguments[0];// ${dojo}/dojo/cldr/nls
22var logDir = arguments[1];
23var logStr = "";
24
25//Add new bundles to the list so that they will be aliased according to the ldml spec.
26var BUNDLES = ['gregorian','hebrew','islamic','islamic-civil','buddhist'];
27
28var LOCALE_ALIAS_MARK = '@localeAlias';
29var LOCALE_ALIAS_SOURCE_PROPERTY = 'source';
30var LOCALE_ALIAS_TARGET_PROPERTY = 'target';
31var LOCALE_ALIAS_TARGET_BUNDLE = 'bundle';
32var localeAliasPaths = [];/**/
33var records = {};/*{property : boolean}, record whether a property has been calculated for alias path*/
34var updated = false;
35
36print('alias.js...');
37
38for(var i = 0; i < BUNDLES.length; i++){
39        var regExp = new RegExp('\/' + BUNDLES[i] + '\.js$'); //e.g. new RegExp('\/gregorian\.js$')
40        var fileList = fileUtil.getFilteredFileList(dir, regExp, true);
41       
42        for(var j = 0; j < fileList.length; j++){
43                var jsFileName = new String(fileList[j]); //Java String
44                var jsFilePath = jsFileName.split("/");
45                var locale = jsFilePath[jsFilePath.length-2];
46                if(locale=="nls"){continue;} // no need for root bundle
47                try{
48//                      dojo.i18n._requireLocalization('dojo.cldr', BUNDLES[i], locale); //declare bundle
49                        var bundle = dojo.i18n.getLocalization('dojo.cldr', BUNDLES[i], locale); //get bundle
50                        var nativeSrcBundle = getNativeBundle(jsFileName);//bundle not flattened
51                }catch(e){/*logStr += "alias: an exception occurred: "+e;/* simply ignore if no bundle found*/}
52               
53                if(!bundle) continue;
54               
55                updated = false;
56                //logStr += locale + ":" + BUNDLES[i] + "=========================================================================\n";
57               
58                _calculateAliasPath(bundle, BUNDLES[i]);
59                //logStr += "all alias paths=" + dojo.toJson(localeAliasPaths) + "\n";
60                               
61                _processLocaleAlias(localeAliasPaths, bundle, nativeSrcBundle,locale);
62               
63                if(updated){
64                        fileUtil.saveUtf8File(jsFileName, "(" + dojo.toJson(nativeSrcBundle, true) + ")");
65                }
66                //logStr += '\n';
67        }
68        cleanLocaleAlias(fileList);
69}
70
71//fileUtil.saveUtf8File(logDir + '/alias.log',logStr+'\n');
72//print('CLDR finished, please refer to logs at ' + logDir + ' for more details.');
73
74
75function _calculateAliasPath(bundle, name/*String*/){
76        for(p in bundle){
77                var index = p.indexOf(LOCALE_ALIAS_MARK);
78                if(index >= 0 /*p like 'xxx@localeAlias6'*/){
79                        var localeAliasSource/*String*/ = p.substring(0,index);
80                        if(records[localeAliasSource]/*calculated*/){
81                                //logStr += p + " has been calculated, ignored\n"
82                                continue;
83                        }
84                       
85                        var path = [];
86                        var aliasIndex = new Number(p.substring(index + LOCALE_ALIAS_MARK.length));
87                        //logStr += "aliasIndex for " + p + " is " + aliasIndex + "\n";
88                        var i = aliasIndex;
89                        while(bundle[localeAliasSource + LOCALE_ALIAS_MARK + (--i)]){}
90                       
91                        var src = localeAliasSource;
92                        while(bundle[localeAliasSource + LOCALE_ALIAS_MARK + (++i)]){
93                                var mapping = {};
94                                mapping[LOCALE_ALIAS_SOURCE_PROPERTY] = src;
95                                mapping[LOCALE_ALIAS_TARGET_PROPERTY] = bundle[localeAliasSource + LOCALE_ALIAS_MARK + i][LOCALE_ALIAS_TARGET_PROPERTY];
96                                mapping[LOCALE_ALIAS_TARGET_BUNDLE] = bundle[localeAliasSource + LOCALE_ALIAS_MARK + i][LOCALE_ALIAS_TARGET_BUNDLE];
97                                //whether aliased to the bundle itself
98                                mapping.inSelf = mapping[LOCALE_ALIAS_TARGET_BUNDLE] === name;
99                                path.push(mapping);
100                                records[src] = true;
101                                src = bundle[localeAliasSource + LOCALE_ALIAS_MARK + i][LOCALE_ALIAS_TARGET_PROPERTY];
102                        }
103                        path = path.reverse();
104                        //logStr += "alias path calucated for " + localeAliasSource + "=" + dojo.toJson(path) + "\n";
105                        localeAliasPaths.push(path);
106                }
107        }
108}
109
110function _processLocaleAlias(localeAliasPaths/*Array*/, bundle/*JSON Obj*/, nativeSrcBundle/*JSON Obj*/,locale/*String*/){
111        //Summary: Update all properties as defined by 'locale' alias mapping
112        //                 E.g.'months-format-abbr@localeAlias6':{'target':"months-format-wide", 'bundle':"gregorian"},
113        //                 means the array values of 'months-format-abbr' in current bundle should be
114        //                 merged with(inherit or overwrite) that of 'months-format-wide' in 'gregorian' bundle
115        //
116        //Note:    Currently no bundle recognition, always assume 'gregorian'.
117        var processed = {};
118        for(var i = 0; i < localeAliasPaths.length; i++){
119                var path = localeAliasPaths[i];
120                for(var j = 0; j < path.length; j++){
121                        var mapping = path[j];
122                        if(mapping.inSelf && mapping[LOCALE_ALIAS_SOURCE_PROPERTY] != mapping[LOCALE_ALIAS_TARGET_PROPERTY]
123                           && bundle[mapping[LOCALE_ALIAS_TARGET_PROPERTY]]/*target existed*/){
124                                //e.g. {'source':'months-format-abbr','target':"months-format-wide",'bundle':"gregorian"},
125                                //currently source and target bundles are the same - gregorian
126                                if(processed[mapping[LOCALE_ALIAS_SOURCE_PROPERTY]]){/*haven't been processed*/
127                                        //logStr += "!" + mapping[LOCALE_ALIAS_SOURCE_PROPERTY] +" has been processed for alias, escaped\n";
128                                        continue;
129                                }
130                                _updateLocaleAlias(bundle, mapping[LOCALE_ALIAS_SOURCE_PROPERTY], bundle,
131                                                                   mapping[LOCALE_ALIAS_TARGET_PROPERTY], nativeSrcBundle);
132                                processed[mapping[LOCALE_ALIAS_SOURCE_PROPERTY]] =  true;
133                        }else if(!mapping.inSelf){
134                                //For other non-gregorian calendars. e.g. "hebrew" etc.
135                                //Get the bundle according to the locale.
136                                var targetBundle = dojo.i18n.getLocalization('dojo.cldr', mapping[LOCALE_ALIAS_TARGET_BUNDLE], locale);
137                                if(processed[mapping[LOCALE_ALIAS_SOURCE_PROPERTY]]){//If being processed, continue;
138                                        continue;
139                                }
140                                _updateNoneGregAlias(bundle, mapping[LOCALE_ALIAS_SOURCE_PROPERTY], targetBundle,
141                                                                   mapping[LOCALE_ALIAS_TARGET_PROPERTY], nativeSrcBundle);
142                                processed[mapping[LOCALE_ALIAS_SOURCE_PROPERTY]] =  true;
143                        }
144                }
145        }
146}
147/*
148* This function will flatten the source bundle for non-gregorian ones by searching in the bundle files generated from the ldml spec which have terms like:
149* "'months-standAlone-abbr@localeAlias131':{'target':"months-format-abbr",'bundle':"hebrew"},".
150*   Parameters:
151*       sourceBundle: The bundles which need to be aliased.
152*       aliasSource:  The source mark string. 'months-standAlone-abbr' for example.
153*       targetBundle: The aliased bundle. 'hebrew' for example.
154*       aliasTarget:  The target mark string. 'months-format-abbr' for example.
155*       nativeSrcBundle: The final flattened bundle file.
156* According to the dojo way of fetching resource bundle, this function will firstly check the bundle under the appointed
157* locale. If the wanted calendar bundle is not under the locale, the root calendar bundle will be fetched. If the non-gregorian
158* bundle in the root can not be found, dojo will finally get the root gregorian bundle.
159*/
160function _updateNoneGregAlias(sourceBundle/*JSON Obj*/, aliasSource/*String*/, targetBundle/*JSON Obj*/, aliasTarget/*String*/, nativeSrcBundle/*JSON Obj*/){
161    for (var sKey in sourceBundle) {
162        var target = targetBundle[sKey],
163            source = sourceBundle[sKey],
164            nativeSrc = nativeSrcBundle[sKey];
165
166        if (sKey.indexOf(aliasSource) == 0 && !nativeSrc && target && !compare(source, target)) {
167            nativeSrcBundle[sKey] = target;
168            sourceBundle[sKey] = target;
169            updated = true;
170        } else {
171            if (sKey.indexOf(aliasSource) == 0 && nativeSrc && dojo.isArray(source) && dojo.isArray(target)) {
172                for (var i = 0; i < source.length; i++) {
173                    if (source[i] === undefined) {
174                        source[i] = target[i];
175                        updated = true;
176                    }
177                }
178                if (source.length < target.length) {
179                    source = sourceBundle[sKey] = source.concat(target.slice(source.length));
180                    updated = true;
181                }
182                if (updated) {
183                    nativeSrcBundle[sKey] = source;
184                }
185            }
186        }
187    }
188}
189function _updateLocaleAlias(sourceBundle/*JSON Obj*/,aliasSource/*String*/, targetBundle/*JSON Obj*/,
190                                                        aliasTarget/*String*/, nativeSrcBundle/*JSON Obj*/){
191                //single property
192                if(!nativeSrcBundle[aliasSource] && nativeSrcBundle[aliasTarget]//no this property in current locale
193                   && !compare(sourceBundle[aliasSource], targetBundle[aliasTarget])){
194                        // then should inherit from alias target (as defined by 'locale' alias)
195                        //logStr += '1 '+aliasSource + "=" + sourceBundle[aliasSource] + " is replaced with " + aliasTarget + "=" + targetBundle[aliasTarget]+'\n';
196                        //sourceBundle[aliasSource] =  targetBundle[aliasTarget];
197                        nativeSrcBundle[aliasSource] =  targetBundle[aliasTarget];
198                        sourceBundle[aliasSource] = nativeSrcBundle[aliasSource];
199                        updated = true;
200                }else if(nativeSrcBundle[aliasSource] && dojo.isArray(sourceBundle[aliasSource])
201                         && dojo.isArray(targetBundle[aliasTarget])){
202                        if(sourceBundle[aliasSource].length > targetBundle[aliasTarget].length){
203                                //logStr +="Error:" + aliasSource + ".length > " +  aliasTarget + ".length \n";
204                        }
205                        //array property, see if need inherit
206                        for(var i = 0; i < sourceBundle[aliasSource].length; i++){
207                                if(sourceBundle[aliasSource][i] == undefined){//need inherit
208                                        //logStr += '2 ' + aliasSource + "["+i+"]=" +sourceBundle[aliasSource][i]+" is replaced with " + aliasTarget+"["+i+"]="+targetBundle[aliasTarget][i]+'\n';
209                                        sourceBundle[aliasSource][i] =  targetBundle[aliasTarget][i];
210                                        updated = true;
211                                }// otherwise no change and use current value
212                        }
213                        if(sourceBundle[aliasSource].length < targetBundle[aliasTarget].length){
214                                //logStr +='3 ' + aliasSource +' from ' + sourceBundle[aliasSource].length +' to '
215                                //                + (targetBundle[aliasTarget].length-1) + ' are copied from '
216                                //                +aliasTarget + '='+ targetBundle[aliasTarget] +'\n';
217                                sourceBundle[aliasSource] = sourceBundle[aliasSource].concat(
218                                                                                        targetBundle[aliasTarget].slice(sourceBundle[aliasSource].length));
219                                updated = true;
220                        }
221                        if(updated){
222                                nativeSrcBundle[aliasSource] = sourceBundle[aliasSource];
223                        }
224                }
225}
226
227function cleanLocaleAlias(fileList/*Array*/){
228        for(var i = 0; i < fileList.length; i++){
229                var fileName = new String(fileList[i]); //Java String
230                try{
231                        var bundle = getNativeBundle(fileName);//bundle not flattened
232                }catch(e){print(e);/* simply ignore if no bundle found*/}
233               
234                var newBundle = {};
235                var needUpdate = false;
236                for(p in bundle){
237                        if(p.indexOf(LOCALE_ALIAS_MARK) < 0){
238                                newBundle[p] = bundle[p];
239                        }else{
240                                needUpdate = true;
241                        }
242                }
243                if(needUpdate){
244                        fileUtil.saveUtf8File(fileName, "(" + dojo.toJson(newBundle, true) + ")");
245                        //logStr += "cleaned @localAlias for " + fileName + "\n";
246                }
247        }
248}
Note: See TracBrowser for help on using the repository browser.