Debian Codegraph @

util/buildscripts/cldr/alias.js

raw symbols SCIP
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
12 djConfig={baseUrl: "../../../dojo/", paths: {"dojo/_base/xhr": "../util/buildscripts/cldr/xhr"}};
13
14 load("../../../dojo/dojo.js");
15 load("../jslib/logger.js");
16 load("../jslib/fileUtil.js");
17 load("cldrUtil.js");
18
19 dojo.require("dojo.i18n");
20
21 var dir/*String*/ = arguments[0];// ${dojo}/dojo/cldr/nls
22 var logDir = arguments[1];
23 var logStr = "";
24
25 //Add new bundles to the list so that they will be aliased according to the ldml spec.
26 var BUNDLES = ['gregorian','hebrew','islamic','islamic-civil','buddhist','persian'];
27
28 var LOCALE_ALIAS_MARK = '@localeAlias';
29 var LOCALE_ALIAS_SOURCE_PROPERTY = 'source';
30 var LOCALE_ALIAS_TARGET_PROPERTY = 'target';
31 var LOCALE_ALIAS_TARGET_BUNDLE = 'bundle';
32 var localeAliasPaths = [];/**/
33 var records = {};/*{property : boolean}, record whether a property has been calculated for alias path*/
34 var updated = false;
35
36 print('alias.js...');
37
38 for(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
75 function _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 if (!mapping[LOCALE_ALIAS_TARGET_BUNDLE]) {
98 // TODO: Fix calendar.xsl to handle the new aliases in root.xml
99 continue;
100 }
101 //whether aliased to the bundle itself
102 mapping.inSelf = mapping[LOCALE_ALIAS_TARGET_BUNDLE] === name;
103 path.push(mapping);
104 records[src] = true;
105 src = bundle[localeAliasSource + LOCALE_ALIAS_MARK + i][LOCALE_ALIAS_TARGET_PROPERTY];
106 }
107 path = path.reverse();
108 //logStr += "alias path calucated for " + localeAliasSource + "=" + dojo.toJson(path) + "\n";
109 localeAliasPaths.push(path);
110 }
111 }
112 }
113
114 function _processLocaleAlias(localeAliasPaths/*Array*/, bundle/*JSON Obj*/, nativeSrcBundle/*JSON Obj*/,locale/*String*/){
115 // summary:
116 // Update all properties as defined by 'locale' alias mapping
117 // E.g.'months-format-abbr@localeAlias6':{'target':"months-format-wide", 'bundle':"gregorian"},
118 // means the array values of 'months-format-abbr' in current bundle should be
119 // merged with(inherit or overwrite) that of 'months-format-wide' in 'gregorian' bundle
120 //
121 // Note: Currently no bundle recognition, always assume 'gregorian'.
122
123 var processed = {};
124 for(var i = 0; i < localeAliasPaths.length; i++){
125 var path = localeAliasPaths[i];
126 for(var j = 0; j < path.length; j++){
127 var mapping = path[j];
128 if(mapping.inSelf && mapping[LOCALE_ALIAS_SOURCE_PROPERTY] != mapping[LOCALE_ALIAS_TARGET_PROPERTY]
129 && bundle[mapping[LOCALE_ALIAS_TARGET_PROPERTY]]/*target existed*/){
130 //e.g. {'source':'months-format-abbr','target':"months-format-wide",'bundle':"gregorian"},
131 //currently source and target bundles are the same - gregorian
132 if(processed[mapping[LOCALE_ALIAS_SOURCE_PROPERTY]]){/*haven't been processed*/
133 //logStr += "!" + mapping[LOCALE_ALIAS_SOURCE_PROPERTY] +" has been processed for alias, escaped\n";
134 continue;
135 }
136 _updateLocaleAlias(bundle, mapping[LOCALE_ALIAS_SOURCE_PROPERTY], bundle,
137 mapping[LOCALE_ALIAS_TARGET_PROPERTY], nativeSrcBundle);
138 processed[mapping[LOCALE_ALIAS_SOURCE_PROPERTY]] = true;
139 }else if(!mapping.inSelf){
140 //For other non-gregorian calendars. e.g. "hebrew" etc.
141 //Get the bundle according to the locale.
142 var targetBundle = dojo.i18n.getLocalization('dojo.cldr', mapping[LOCALE_ALIAS_TARGET_BUNDLE], locale);
143 if(processed[mapping[LOCALE_ALIAS_SOURCE_PROPERTY]]){//If being processed, continue;
144 continue;
145 }
146 _updateNoneGregAlias(bundle, mapping[LOCALE_ALIAS_SOURCE_PROPERTY], targetBundle,
147 mapping[LOCALE_ALIAS_TARGET_PROPERTY], nativeSrcBundle);
148 processed[mapping[LOCALE_ALIAS_SOURCE_PROPERTY]] = true;
149 }
150 }
151 }
152 }
153 /*
154 * 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:
155 * "'months-standAlone-abbr@localeAlias131':{'target':"months-format-abbr",'bundle':"hebrew"},".
156 * Parameters:
157 * sourceBundle: The bundles which need to be aliased.
158 * aliasSource: The source mark string. 'months-standAlone-abbr' for example.
159 * targetBundle: The aliased bundle. 'hebrew' for example.
160 * aliasTarget: The target mark string. 'months-format-abbr' for example.
161 * nativeSrcBundle: The final flattened bundle file.
162 * According to the dojo way of fetching resource bundle, this function will firstly check the bundle under the appointed
163 * locale. If the wanted calendar bundle is not under the locale, the root calendar bundle will be fetched. If the non-gregorian
164 * bundle in the root can not be found, dojo will finally get the root gregorian bundle.
165 */
166 function _updateNoneGregAlias(sourceBundle/*JSON Obj*/, aliasSource/*String*/, targetBundle/*JSON Obj*/, aliasTarget/*String*/, nativeSrcBundle/*JSON Obj*/){
167 for (var sKey in sourceBundle) {
168 var target = targetBundle[sKey],
169 source = sourceBundle[sKey],
170 nativeSrc = nativeSrcBundle[sKey];
171
172 if (sKey.indexOf(aliasSource) == 0 && !nativeSrc && target && !compare(source, target)) {
173 nativeSrcBundle[sKey] = target;
174 sourceBundle[sKey] = target;
175 updated = true;
176 } else {
177 if (sKey.indexOf(aliasSource) == 0 && nativeSrc && dojo.isArray(source) && dojo.isArray(target)) {
178 for (var i = 0; i < source.length; i++) {
179 if (source[i] === undefined) {
180 source[i] = target[i];
181 updated = true;
182 }
183 }
184 if (source.length < target.length) {
185 source = sourceBundle[sKey] = source.concat(target.slice(source.length));
186 updated = true;
187 }
188 if (updated) {
189 nativeSrcBundle[sKey] = source;
190 }
191 }
192 }
193 }
194 }
195 function _updateLocaleAlias(sourceBundle/*JSON Obj*/,aliasSource/*String*/, targetBundle/*JSON Obj*/,
196 aliasTarget/*String*/, nativeSrcBundle/*JSON Obj*/){
197 //single property
198 if(!nativeSrcBundle[aliasSource] && nativeSrcBundle[aliasTarget]//no this property in current locale
199 && !compare(sourceBundle[aliasSource], targetBundle[aliasTarget])){
200 // then should inherit from alias target (as defined by 'locale' alias)
201 //logStr += '1 '+aliasSource + "=" + sourceBundle[aliasSource] + " is replaced with " + aliasTarget + "=" + targetBundle[aliasTarget]+'\n';
202 //sourceBundle[aliasSource] = targetBundle[aliasTarget];
203 nativeSrcBundle[aliasSource] = targetBundle[aliasTarget];
204 sourceBundle[aliasSource] = nativeSrcBundle[aliasSource];
205 updated = true;
206 }else if(nativeSrcBundle[aliasSource] && dojo.isArray(sourceBundle[aliasSource])
207 && dojo.isArray(targetBundle[aliasTarget])){
208 if(sourceBundle[aliasSource].length > targetBundle[aliasTarget].length){
209 //logStr +="Error:" + aliasSource + ".length > " + aliasTarget + ".length \n";
210 }
211 //array property, see if need inherit
212 for(var i = 0; i < sourceBundle[aliasSource].length; i++){
213 if(sourceBundle[aliasSource][i] == undefined){//need inherit
214 //logStr += '2 ' + aliasSource + "["+i+"]=" +sourceBundle[aliasSource][i]+" is replaced with " + aliasTarget+"["+i+"]="+targetBundle[aliasTarget][i]+'\n';
215 sourceBundle[aliasSource][i] = targetBundle[aliasTarget][i];
216 updated = true;
217 }// otherwise no change and use current value
218 }
219 if(sourceBundle[aliasSource].length < targetBundle[aliasTarget].length){
220 //logStr +='3 ' + aliasSource +' from ' + sourceBundle[aliasSource].length +' to '
221 // + (targetBundle[aliasTarget].length-1) + ' are copied from '
222 // +aliasTarget + '='+ targetBundle[aliasTarget] +'\n';
223 sourceBundle[aliasSource] = sourceBundle[aliasSource].concat(
224 targetBundle[aliasTarget].slice(sourceBundle[aliasSource].length));
225 updated = true;
226 }
227 if(updated){
228 nativeSrcBundle[aliasSource] = sourceBundle[aliasSource];
229 }
230 }
231 }
232
233 function cleanLocaleAlias(fileList/*Array*/){
234 for(var i = 0; i < fileList.length; i++){
235 var fileName = new String(fileList[i]); //Java String
236 try{
237 var bundle = getNativeBundle(fileName);//bundle not flattened
238 }catch(e){print(e);/* simply ignore if no bundle found*/}
239
240 var newBundle = {};
241 var needUpdate = false;
242 for(p in bundle){
243 if(p.indexOf(LOCALE_ALIAS_MARK) < 0){
244 newBundle[p] = bundle[p];
245 }else{
246 needUpdate = true;
247 }
248 }
249 if(needUpdate){
250 fileUtil.saveUtf8File(fileName, "(" + dojo.toJson(newBundle, true) + ")");
251 //logStr += "cleaned @localAlias for " + fileName + "\n";
252 }
253 }
254 }