Skip to content
This repository was archived by the owner on Apr 14, 2021. It is now read-only.

Commit 19bb70d

Browse files
committed
Merge language data into territory data
1 parent 14cff84 commit 19bb70d

File tree

3 files changed

+96
-6
lines changed

3 files changed

+96
-6
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cldr/

tools/Ldml2Json.js

+90-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*jshint laxbreak:true */
12
/**
23
* Converts Unicode CLDR data to JSON format for use with Intl.js
34
* Copyright 2013 Andy Earnshaw, MIT License
@@ -11,6 +12,14 @@
1112
var
1213
child,
1314
spawn = require('child_process').spawn,
15+
fs = require('fs'),
16+
17+
// The 'callback' function for the JSONP files
18+
jsonpFn = 'Intl.__addLocaleData',
19+
20+
// Regex for converting locale JSON to object grammar, obviously simple and
21+
// incomplete but should be good enough for the CLDR JSON
22+
jsonpExp = /"(?!default)([\w$][\w\d$]+)":/g,
1423

1524
// Path to CLDR root
1625
cldr = process.argv[2];
@@ -37,16 +46,91 @@ function cleanUp () {
3746
process.on('exit', cleanUp);
3847
process.on('SIGINT', cleanUp);
3948

40-
child = spawn('java', [ '-DCLDR_DIR='+cldr, '-cp', jPath + clsPaths.join(':'+jPath), cls, '-d', out, '-k', cfg, '-m *en*' ]);
49+
// Usage: Ldml2JsonConverter [OPTIONS] [FILES]
50+
// This program converts CLDR data to the JSON format.
51+
// Please refer to the following options.
52+
// example: org.unicode.cldr.json.Ldml2JsonConverter -c xxx -d yyy
53+
// Here are the options:
54+
// -h (help) no-arg Provide the list of possible options
55+
// -c (commondir) .* Common directory for CLDR files, defaults to CldrUtility.COMMON_DIRECTORY
56+
// -d (destdir) .* Destination directory for output files, defaults to CldrUtility.GEN_DIRECTORY
57+
// -m (match) .* Regular expression to define only specific locales or files to be generated
58+
// -t (type) (main|supplemental) Type of CLDR data being generated, main or supplemental.
59+
// -r (resolved) (true|false) Whether the output JSON for the main directory should be based on resolved or unresolved data
60+
// -s (draftstatus) (approved|contributed|provisional|unconfirmed) The minimum draft status of the output data
61+
// -l (coverage) (minimal|basic|moderate|modern|comprehensive|optional) The maximum coverage level of the output data
62+
// -n (fullnumbers) (true|false) Whether the output JSON should output data for all numbering systems, even those not used in the locale
63+
// -o (other) (true|false) Whether to write out the 'other' section, which contains any unmatched paths
64+
// -k (konfig) .* LDML to JSON configuration file
65+
child = spawn('java', [ '-DCLDR_DIR='+cldr, '-cp', jPath + clsPaths.join(':'+jPath), cls, '-d', out, '-k', cfg/*, '-men.*' */]);
4166

4267
child.stdout.on('data', function (data) {
4368
if (data.toString().indexOf('Processing') >= 0)
44-
process.stdout.write('\r\x1b[K\r\t' + String(data).slice(0, String(data).indexOf('\n')));
69+
process.stdout.write('\r\x1b[K\r\t' + String(data).split('\n')[0]);
70+
});
71+
72+
var ldml2jsonErr = '';
73+
74+
child.stderr.on('data', function (data) {
75+
ldml2jsonErr += String(data);
4576
});
4677

4778
child.on('exit', function (err) {
48-
if (err !== 0)
49-
process.stderr.write(String(err));
50-
else
51-
console.log('\n\nDone!');
79+
if (err !== 0) {
80+
process.stderr.write(ldml2jsonErr);
81+
process.stderr.write('\nLdml2JsonConverter exited with error code ' +err);
82+
}
83+
else {
84+
console.log('\n\nProcessing JSON data...\n');
85+
var langData,
86+
locales = fs.readdirSync(out);
87+
88+
locales.forEach(function (dir) {
89+
var json, obj;
90+
91+
// The Ldml2JsonConverter tool creats directories even for locales that have
92+
// no data that we require
93+
try {
94+
json = fs.readFileSync(out + dir + '/data.json');
95+
obj = JSON.parse(json).main[dir];
96+
}
97+
catch (e) {
98+
return;
99+
}
100+
101+
// Need to copy in some language data that may not be present in territory data
102+
if (langData && (obj.identity.territory || obj.identity.script) && obj.identity.language === langData.identity.language)
103+
copyLocaleData(obj, langData);
104+
105+
else if (!obj.identity.territory && !obj.identity.script && !obj.identity.variant)
106+
langData = obj;
107+
108+
process.stdout.write('\r\x1b[K\r\tWriting locale-data/json/'+ dir +'.json');
109+
fs.writeFileSync('locale-data/json/'+ dir +'.json', JSON.stringify(obj, null, 4));
110+
111+
var jsonp = jsonpFn
112+
+ '('
113+
+ JSON.stringify(obj).replace(jsonpExp, '$1:')
114+
+ ')';
115+
116+
process.stdout.write('\r\x1b[K\r\tWriting locale-data/jsonp/'+ dir +'.js');
117+
fs.writeFileSync('locale-data/jsonp/'+ dir +'.js', jsonp);
118+
});
119+
120+
console.log('\n\nDone');
121+
}
52122
});
123+
124+
/**
125+
* Copies missing locale data from object `from` to object `to`
126+
*/
127+
function copyLocaleData(to, from) {
128+
for (var k in from) {
129+
if (!to.hasOwnProperty(k))
130+
to[k] = from[k];
131+
132+
else if (typeof from[k] === 'object')
133+
copyLocaleData(to[k], from[k]);
134+
}
135+
}
136+

tools/Ldml2JsonConverter.config

+5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
# Identity contains some useful information
2+
section=data ; path=//cldr/main/[^/]++/identity/.*
3+
14
# Match all calendar data except those chunky interval patterns
25
section=data ; path=//cldr/main/[^/]++/dates/calendars/((?!interval).)*
36

47
# Time zones
58
section=data ; path=//cldr/main/[^/]++/dates/timeZoneNames/((?!Format).)*
69

710
# Number formatting sections, minus any cruft
11+
section=data ; path=//cldr/main/[^/]++/numbers/currencies/symbols[^/]++/.*
12+
section=data ; path=//cldr/main/[^/]++/numbers/decimalFormats[^/]++/((?!long|short).)*
813
section=data ; path=//cldr/main/[^/]++/numbers/decimalFormats[^/]++/((?!long|short).)*
914
section=data ; path=//cldr/main/[^/]++/numbers/currencyFormats[^/]++/((?!long|short).)*
1015
section=data ; path=//cldr/main/[^/]++/numbers/percentFormats[^/]++/((?!long|short).)*

0 commit comments

Comments
 (0)