def extract_messages(): """generate .pot/.ts files from sources""" # generate UI .ts file sh('pylupdate4 -noobsolete MetaSearch.pro') # generate .po file from plugin templates env = Environment(extensions=['jinja2.ext.i18n'], loader=FileSystemLoader(options.base.plugin)) msg_strings = [] for tfile in ['service_metadata.html', 'record_metadata_dc.html']: html_file = options.base.plugin / 'resources/templates' / tfile for msg in env.extract_translations(open(html_file).read()): if msg[2] not in msg_strings: msg_strings.append(msg[2]) po_file = options.base.plugin / 'locale/en/LC_MESSAGES/templates.po' with open(po_file, 'w') as po_file_obj: po_file_obj.write( '\nmsgid ""\n' 'msgstr ""\n' '"Project-Id-Version: MetaSearch 0.1-dev\\n"\n' '"Report-Msgid-Bugs-To: \\n"\n' '"POT-Creation-Date: 2014-02-25 12:58-0500\\n"\n' '"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"\n' '"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"\n' '"Language-Team: LANGUAGE <*****@*****.**>\\n"\n' '"MIME-Version: 1.0\\n"\n' '"Content-Type: text/plain; charset=UTF-8\\n"\n' '"Content-Transfer-Encoding: 8bit\\n"\n\n') for msg in msg_strings: po_file_obj.write('msgid "%s"\nmsgstr ""\n\n' % msg) # generate docs .po files with pushd(options.base.docs): sh('make gettext') locales_arg = '' for lang in os.listdir('locale'): locales_arg = '%s -l %s' % (locales_arg, lang) sh('sphinx-intl update -p _build/locale %s' % locales_arg)
def extract_messages(): """generate .pot/.ts files from sources""" # generate UI .ts file sh('pylupdate4 -noobsolete MetaSearch.pro') # generate .po file from plugin templates env = Environment(extensions=['jinja2.ext.i18n'], loader=FileSystemLoader(options.base.plugin)) msg_strings = [] for tfile in ['service_metadata.html', 'record_metadata_dc.html']: html_file = options.base.plugin / 'resources/templates' / tfile for msg in env.extract_translations(open(html_file).read()): if msg[2] not in msg_strings: msg_strings.append(msg[2]) po_file = options.base.plugin / 'locale/en/LC_MESSAGES/templates.po' with open(po_file, 'w') as po_file_obj: po_file_obj.write('\nmsgid ""\n' 'msgstr ""\n' '"Project-Id-Version: MetaSearch 0.1-dev\\n"\n' '"Report-Msgid-Bugs-To: \\n"\n' '"POT-Creation-Date: 2014-02-25 12:58-0500\\n"\n' '"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"\n' '"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"\n' '"Language-Team: LANGUAGE <*****@*****.**>\\n"\n' '"MIME-Version: 1.0\\n"\n' '"Content-Type: text/plain; charset=UTF-8\\n"\n' '"Content-Transfer-Encoding: 8bit\\n"\n\n') for msg in msg_strings: po_file_obj.write('msgid "%s"\nmsgstr ""\n\n' % msg) # generate docs .po files with pushd(options.base.docs): sh('make gettext') locales_arg = '' for lang in os.listdir('locale'): locales_arg = '%s -l %s' % (locales_arg, lang) sh('sphinx-intl update -p _build/locale %s' % locales_arg)
class MakeMessages(object): msguniq_options = ['--to-code=utf-8'] msgmerge_options = ['-q', '--previous'] def __init__(self): languages = config.get_languages() if len(languages) == 1 and languages[0] == constants.DEFAULT_LANG: logger.error( 'No languages configuration detected, can\'t create messages') sys.exit(1) loader = FileSystemLoader(config.files_path) self.env = Environment(extensions=['jinja2.ext.i18n'], loader=loader) self.env.filters['clean_msg'] = clean_msg self.translations = [] self.pot_filename = '{}.pot'.format(constants.GETTEXT_DOMAIN) self.po_filename = '{}.po'.format(constants.GETTEXT_DOMAIN) self.pot_file_path = os.path.join(config.locales_path, self.pot_filename) def run(self): self.extract_translations() self.generate_pot_file() self.generate_po_file() self.delete_pot_file() def extract_translations(self): for root, directory, files in os.walk(config.templates_path): for f in files: if not f.endswith(tuple(constants.VALID_TEMPLATES_EXTENSIONS)): continue file_path = os.path.join(root, f) relative_file_path = file_path.replace(config.base_path, '') with open(file_path, 'r') as content: translations = self.env.extract_translations( content.read()) for t in translations: t = list(t) t.insert(0, relative_file_path) self.translations.append(t) def generate_pot_file(self): pot_template = self.env.get_template('pot_template.jinja') pot_content = pot_template.render({'translations': self.translations}) if not os.path.isdir(config.locales_path): os.makedirs(config.locales_path) with open(self.pot_file_path, 'w', encoding='utf-8') as f: f.write(pot_content) msguniq_args = ['msguniq' ] + self.msguniq_options + [self.pot_file_path] output, errors, status = call_subprocess(msguniq_args) if status != constants.STATUS_OK and errors: logger.error( "errors happened while running msguniq\n{}".format(errors)) sys.exit(1) elif errors: logger.error(errors) # Replace pot file with msguniq output with open(self.pot_file_path, 'w', encoding='utf-8') as f: f.write(output) def generate_po_file(self): for language in config.get_languages(): locale_path = os.path.join(config.locales_path, language, 'LC_MESSAGES') po_file_path = os.path.join(locale_path, self.po_filename) po_content = '' if not os.path.isdir(locale_path): os.makedirs(locale_path) if not os.path.exists(po_file_path): with open(self.pot_file_path, 'r') as f: po_content = f.read() else: msgmerge_args = ['msgmerge'] + self.msgmerge_options + [ po_file_path, self.pot_file_path ] po_content, errors, status = call_subprocess(msgmerge_args) if status != constants.STATUS_OK and errors: logger.error( "errors happened while running msgmerge\n{}".format( errors)) sys.exit(1) elif errors: logger.error(errors) with open(po_file_path, 'w', encoding='utf-8') as f: f.write(po_content) logger.info( '«.po» file generated for language «{}» at «{}»'.format( language, po_file_path)) def delete_pot_file(self): os.remove(self.pot_file_path)
from jinja2 import Environment env = Environment(extensions=['jinja2.ext.i18n']) print ( '# SOME DESCRIPTIVE TITLE.\n' '# Copyright (C) YEAR THE PACKAGE\'S COPYRIGHT HOLDER\n' '# This file is distributed under the same license as the PACKAGE package.\n' '# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n' '#\n' '#, fuzzy\n' 'msgid ""\n' 'msgstr ""\n' '"Project-Id-Version: PACKAGE VERSION\\n"\n' '"Report-Msgid-Bugs-To: \\n"\n' '"POT-Creation-Date: 2012-01-31 19:11+0400\\n"\n' '"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"\n' '"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"\n' '"Language-Team: LANGUAGE <*****@*****.**>\\n"\n' '"Language: \\n"\n' '"MIME-Version: 1.0\\n"\n' '"Content-Type: text/plain; charset=CHARSET\\n"\n' '"Content-Transfer-Encoding: 8bit\\n"\n' ) for fname in glob('templates/*.html'): for i in env.extract_translations(unicode(file(fname).read(), "utf8")): print '#: %s:%s' % (fname, i[0]) print 'msgid "%s"' % i[2] print 'msgstr ""\n'