|
| 1 | +/*jshint node:true, eqnull:true, laxbreak:true, newcap:false, shadow:true, funcscope:true*/ |
| 2 | +// Copyright 2013 Andy Earnshaw, MIT License |
| 3 | + |
| 4 | +/** |
| 5 | + * Downloads the latest CLDR data in JSON format from the Unicode site. |
| 6 | + */ |
| 7 | + |
| 8 | +var |
| 9 | + // Our wrapping function for JSONP |
| 10 | + jsonp = 'Intl.__addLocaleData', |
| 11 | + |
| 12 | + fs = require('fs'), |
| 13 | + http = require('http'), |
| 14 | + util = require('util'), |
| 15 | + |
| 16 | + httpOpts = { |
| 17 | + host: 'www.unicode.org', |
| 18 | + path: '/repos/cldr-aux/json/22.1/main/' |
| 19 | + }, |
| 20 | + |
| 21 | + links = [], |
| 22 | + html = '', |
| 23 | + evil = /<a href="(\w+\.json)"/gi, |
| 24 | + req = http.request(httpOpts, function(res) { |
| 25 | + util.log('Downloading list of CLDR locales.'); |
| 26 | + |
| 27 | + res.on('error', function (err) { |
| 28 | + util.error('Unable to download list ('+err+')'); |
| 29 | + process.exit(1); |
| 30 | + }); |
| 31 | + |
| 32 | + res.on('data', function (chunk) { |
| 33 | + html += chunk; |
| 34 | + }); |
| 35 | + |
| 36 | + res.on('end', function () { |
| 37 | + while (evil.exec(html)) { |
| 38 | + links.push(RegExp.$1); |
| 39 | + } |
| 40 | + |
| 41 | + util.log('Parsed aforementioned list from the HTML (using a regex :evil laugh:).'); |
| 42 | + |
| 43 | + downloadLocales(); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | +req.end(); |
| 48 | + |
| 49 | +/** |
| 50 | + * Loops over the `links` array and fetches the individual locale data |
| 51 | + */ |
| 52 | +function downloadLocales() { |
| 53 | + links.forEach(function (lcl) { |
| 54 | + var opts = { |
| 55 | + host: httpOpts.host, |
| 56 | + path: httpOpts.path + lcl |
| 57 | + }, |
| 58 | + json = '', |
| 59 | + req = http.request(opts, function (res) { |
| 60 | + res.on('error', function (err) { |
| 61 | + util.log('Unable to download '+lcl+' ('+err+')'); |
| 62 | + }); |
| 63 | + res.on('data', function (chunk) { |
| 64 | + json += chunk; |
| 65 | + }); |
| 66 | + res.on('end', function () { |
| 67 | + saveJSON(lcl, json); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + req.end(); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Saves the JSON data from a locale to the correct files (JSON + JSONP); |
| 77 | + */ |
| 78 | +function saveJSON(lcl, json) { |
| 79 | + var obj; |
| 80 | + lcl = lcl.replace(/_/g, '-'); |
| 81 | + |
| 82 | + try { |
| 83 | + obj = JSON.parse(json); |
| 84 | + } |
| 85 | + catch (e) { |
| 86 | + util.error(lcl + '.json will not parse ('+e.message+')'); |
| 87 | + return; |
| 88 | + } |
| 89 | + try { |
| 90 | + fs.writeFileSync('json/' + lcl, json); |
| 91 | + util.log('Saved '+lcl+' data in JSON format.'); |
| 92 | + } |
| 93 | + catch (e) { |
| 94 | + util.error('Unable to save '+lcl+' data in JSON format ('+e.message+')'); |
| 95 | + } |
| 96 | + |
| 97 | + try { |
| 98 | + fs.writeFileSync('jsonp/' + lcl.slice(0, -4) + 'js', jsonp + '(' + JSON.stringify(obj) + ')'); |
| 99 | + util.log('Saved '+lcl+' data in JSONP format.'); |
| 100 | + } |
| 101 | + catch (e) { |
| 102 | + util.error('Unable to save '+lcl+' data in JSONP format ('+e.message+')'); |
| 103 | + } |
| 104 | +} |
0 commit comments