|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +''' |
| 4 | +Copyright 2022 Veloman Yunkan <[email protected]> |
| 5 | +
|
| 6 | +This program is free software; you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU General Public License as published by |
| 8 | +the Free Software Foundation; either version 3 of the License, or any |
| 9 | +later version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful, but |
| 12 | +WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU General Public License |
| 17 | +along with this program; if not, write to the Free Software |
| 18 | +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 | +02110-1301, USA. |
| 20 | +''' |
| 21 | + |
| 22 | +import argparse |
| 23 | +import os.path |
| 24 | +import re |
| 25 | +import json |
| 26 | + |
| 27 | +def to_identifier(name): |
| 28 | + ident = re.sub(r'[^0-9a-zA-Z]', '_', name) |
| 29 | + if ident[0].isnumeric(): |
| 30 | + return "_"+ident |
| 31 | + return ident |
| 32 | + |
| 33 | +def lang_code(filename): |
| 34 | + filename = os.path.basename(filename) |
| 35 | + lang = to_identifier(os.path.splitext(filename)[0]) |
| 36 | + print(filename, '->', lang) |
| 37 | + return lang |
| 38 | + |
| 39 | +from string import Template |
| 40 | + |
| 41 | +def expand_cxx_template(t, **kwargs): |
| 42 | + return Template(t).substitute(**kwargs) |
| 43 | + |
| 44 | +def cxx_string_literal(s): |
| 45 | + # Taking advantage of the fact the JSON string escape rules match |
| 46 | + # those of C++ |
| 47 | + return 'u8' + json.dumps(s) |
| 48 | + |
| 49 | +string_table_cxx_template = ''' |
| 50 | +const I18nString $TABLE_NAME[] = { |
| 51 | + $TABLE_ENTRIES |
| 52 | +}; |
| 53 | +''' |
| 54 | + |
| 55 | +lang_table_entry_cxx_template = ''' |
| 56 | + { |
| 57 | + $LANG_STRING_LITERAL, |
| 58 | + ARRAY_ELEMENT_COUNT($STRING_TABLE_NAME), |
| 59 | + $STRING_TABLE_NAME |
| 60 | + }''' |
| 61 | + |
| 62 | +cxxfile_template = '''// This file is automatically generated. Do not modify it. |
| 63 | +
|
| 64 | +#include "server/i18n.h" |
| 65 | +
|
| 66 | +namespace kiwix { |
| 67 | +namespace i18n { |
| 68 | +
|
| 69 | +namespace |
| 70 | +{ |
| 71 | +
|
| 72 | +$STRING_DATA |
| 73 | +
|
| 74 | +} // unnamed namespace |
| 75 | +
|
| 76 | +#define ARRAY_ELEMENT_COUNT(a) (sizeof(a)/sizeof(a[0])) |
| 77 | +
|
| 78 | +extern const I18nStringTable stringTables[] = { |
| 79 | + $LANG_TABLE |
| 80 | +}; |
| 81 | +
|
| 82 | +extern const size_t langCount = $LANG_COUNT; |
| 83 | +
|
| 84 | +} // namespace i18n |
| 85 | +} // namespace kiwix |
| 86 | +''' |
| 87 | + |
| 88 | +class Resource: |
| 89 | + def __init__(self, base_dirs, filename): |
| 90 | + filename = filename.strip() |
| 91 | + self.filename = filename |
| 92 | + self.lang_code = lang_code(filename) |
| 93 | + found = False |
| 94 | + for base_dir in base_dirs: |
| 95 | + try: |
| 96 | + with open(os.path.join(base_dir, filename), 'r') as f: |
| 97 | + self.data = f.read() |
| 98 | + found = True |
| 99 | + break |
| 100 | + except FileNotFoundError: |
| 101 | + continue |
| 102 | + if not found: |
| 103 | + raise Exception("Impossible to find {}".format(filename)) |
| 104 | + |
| 105 | + |
| 106 | + def get_string_table_name(self): |
| 107 | + return "string_table_for_" + self.lang_code |
| 108 | + |
| 109 | + def get_string_table(self): |
| 110 | + table_entries = ",\n ".join(self.get_string_table_entries()) |
| 111 | + return expand_cxx_template(string_table_cxx_template, |
| 112 | + TABLE_NAME=self.get_string_table_name(), |
| 113 | + TABLE_ENTRIES=table_entries) |
| 114 | + |
| 115 | + def get_string_table_entries(self): |
| 116 | + d = json.loads(self.data) |
| 117 | + for k in sorted(d.keys()): |
| 118 | + if k != "@metadata": |
| 119 | + key_string = cxx_string_literal(k) |
| 120 | + value_string = cxx_string_literal(d[k]) |
| 121 | + yield '{ ' + key_string + ', ' + value_string + ' }' |
| 122 | + |
| 123 | + def get_lang_table_entry(self): |
| 124 | + return expand_cxx_template(lang_table_entry_cxx_template, |
| 125 | + LANG_STRING_LITERAL=cxx_string_literal(self.lang_code), |
| 126 | + STRING_TABLE_NAME=self.get_string_table_name()) |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +def gen_c_file(resources): |
| 131 | + string_data = [] |
| 132 | + lang_table = [] |
| 133 | + for r in resources: |
| 134 | + string_data.append(r.get_string_table()) |
| 135 | + lang_table.append(r.get_lang_table_entry()) |
| 136 | + |
| 137 | + return expand_cxx_template(cxxfile_template, |
| 138 | + STRING_DATA="\n".join(string_data), |
| 139 | + LANG_TABLE=",\n ".join(lang_table), |
| 140 | + LANG_COUNT=len(resources) |
| 141 | + ) |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + parser = argparse.ArgumentParser() |
| 147 | + parser.add_argument('--cxxfile', |
| 148 | + required=True, |
| 149 | + help='The Cpp file name to generate') |
| 150 | + parser.add_argument('i18n_resource_file', |
| 151 | + help='The list of resources to compile.') |
| 152 | + args = parser.parse_args() |
| 153 | + |
| 154 | + base_dir = os.path.dirname(os.path.realpath(args.i18n_resource_file)) |
| 155 | + with open(args.i18n_resource_file, 'r') as f: |
| 156 | + resources = [Resource([base_dir], filename) |
| 157 | + for filename in f.readlines()] |
| 158 | + |
| 159 | + with open(args.cxxfile, 'w') as f: |
| 160 | + f.write(gen_c_file(resources)) |
| 161 | + |
0 commit comments