| 1 | /** |
| 2 | * The CLDR represents some lists, like month names, as separate entries, and our JSON uses arrays to express them. |
| 3 | * For some variants, the best our XSLT can do is translate this to a sparse array with 'undefined' entries. |
| 4 | * These entries need to be picked up from the parent locale(s) and copied into the array as necessary(only when the item |
| 5 | * is not the source of a locale alias mapping, like 'months-format-abbr' should be ignored if it is the source of |
| 6 | * locale alias mapping like 'months-format-abbr@localeAlias' :{'target':"months-format-wide",'bundle':"gregorian"}). |
| 7 | * So, this script is responsible for taking all the generated JSON files, and for values which are of type |
| 8 | * array and not the source of locale alias mapping, mixing in the parent values with the undefined ones, recursing |
| 9 | * all the way to the 'root' locale,and replacing the contents of the file. |
| 10 | * |
| 11 | * this script traverses all locales dir in the given root dir |
| 12 | * |
| 13 | * E.g.(Just for example, the contents are not applicable) |
| 14 | * parent locale - "en": |
| 15 | * // generated from ldml/main/ *.xml, xpath: ldml/calendars/calendar-ethiopic |
| 16 | * ({ |
| 17 | * 'months-format-abbr':["1","2","3","4","5","6","7","8","9","10","11","12"], |
| 18 | * 'dateFormat-long': "yyyy MMMM d", |
| 19 | * 'dateTimeFormat': "{1} {0}" |
| 20 | * }) |
| 21 | * |
| 22 | * child locale - "en-us": |
| 23 | * // generated from ldml/main/ *.xml, xpath: ldml/calendars/calendar-ethiopic |
| 24 | * ({ |
| 25 | * 'months-format-abbr':[undefined,undefined,"March",undefined,undefined,"June"], |
| 26 | * 'dateFormat-long': "yyyy-MMMM-d" |
| 27 | * }) |
| 28 | * |
| 29 | * After process, the result will be: |
| 30 | * child locale - "en-us": |
| 31 | * // generated from ldml/main/ *.xml, xpath: ldml/calendars/calendar-ethiopic |
| 32 | * ({ |
| 33 | * 'months-format-abbr':["1","2","March","4","5","June","7","8","9","10","11","12"], |
| 34 | * 'dateFormat-long': "yyyy-MMMM-d" |
| 35 | * }) |
| 36 | */ |
| 37 | |
| 38 | djConfig={baseUrl: "../../../dojo/", paths: {"dojo/_base/xhr": "../util/buildscripts/cldr/xhr"}}; |
| 39 | |
| 40 | load("../../../dojo/dojo.js"); |
| 41 | load("../jslib/logger.js"); |
| 42 | load("../jslib/fileUtil.js"); |
| 43 | load("cldrUtil.js"); |
| 44 | |
| 45 | dojo.require("dojo.i18n"); |
| 46 | |
| 47 | var _searchLocalePath = function(/*String*/locale, /*Boolean*/down, /*Function*/searchFunc){ |
| 48 | // summary: |
| 49 | // A helper method to assist in searching for locale-based resources. |
| 50 | // Will iterate through the variants of a particular locale, either up |
| 51 | // or down, executing a callback function. For example, "en-us" and |
| 52 | // true will try "en-us" followed by "en" and finally "ROOT". |
| 53 | |
| 54 | locale = dojo.i18n.normalizeLocale(locale); |
| 55 | |
| 56 | var elements = locale.split('-'); |
| 57 | var searchlist = []; |
| 58 | for(var i = elements.length; i > 0; i--){ |
| 59 | searchlist.push(elements.slice(0, i).join('-')); |
| 60 | } |
| 61 | searchlist.push(false); |
| 62 | if(down){searchlist.reverse();} |
| 63 | |
| 64 | for(var j = searchlist.length - 1; j >= 0; j--){ |
| 65 | var loc = searchlist[j] || "ROOT"; |
| 66 | var stop = searchFunc(loc); |
| 67 | if(stop){ break; } |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | var dir = arguments[0]; |
| 72 | var logDir = arguments[1]; |
| 73 | var logStr = ""; |
| 74 | |
| 75 | print('arrayInherit.js...'); |
| 76 | |
| 77 | // limit search to gregorian.js files, which are the only ones to use Array as data type |
| 78 | var fileList = fileUtil.getFilteredFileList(dir, /\/gregorian\.js$/, true); |
| 79 | |
| 80 | for(var i= 0; i < fileList.length; i++){ |
| 81 | //Use new String so we get a JS string and not a Java string. |
| 82 | var jsFileName = new String(fileList[i]); |
| 83 | var data = null; |
| 84 | var jsPath = jsFileName.split("/"); |
| 85 | var localeIndex = jsPath.length-2; |
| 86 | var locale = jsPath[localeIndex]; |
| 87 | if(locale=="nls"){continue;} // don't run on ROOT resource |
| 88 | var hasChanged = false; |
| 89 | |
| 90 | try{ |
| 91 | // dojo.i18n._requireLocalization('dojo.cldr', 'gregorian', locale); |
| 92 | var bundle = dojo.i18n.getLocalization('dojo.cldr', 'gregorian', locale); //flattened bundle |
| 93 | }catch(e){/* logStr += "arrayInherit: an exception occurred: "+e;/* simply ignore if no bundle found*/} |
| 94 | |
| 95 | _searchLocalePath(locale, true, function(variant){ |
| 96 | var isComplete = false; |
| 97 | var path = jsPath; |
| 98 | if(variant=="ROOT"){ |
| 99 | path = path.slice(0, localeIndex); |
| 100 | path.push(jsPath[localeIndex+1]); |
| 101 | }else{ |
| 102 | path[localeIndex] = variant; |
| 103 | } |
| 104 | var contents; |
| 105 | try{ |
| 106 | contents = new String(readFile(path.join("/"), "utf-8")); |
| 107 | }catch(e){ |
| 108 | print(e); //TODO: should we be catching this? |
| 109 | return false; |
| 110 | } |
| 111 | var variantData = dojo.fromJson(contents); |
| 112 | if(!data){ |
| 113 | data = variantData; |
| 114 | }else{ |
| 115 | isComplete = true; |
| 116 | //logStr += locale + "===============================================\n"; |
| 117 | for(prop in data){ |
| 118 | if(dojo.isArray(data[prop])){ |
| 119 | //ignore if the property is an alias source, for alias.js and specialLocale.js |
| 120 | if(isLocaleAliasSrc(prop, bundle)){ |
| 121 | //logStr += prop + " is alias, ignored\n"; |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | var variantArray = variantData[prop]; |
| 126 | dojo.forEach(data[prop], function(element, index, list){ |
| 127 | if(element === undefined && dojo.isArray(variantArray)){ |
| 128 | list[index] = variantArray[index]; |
| 129 | //logStr += prop + "[" + index + "] undefined, is replaced with " + list[index] + "\n"; |
| 130 | hasChanged = true; |
| 131 | if(!("index" in list)){ |
| 132 | isComplete = false; |
| 133 | } |
| 134 | } |
| 135 | }); |
| 136 | if(dojo.isArray(variantArray) && variantArray.length > data[prop].length){ |
| 137 | data[prop] = data[prop].concat(variantArray.slice(data[prop].length)); |
| 138 | hasChanged = true; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | //logStr += "\n"; |
| 143 | } |
| 144 | return isComplete; |
| 145 | }); |
| 146 | if(hasChanged){ |
| 147 | fileUtil.saveUtf8File(jsFileName, "(" + dojo.toJson(data, true) + ")"); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | //fileUtil.saveUtf8File(logDir + '/arrayInherit.log',logStr+'\n'); |