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

Last change on this file since 275 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: 6.7 KB
Line 
1/**
2 * In CLDR, there are some special locales with abnormal hierarchy.
3 *
4 * E.g.zh-hk.xml is aliased to zh-hant-hk.xml for all the calendar/number/currency data.
5 * So after CLDR transformation, JSON bundles under zh-hk is totally the same as those under zh-hant-hk.
6 * Problems will occur when dojo loads zh-hk bundle, as dojo will flatten it with the following sequence:
7 * Root -> zh -> zh-hk, but the right sequence should be Root -> zh -> zh-hant -> zh-hk(zh-hant-hk)
8 * so the bundles under zh-hant locale is missing.
9 *
10 * This script is used to process all the special locales so that after CLDR transformation,
11 * zh-hk bundle will be flatted both with zh-hant and zh-hant-hk, nothing will be lost then.
12 * Please see the following SPECIAL_LOCALES_MAP for detail mapping info.
13 *
14 * Note: Here for simplification, we name zh-hk as source locale,and name zh-hant-hk as alias locale.
15 */
16djConfig={baseUrl: "../../../dojo/"};
17
18load("../../../dojo/dojo.js");
19load("../jslib/logger.js");
20load("../jslib/fileUtil.js");
21load("cldrUtil.js");
22
23dojo.require("dojo.i18n");
24
25var dir/*String*/ = arguments[0];// ${dojo}/dojo/cldr/nls
26
27//locales that are generated by ${dojo}/util/buildscripts/cldr/build.xml
28var allLocalesStr/*String*/ = arguments[1];//e.g. "ar,ar-eg,en-au,en-ca,en-gb,en-us,de-de,es-es,fr-fr,..."
29
30var logDir = arguments[2];
31
32//related bundles, currently only 'number','currency','gregorian' bundles
33//TBD - 'buddhist','chinese','coptic','currency','ethiopic','gregorian','hebrew','islamic','islamic-civil','japanese','persian'
34var BUNDLE_MAP = ['number','currency','gregorian'];
35
36//headers for generated bundle files
37var NLS_JSON_HEAD = ['// generated from ldml/main/*.xml, xpath: ldml/numbers\n',
38                                         '// generated from ldml/main/*.xml, xpath: ldml/numbers/currencies\n',
39                                     '// generated from ldml/main/*.xml, xpath: ldml/calendars/calendar-gregorian\n'];
40
41var SPECIAL_LOCALES_MAP = {
42        //Mapping info for some special locales with abnormal hierarchy.
43        //Currently for CLDR 1.6, will be updated with latest CLDR release.
44
45        'zh-hk':'zh-hant-hk',
46        'zh-mo':'zh-hant-mo',
47        'sh':'sr-latn',
48        'mo':'ro-md',
49        'pa-pk':'pa-arab-pk',//pa-pk and pa-arab-pk don't exist, but pa-arab exists
50        'zh-tw':'zh-hant-tw',//zh-tw and zh-hant-tw don't exist, but zh-hant exists
51        'uz-af':'uz-arab-af',//uz-af and uz-arab-af don't exist, but uz-arab exists
52        'ha-sd':'ha-arab-sd',//ha-sd and ha-arab-sd don't exist, but ha-arab exists
53        'ku-tr':'ku-latn-tr' //ku-tr and ku-latn-tr don't exist, but ku-latn exists
54       
55        /* The following locales don't have any bundles currently (CLDR 1.6),
56         * listed here for CLDR future release.
57         *
58        'az-az':'az-latn-az',
59        'ha-gh':'ha-latn-gh',
60        'ha-ne':'ha-latn-ne',
61        'ha-ng':'ha-latn-ng',
62        'kk-kz':'kk-cyrl-kz',
63        'ku-iq':'ku-latn-iq',
64        'ku-ir':'ku-latn-ir',
65        'ku-sy':'ku-latn-sy',
66        'pa-in':'pa-guru-in',
67        'sr-cs':'sr-cyrl-cs',
68        'sr-me':'sr-cyrl-me',
69        'sr-rs':'sr-cyrl-rs',
70        'sr-yu':'sr-cyrl-rs',
71        'uz-uz':'uz-cyrl-uz',
72        'zh-sg':'zh-hans-sg',
73        'zh-cn':'zh-hans-cn',
74        'mn-cn':'mn-mong-cn',
75        'mn-mn':'mn-cyrl-cn',
76        'pa-in':'pa-guru-in',
77       
78        */
79       
80        /* Don't need to process the following locale alias
81         * only listed here for futher comparison
82         *
83        //sh is already aliased to sr-latn
84        'sh-cs':'sr-latn-rs',
85        'sh-yu':'sr-latn-rs',
86        'sh-ba':'sr-latn-ba',//sh-ba and sr-latn-ba don't exist, but sr-latn exists
87       
88        //has the same parent
89        'sr-cyrl-cs':'sr-cyrl-rs',
90        'sr-cyrl-yu':'sr-cyrl-rs',
91       
92        'sr-cs':'sr-cyrl-cs',
93        'sr-me':'sr-cyrl-me',
94        'sr-rs':'sr-cyrl-rs',
95        'sr-yu':'sr-cyrl-rs',
96        'sr-ba':'sr-cyrl-ba',//sr-cyrl is null
97        'tg-tj':'tg-cyrl-tj',//tg-cyrl is null
98        'ug-cn':'ug-arab-cn',//ug-arab is null
99        'uz-uz':'uz-cyrl-uz',//uz-cyrl is null
100        'zh-cn':'zh-hans-cn',//zh-hans is null
101        'zh-sg':'zh-hans-sg',//zh-hans is null
102        */
103};
104
105print('specialLocale.js...');
106
107var srcLocaleList = [];//source locale file paths
108for(x in SPECIAL_LOCALES_MAP){
109        if(allLocalesStr == '${locales}' //no $locales parameter,all locales required
110           || (allLocalesStr && 0 <= allLocalesStr.indexOf(x))){
111                //only if this locale is required
112                srcLocaleList.push(dir + '/' + x);
113        }
114}
115
116/*
117 * Get and compare the flattened bundles(using dojo.i18n) of each source locale and its alias
118 * Copy those bundles that alias has but source locale doesn't to source locale,
119 * and also update new items in source locale bundle
120 */
121var logStr = "";
122for(var i= 0; i < srcLocaleList.length; i++){
123        var srcLocalePath = srcLocaleList[i];//source locale path
124        var srcPathSegments = srcLocalePath.split("/");
125        var srcLocale = srcPathSegments[srcPathSegments.length - 1];
126        var aliasLocale = SPECIAL_LOCALES_MAP[srcLocale];
127       
128        //iterate each bundle
129        for(var len = 0; len < BUNDLE_MAP.length; len++){
130                try{
131                        //declare bundles
132//                      dojo.i18n._requireLocalization('dojo.cldr', BUNDLE_MAP[len], srcLocale);
133//                      dojo.i18n._requireLocalization('dojo.cldr', BUNDLE_MAP[len], aliasLocale);
134                                               
135                        //get bundles
136                        var srcBundle = dojo.i18n.getLocalization('dojo.cldr', BUNDLE_MAP[len], srcLocale);
137                        var aliasBundle = dojo.i18n.getLocalization('dojo.cldr', BUNDLE_MAP[len], aliasLocale);
138                }catch(e){/* logStr+="specialLocale: an exception occurred: "+e; /* it's ok if no bundle found*/}
139               
140                if(!aliasBundle && !srcBundle){
141                        break;
142                }else if(!aliasBundle && srcBundle){
143                        //should be an error case
144                        //logStr += 'specialLocale.js error: source locale has more bundles than alias locale\n';
145                        break;
146                }else if(aliasBundle && !srcBundle){
147                        //add the new bundle to source locale
148                        validateDir(srcLocalePath);
149                        fileUtil.saveUtf8File(srcLocalePath + '/' + BUNDLE_MAP[len] + '.js', NLS_JSON_HEAD[len] + '(' + dojo.toJson(aliasBundle, true) + ')');
150                        //logStr += "specialLocale.js : copied " + BUNDLE_MAP[len] + '.js to ' + srcLocalePath + '\n';
151                }else if(aliasBundle && srcBundle){
152                        var isUpdated = false;
153                        //get native bundle whose content is not flattened
154                        try{
155                                var nativeSrcBundle = getNativeBundle(srcLocalePath + '/' + BUNDLE_MAP[len] + '.js');
156                        }catch(e){
157                                //if no nativeSrcBundle
158                                nativeSrcBundle = {};
159                        }
160
161                        for(p in aliasBundle){
162                                if(!isLocaleAliasSrc(p, aliasBundle) // p is not the source of a 'locale' alias mapping
163                                   && (!srcBundle[p] || !compare(srcBundle[p], aliasBundle[p]))){
164                                   //inherit
165                                   nativeSrcBundle[p] = aliasBundle[p];
166                                   //logStr += "copied " + p + "=" + aliasBundle[p] + "\n";
167                                   isUpdated = true;
168                                }
169                        }
170                       
171                        if(isUpdated){
172                                validateDir(srcLocalePath);
173                                fileUtil.saveUtf8File(srcLocalePath + '/' + BUNDLE_MAP[len] + '.js', NLS_JSON_HEAD[len] + '(' + dojo.toJson(nativeSrcBundle, true) + ')');
174                                //logStr += 'specialLocale.js : updated ' + BUNDLE_MAP[len] + '.js in ' + srcLocalePath + '\n';
175                        }
176                }
177        }
178}
179
180//fileUtil.saveUtf8File(logDir + '/specialLocale.log',logStr+'\n');
181
182function validateDir(/*String*/dirPath){
183        //summary:make sure the dir exists
184        var dir = new java.io.File(dirPath);
185        if(!dir.exists()){
186                dir.mkdir();
187        }
188}
Note: See TracBrowser for help on using the repository browser.