@@ -9,10 +9,20 @@ for (const arg of process.argv.slice(2)) {
9
9
writePoFile ( arg , outpath ) ;
10
10
}
11
11
12
+ function assertHeader ( name , value ) {
13
+ if ( ! value ) {
14
+ throw new Error ( `Bad .po file. "${ name } " header is missing` ) ;
15
+ }
16
+ }
17
+
12
18
function writePoFile ( inpath , outpath ) {
13
19
const input = fs . readFileSync ( inpath ) . toString ( ) . normalize ( ) ;
14
20
const poData = po . parse ( input ) ;
15
-
21
+ const pluralHeader =
22
+ poData . headers [ 'plural-forms' ] || poData . headers [ 'Plural-Forms' ] ;
23
+ const language = poData . headers . language || poData . headers . Language ;
24
+ assertHeader ( 'Plural-Forms' , pluralHeader ) ;
25
+ assertHeader ( 'Language' , language ) ;
16
26
const dict = { } ;
17
27
for ( const msg of Object . values ( poData . translations [ '' ] ) ) {
18
28
const msgid = msg . msgid ;
@@ -23,21 +33,11 @@ function writePoFile(inpath, outpath) {
23
33
}
24
34
const compactPo = {
25
35
headers : {
26
- 'plural-forms' : '' ,
27
- language : 'en' ,
28
- } ,
29
- contexts : {
30
- '' : dict ,
36
+ 'plural-forms' : pluralHeader ,
37
+ language : language ,
31
38
} ,
39
+ contexts : { '' : dict } ,
32
40
} ;
33
- const pluralHeader =
34
- poData . headers [ 'plural-forms' ] || poData . headers [ 'Plural-Forms' ] ;
35
- if ( ! pluralHeader ) {
36
- throw new Error ( 'Bad .po file. "Plural-Forms" header is missing ' ) ;
37
- }
38
- compactPo . headers [ 'plural-forms' ] = pluralHeader ;
39
- compactPo . headers . language = poData . headers . language ;
40
-
41
41
const outstream = fs . createWriteStream ( outpath , { flags : 'w' } ) ;
42
42
outstream . write ( 'export default ' ) ;
43
43
outstream . write ( JSON . stringify ( compactPo , null , 0 ) ) ;
0 commit comments