-reLibintl_h = re.compile (r'#\s*include\s*<libintl.h>')
-reDESC = re.compile (r'(\s*DRI_CONF_DESC\s*\(\s*)([a-z]+)(\s*,\s*)(gettext\s*\(\s*")(.*)("\s*\))(\s*\)[ \t]*\\?)$')
-reDESC_BEGIN = re.compile (r'(\s*DRI_CONF_DESC_BEGIN\s*\(\s*)([a-z]+)(\s*,\s*)(gettext\s*\(\s*")(.*)("\s*\))(\s*\)[ \t]*\\?)$')
-reENUM = re.compile (r'(\s*DRI_CONF_ENUM\s*\([^,]+,\s*)(gettext\s*\(\s*")(.*)("\s*\))(\s*\)[ \t]*\\?)$')
-reDESC_END = re.compile (r'\s*DRI_CONF_DESC_END')
-
-# Print a header
-print("/***********************************************************************\n" \
-" *** THIS FILE IS GENERATED AUTOMATICALLY. DON'T EDIT! ***\n" \
-" ***********************************************************************/")
-
-# Process the options template and generate options.h with all
-# translations.
-template = open (template_header_path, "rb")
-descMatches = []
-for line in template:
- line = line.decode('utf-8')
-
- if len(descMatches) > 0:
- matchENUM = reENUM .match (line)
- matchDESC_END = reDESC_END.match (line)
- if matchENUM:
- descMatches.append (matchENUM)
- elif matchDESC_END:
- expandMatches (descMatches, translations, line)
+reLibintl_h = re.compile(r'#\s*include\s*<libintl.h>')
+reDESC = re.compile(r'(\s*DRI_CONF_DESC\s*\(\s*)([a-z]+)(\s*,\s*)(gettext\s*\(\s*")(.*)("\s*\))(\s*\)[ \t]*\\?)$')
+reDESC_BEGIN = re.compile(r'(\s*DRI_CONF_DESC_BEGIN\s*\(\s*)([a-z]+)(\s*,\s*)(gettext\s*\(\s*")(.*)("\s*\))(\s*\)[ \t]*\\?)$')
+reENUM = re.compile(r'(\s*DRI_CONF_ENUM\s*\([^,]+,\s*)(gettext\s*\(\s*")(.*)("\s*\))(\s*\)[ \t]*\\?)$')
+reDESC_END = re.compile(r'\s*DRI_CONF_DESC_END')
+
+
+def main():
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--template', required=True)
+ parser.add_argument('--output', required=True)
+ parser.add_argument('--localedir', required=True)
+ parser.add_argument('--languages', nargs='*', default=[])
+ args = parser.parse_args()
+
+ # Compile a list of translation classes to all supported languages.
+ # The first translation is always a NullTranslations.
+ translations = [("en", gettext.NullTranslations())]
+ for lang in args.languages:
+ try:
+ filename = os.path.join(args.localedir, '{}.gmo'.format(lang))
+ with io.open(filename, 'rb') as f:
+ trans = gettext.GNUTranslations(f)
+ except (IOError, OSError):
+ print("Warning: language '%s' not found." % lang, file=sys.stderr)
+ continue
+ translations.append((lang, trans))
+
+ with io.open(args.output, mode='wt', encoding='utf-8') as output:
+ output.write("/* This is file is generated automatically. Don't edit! */\n")
+
+ # Process the options template and generate options.h with all
+ # translations.
+ with io.open(args.template, mode="rt", encoding='utf-8') as template: