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