def extract_from_jinja2(*args, **kwargs): """Just like Jinja2's Babel extractor, but fixes the i18n issue The Jinja2 Babel extractor appends the i18n extension to the list of extensions before extracting. Since Puente has its own i18n extension, this creates problems. So we monkeypatch and then call Jinja2's Babel extractor. .. Note:: You only need to use this if you're using Babel's pybabel extract. If you're using Puente's extract command, then it does this already and you can use the Jinja2 Babel extractor. """ # Must monkeypatch first to fix InternationalizationExtension # stomping issues! See docstring for details. monkeypatch_i18n() return babel_extract(*args, **kwargs)
def extract_command(domain, outputdir, domain_methods, standalone_domains, text_domain, keywords, comment_tags, base_dir): """Extracts strings into .pot files :arg domain: domains to generate strings for or 'all' for all domains :arg outputdir: output dir for .pot files; usually locale/templates/LC_MESSAGES/ :arg domain_methods: DOMAIN_METHODS setting :arg standalone_domains: STANDALONE_DOMAINS setting :arg text_domain: TEXT_DOMAIN settings :arg keywords: KEYWORDS setting :arg comment_tags: COMMENT_TAGS setting :arg base_dir: BASE_DIR setting """ # Must monkeypatch first to fix i18n extensions stomping issues! monkeypatch_i18n() # Create the outputdir if it doesn't exist outputdir = os.path.abspath(outputdir) if not os.path.isdir(outputdir): print 'Creating output dir %s ...' % outputdir os.makedirs(outputdir) # Figure out what domains to extract if domain == DEFAULT_DOMAIN_VALUE: domains = domain_methods.keys() else: domains = [domain] def callback(filename, method, options): if method != 'ignore': print ' %s' % filename # Extract string for each domain for domain in domains: print 'Extracting all strings in domain %s...' % (domain) methods = domain_methods[domain] catalog = Catalog(charset='utf-8', header_comment='') extracted = extract_from_dir( base_dir, method_map=methods, options_map=generate_options_map(), keywords=keywords, comment_tags=comment_tags, callback=callback, ) for filename, lineno, msg, cmts, ctxt in extracted: catalog.add(msg, None, [(filename, lineno)], auto_comments=cmts, context=ctxt) with open(os.path.join(outputdir, '%s.pot' % domain), 'w') as fp: write_po(fp, catalog, width=80) not_standalone_domains = [ dom for dom in domains if dom not in standalone_domains ] pot_files = [] for dom in not_standalone_domains: pot_files.append(os.path.join(outputdir, '%s.pot' % dom)) if len(pot_files) > 1: pot_file = text_domain + '.pot' print ('Concatenating the non-standalone domains into %s' % pot_file) final_out = os.path.join(outputdir, pot_file) # We add final_out back on because msgcat will combine all # specified files. We'll redirect everything back in to # final_out in a minute. pot_files.append(final_out) meltingpot = tempfile.TemporaryFile() p1 = Popen(['msgcat'] + pot_files, stdout=meltingpot) p1.communicate() meltingpot.seek(0) # w+ truncates the file first with open(final_out, 'w+') as final: final.write(meltingpot.read()) meltingpot.close() for dom in not_standalone_domains: os.remove(os.path.join(outputdir, '%s.pot' % dom)) print 'Done'
def extract_command(outputdir, domain_methods, text_domain, keywords, comment_tags, base_dir, project, version, msgid_bugs_address): """Extracts strings into .pot files :arg domain: domains to generate strings for or 'all' for all domains :arg outputdir: output dir for .pot files; usually locale/templates/LC_MESSAGES/ :arg domain_methods: DOMAIN_METHODS setting :arg text_domain: TEXT_DOMAIN settings :arg keywords: KEYWORDS setting :arg comment_tags: COMMENT_TAGS setting :arg base_dir: BASE_DIR setting :arg project: PROJECT setting :arg version: VERSION setting :arg msgid_bugs_address: MSGID_BUGS_ADDRESS setting """ # Must monkeypatch first to fix i18n extensions stomping issues! monkeypatch_i18n() # Create the outputdir if it doesn't exist outputdir = os.path.abspath(outputdir) if not os.path.isdir(outputdir): print('Creating output dir %s ...' % outputdir) os.makedirs(outputdir) domains = domain_methods.keys() def callback(filename, method, options): if method != 'ignore': print(' %s' % filename) # Extract string for each domain for domain in domains: print('Extracting all strings in domain %s...' % domain) methods = domain_methods[domain] catalog = Catalog( header_comment='', project=project, version=version, msgid_bugs_address=msgid_bugs_address, charset='utf-8', ) extracted = extract_from_dir( base_dir, method_map=methods, options_map=generate_options_map(), keywords=keywords, comment_tags=comment_tags, callback=callback, ) for filename, lineno, msg, cmts, ctxt in extracted: catalog.add(msg, None, [(filename, lineno)], auto_comments=cmts, context=ctxt) with open(os.path.join(outputdir, '%s.pot' % domain), 'wb') as fp: write_po(fp, catalog, width=80) print('Done')