summaryrefslogtreecommitdiff
path: root/packages/pogen/src/po2ts.ts
blob: 0b5b1384da5812edad16e9f0b087b37beca04de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 This file is part of GNU Taler
 (C) 2020 Taler Systems S.A.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

/**
 * Convert a <lang>.po file into a JavaScript / TypeScript expression.
 */

// @ts-ignore
import * as po2jsonLib from "po2json";
import * as fs from "fs";
import glob = require("glob");

//types defined by the po2json library
type Header = {
  domain: string;
  lang: string;
  'plural_forms': string;
};

type MessagesType = Record<string, undefined | Array<string>> & { "": Header }
interface pojsonType {
  // X-Domain or 'messages'
  domain: string;
  locale_data: {
    messages: MessagesType
  }
}
// ----------- end pf po2json

interface StringsType {
  // X-Domain or 'messages'
  domain: string;
  lang: string;
  completeness: number,
  'plural_forms': string;
  locale_data: {
    messages: Record<string, undefined | Array<string>>
  }
}

// This prelude match the types above
const TYPES_FOR_STRING_PRELUDE = `
export interface StringsType {
  domain: string;
  lang: string;
  completeness: number;
  'plural_forms': string;
  locale_data: {
    messages: Record<string, unknown>;
  };
};
`;

const DEFAULT_STRING_PRELUDE = `${TYPES_FOR_STRING_PRELUDE}export const strings: Record<string,StringsType> = {};\n\n`


export function po2ts(): void {
  const files = glob.sync("src/i18n/*.po");

  if (files.length === 0) {
    console.error("no .po files found in src/i18n/");
    process.exit(1);
  }

  console.log(files);

  let prelude: string;
  try {
    prelude = fs.readFileSync("src/i18n/strings-prelude", "utf-8")
  } catch (e) {
    prelude = DEFAULT_STRING_PRELUDE
  }

  const chunks = [prelude];

  for (const filename of files) {
    const m = filename.match(/([a-zA-Z0-9-_]+).po/);

    if (!m) {
      console.error("error: unexpected filename (expected <lang>.po)");
      process.exit(1);
    }

    const lang = m[1];
    const poAsJson: pojsonType = po2jsonLib.parseFileSync(filename, {
      format: "jed1.x",
      fuzzy: true,
    });
    const header = poAsJson.locale_data.messages[""]
    const total = calculateTotalTranslations(poAsJson.locale_data.messages)
    const completeness =
      header.lang === "en"
        ? 100 // 'en' is always complete
        : Math.floor(total.translations * 100 / total.keys);

    const strings: StringsType = {
      locale_data: poAsJson.locale_data,
      domain: poAsJson.domain,
      plural_forms: header.plural_forms,
      lang: header.lang,
      completeness,
    }
    const value = JSON.stringify(strings, undefined, 2)
    const s = `strings['${lang}'] = ${value};\n\n`
    chunks.push(s);
  }

  const tsContents = chunks.join("");

  fs.writeFileSync("src/i18n/strings.ts", tsContents);
}

function calculateTotalTranslations(msgs: MessagesType): { keys: number, translations: number } {
  const kv = Object.entries(msgs)
  const [keys, translations] = kv.reduce(([total, withTranslation], translation) => {
    if (!translation || translation.length !== 2 || !translation[1]) {
      //current key is empty
      return [total, withTranslation]
    }
    const v = translation[1]
    if (!Array.isArray(v)) {
      // this is not a translation
      return [total, withTranslation]
    }
    if (!v.length || !v[0].length) {
      //translation is missing
      return [total + 1, withTranslation]
    }
    //current key has a translation
    return [total + 1, withTranslation + 1]
  }, [0, 0])
  return { keys, translations }
}