[483] | 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 | //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 | |
---|
| 110 | function _processLocaleAlias(localeAliasPaths/*Array*/, bundle/*JSON Obj*/, nativeSrcBundle/*JSON Obj*/,locale/*String*/){ |
---|
| 111 | // summary: |
---|
| 112 | // Update all properties as defined by 'locale' alias mapping |
---|
| 113 | // E.g.'months-format-abbr@localeAlias6':{'target':"months-format-wide", 'bundle':"gregorian"}, |
---|
| 114 | // means the array values of 'months-format-abbr' in current bundle should be |
---|
| 115 | // merged with(inherit or overwrite) that of 'months-format-wide' in 'gregorian' bundle |
---|
| 116 | // |
---|
| 117 | // Note: Currently no bundle recognition, always assume 'gregorian'. |
---|
| 118 | |
---|
| 119 | var processed = {}; |
---|
| 120 | for(var i = 0; i < localeAliasPaths.length; i++){ |
---|
| 121 | var path = localeAliasPaths[i]; |
---|
| 122 | for(var j = 0; j < path.length; j++){ |
---|
| 123 | var mapping = path[j]; |
---|
| 124 | if(mapping.inSelf && mapping[LOCALE_ALIAS_SOURCE_PROPERTY] != mapping[LOCALE_ALIAS_TARGET_PROPERTY] |
---|
| 125 | && bundle[mapping[LOCALE_ALIAS_TARGET_PROPERTY]]/*target existed*/){ |
---|
| 126 | //e.g. {'source':'months-format-abbr','target':"months-format-wide",'bundle':"gregorian"}, |
---|
| 127 | //currently source and target bundles are the same - gregorian |
---|
| 128 | if(processed[mapping[LOCALE_ALIAS_SOURCE_PROPERTY]]){/*haven't been processed*/ |
---|
| 129 | //logStr += "!" + mapping[LOCALE_ALIAS_SOURCE_PROPERTY] +" has been processed for alias, escaped\n"; |
---|
| 130 | continue; |
---|
| 131 | } |
---|
| 132 | _updateLocaleAlias(bundle, mapping[LOCALE_ALIAS_SOURCE_PROPERTY], bundle, |
---|
| 133 | mapping[LOCALE_ALIAS_TARGET_PROPERTY], nativeSrcBundle); |
---|
| 134 | processed[mapping[LOCALE_ALIAS_SOURCE_PROPERTY]] = true; |
---|
| 135 | }else if(!mapping.inSelf){ |
---|
| 136 | //For other non-gregorian calendars. e.g. "hebrew" etc. |
---|
| 137 | //Get the bundle according to the locale. |
---|
| 138 | var targetBundle = dojo.i18n.getLocalization('dojo.cldr', mapping[LOCALE_ALIAS_TARGET_BUNDLE], locale); |
---|
| 139 | if(processed[mapping[LOCALE_ALIAS_SOURCE_PROPERTY]]){//If being processed, continue; |
---|
| 140 | continue; |
---|
| 141 | } |
---|
| 142 | _updateNoneGregAlias(bundle, mapping[LOCALE_ALIAS_SOURCE_PROPERTY], targetBundle, |
---|
| 143 | mapping[LOCALE_ALIAS_TARGET_PROPERTY], nativeSrcBundle); |
---|
| 144 | processed[mapping[LOCALE_ALIAS_SOURCE_PROPERTY]] = true; |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | /* |
---|
| 150 | * 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: |
---|
| 151 | * "'months-standAlone-abbr@localeAlias131':{'target':"months-format-abbr",'bundle':"hebrew"},". |
---|
| 152 | * Parameters: |
---|
| 153 | * sourceBundle: The bundles which need to be aliased. |
---|
| 154 | * aliasSource: The source mark string. 'months-standAlone-abbr' for example. |
---|
| 155 | * targetBundle: The aliased bundle. 'hebrew' for example. |
---|
| 156 | * aliasTarget: The target mark string. 'months-format-abbr' for example. |
---|
| 157 | * nativeSrcBundle: The final flattened bundle file. |
---|
| 158 | * According to the dojo way of fetching resource bundle, this function will firstly check the bundle under the appointed |
---|
| 159 | * locale. If the wanted calendar bundle is not under the locale, the root calendar bundle will be fetched. If the non-gregorian |
---|
| 160 | * bundle in the root can not be found, dojo will finally get the root gregorian bundle. |
---|
| 161 | */ |
---|
| 162 | function _updateNoneGregAlias(sourceBundle/*JSON Obj*/, aliasSource/*String*/, targetBundle/*JSON Obj*/, aliasTarget/*String*/, nativeSrcBundle/*JSON Obj*/){ |
---|
| 163 | for (var sKey in sourceBundle) { |
---|
| 164 | var target = targetBundle[sKey], |
---|
| 165 | source = sourceBundle[sKey], |
---|
| 166 | nativeSrc = nativeSrcBundle[sKey]; |
---|
| 167 | |
---|
| 168 | if (sKey.indexOf(aliasSource) == 0 && !nativeSrc && target && !compare(source, target)) { |
---|
| 169 | nativeSrcBundle[sKey] = target; |
---|
| 170 | sourceBundle[sKey] = target; |
---|
| 171 | updated = true; |
---|
| 172 | } else { |
---|
| 173 | if (sKey.indexOf(aliasSource) == 0 && nativeSrc && dojo.isArray(source) && dojo.isArray(target)) { |
---|
| 174 | for (var i = 0; i < source.length; i++) { |
---|
| 175 | if (source[i] === undefined) { |
---|
| 176 | source[i] = target[i]; |
---|
| 177 | updated = true; |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | if (source.length < target.length) { |
---|
| 181 | source = sourceBundle[sKey] = source.concat(target.slice(source.length)); |
---|
| 182 | updated = true; |
---|
| 183 | } |
---|
| 184 | if (updated) { |
---|
| 185 | nativeSrcBundle[sKey] = source; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | function _updateLocaleAlias(sourceBundle/*JSON Obj*/,aliasSource/*String*/, targetBundle/*JSON Obj*/, |
---|
| 192 | aliasTarget/*String*/, nativeSrcBundle/*JSON Obj*/){ |
---|
| 193 | //single property |
---|
| 194 | if(!nativeSrcBundle[aliasSource] && nativeSrcBundle[aliasTarget]//no this property in current locale |
---|
| 195 | && !compare(sourceBundle[aliasSource], targetBundle[aliasTarget])){ |
---|
| 196 | // then should inherit from alias target (as defined by 'locale' alias) |
---|
| 197 | //logStr += '1 '+aliasSource + "=" + sourceBundle[aliasSource] + " is replaced with " + aliasTarget + "=" + targetBundle[aliasTarget]+'\n'; |
---|
| 198 | //sourceBundle[aliasSource] = targetBundle[aliasTarget]; |
---|
| 199 | nativeSrcBundle[aliasSource] = targetBundle[aliasTarget]; |
---|
| 200 | sourceBundle[aliasSource] = nativeSrcBundle[aliasSource]; |
---|
| 201 | updated = true; |
---|
| 202 | }else if(nativeSrcBundle[aliasSource] && dojo.isArray(sourceBundle[aliasSource]) |
---|
| 203 | && dojo.isArray(targetBundle[aliasTarget])){ |
---|
| 204 | if(sourceBundle[aliasSource].length > targetBundle[aliasTarget].length){ |
---|
| 205 | //logStr +="Error:" + aliasSource + ".length > " + aliasTarget + ".length \n"; |
---|
| 206 | } |
---|
| 207 | //array property, see if need inherit |
---|
| 208 | for(var i = 0; i < sourceBundle[aliasSource].length; i++){ |
---|
| 209 | if(sourceBundle[aliasSource][i] == undefined){//need inherit |
---|
| 210 | //logStr += '2 ' + aliasSource + "["+i+"]=" +sourceBundle[aliasSource][i]+" is replaced with " + aliasTarget+"["+i+"]="+targetBundle[aliasTarget][i]+'\n'; |
---|
| 211 | sourceBundle[aliasSource][i] = targetBundle[aliasTarget][i]; |
---|
| 212 | updated = true; |
---|
| 213 | }// otherwise no change and use current value |
---|
| 214 | } |
---|
| 215 | if(sourceBundle[aliasSource].length < targetBundle[aliasTarget].length){ |
---|
| 216 | //logStr +='3 ' + aliasSource +' from ' + sourceBundle[aliasSource].length +' to ' |
---|
| 217 | // + (targetBundle[aliasTarget].length-1) + ' are copied from ' |
---|
| 218 | // +aliasTarget + '='+ targetBundle[aliasTarget] +'\n'; |
---|
| 219 | sourceBundle[aliasSource] = sourceBundle[aliasSource].concat( |
---|
| 220 | targetBundle[aliasTarget].slice(sourceBundle[aliasSource].length)); |
---|
| 221 | updated = true; |
---|
| 222 | } |
---|
| 223 | if(updated){ |
---|
| 224 | nativeSrcBundle[aliasSource] = sourceBundle[aliasSource]; |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | function cleanLocaleAlias(fileList/*Array*/){ |
---|
| 230 | for(var i = 0; i < fileList.length; i++){ |
---|
| 231 | var fileName = new String(fileList[i]); //Java String |
---|
| 232 | try{ |
---|
| 233 | var bundle = getNativeBundle(fileName);//bundle not flattened |
---|
| 234 | }catch(e){print(e);/* simply ignore if no bundle found*/} |
---|
| 235 | |
---|
| 236 | var newBundle = {}; |
---|
| 237 | var needUpdate = false; |
---|
| 238 | for(p in bundle){ |
---|
| 239 | if(p.indexOf(LOCALE_ALIAS_MARK) < 0){ |
---|
| 240 | newBundle[p] = bundle[p]; |
---|
| 241 | }else{ |
---|
| 242 | needUpdate = true; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | if(needUpdate){ |
---|
| 246 | fileUtil.saveUtf8File(fileName, "(" + dojo.toJson(newBundle, true) + ")"); |
---|
| 247 | //logStr += "cleaned @localAlias for " + fileName + "\n"; |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | } |
---|