1
+ /*jshint laxbreak:true */
1
2
/**
2
3
* Converts Unicode CLDR data to JSON format for use with Intl.js
3
4
* Copyright 2013 Andy Earnshaw, MIT License
11
12
var
12
13
child ,
13
14
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 = / " (? ! d e f a u l t ) ( [ \w $ ] [ \w \d $ ] + ) " : / g,
14
23
15
24
// Path to CLDR root
16
25
cldr = process . argv [ 2 ] ;
@@ -37,16 +46,91 @@ function cleanUp () {
37
46
process . on ( 'exit' , cleanUp ) ;
38
47
process . on ( 'SIGINT' , cleanUp ) ;
39
48
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.*' */ ] ) ;
41
66
42
67
child . stdout . on ( 'data' , function ( data ) {
43
68
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 ) ;
45
76
} ) ;
46
77
47
78
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
+ }
52
122
} ) ;
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
+
0 commit comments