def rebuild_pot(potfile, package_dir, i18n_domain): if not os.path.exists(package_dir): return translatable_key_expr = re.compile( r'^[^:]*:translate\(%s\)$' % re.escape(i18n_domain)) content_creation_files = [] for dirpath, dirnames, filenames in os.walk(package_dir): if not dirpath.rstrip('/').endswith('/content_creation'): continue content_creation_files.extend( map(lambda name: os.path.join(dirpath, name), filter(lambda name: name.endswith('.json'), filenames))) msgids = set() for jsonpath in content_creation_files: data = load(open(jsonpath)) msgids.update(get_translated_values(data, translatable_key_expr)) if not msgids: return catalog = MessageCatalog(domain=i18n_domain) for msgid in msgids: catalog.add(msgid) potfile_dir = os.path.dirname(potfile) if not os.path.exists(potfile_dir): os.makedirs(potfile_dir) with open(potfile, 'w') as potfile: writer = POWriter(potfile, catalog) writer.write(msgstrToComment=True)
def rebuild_pot(potfile, package_dir, i18n_domain): if not os.path.exists(package_dir): return translatable_key_expr = re.compile(r'^[^:]*:translate\(%s\)$' % re.escape(i18n_domain)) content_creation_files = [] for dirpath, dirnames, filenames in os.walk(package_dir): if not dirpath.rstrip('/').endswith('/content_creation'): continue content_creation_files.extend( map(lambda name: os.path.join(dirpath, name), filter(lambda name: name.endswith('.json'), filenames))) msgids = set() for jsonpath in content_creation_files: data = load(open(jsonpath)) msgids.update(get_translated_values(data, translatable_key_expr)) if not msgids: return catalog = MessageCatalog(domain=i18n_domain) for msgid in msgids: catalog.add(msgid) potfile_dir = os.path.dirname(potfile) if not os.path.exists(potfile_dir): os.makedirs(potfile_dir) with open(potfile, 'w') as potfile: writer = POWriter(potfile, catalog) writer.write(msgstrToComment=True)
def _update_message_catalog(self, catalog_path, is_pot=False): if not os.path.exists(os.path.dirname(catalog_path)): os.makedirs(os.path.dirname(catalog_path)) if not os.path.exists(catalog_path): open(catalog_path, 'w+').close() generator = getUtility(IWorkflowGenerator) translations = generator.get_translations( self.workflow_id, self.specification) catalog = MessageCatalog(filename=catalog_path) def is_delete_candidate(msg): # When our spec-path is in the references of the message, # we treat the message as delete candiate. if self.relative_specification_path in msg.references: # Remove the reference. It will be added again if we # still need the message. msg.references.remove(self.relative_specification_path) return True return False delete_candidates = map(attrgetter('msgid'), filter(is_delete_candidate, catalog.values())) for msgid, msgstr in translations.items(): msgid = msgid.decode('utf-8').replace('"', '\\"') msgstr = msgstr.decode('utf-8').replace('"', '\\"') if msgid in delete_candidates: delete_candidates.remove(msgid) if msgid in catalog: msg = catalog[msgid] msg.msgstr = msgstr if self.relative_specification_path not in msg.references: msg.references.append(self.relative_specification_path) msg.automatic_comments = filter( lambda text: not text.startswith('Default: '), msg.automatic_comments) msg.automatic_comments.append(u'Default: "{0}"'.format(msgstr)) else: catalog.add(msgid, msgstr=msgstr, references=[self.relative_specification_path], automatic_comments=[ u'Default: "{0}"'.format(msgstr)]) if is_pot: catalog[msgid].msgstr = u'' for msgid in delete_candidates: if not catalog[msgid].references: del catalog[msgid] with open(catalog_path, 'w+') as catalog_file: writer = POWriter(catalog_file, catalog) writer.write(msgstrToComment=False, sync=True) cleanup_pofile(catalog_path)
def _update_message_catalog(self, catalog_path, is_pot=False): if not os.path.exists(os.path.dirname(catalog_path)): os.makedirs(os.path.dirname(catalog_path)) if not os.path.exists(catalog_path): open(catalog_path, 'w+').close() generator = getUtility(IWorkflowGenerator) translations = generator.get_translations(self.workflow_id, self.specification) catalog = MessageCatalog(filename=catalog_path) def is_delete_candidate(msg): # When our spec-path is in the references of the message, # we treat the message as delete candiate. if self.relative_specification_path in msg.references: # Remove the reference. It will be added again if we # still need the message. msg.references.remove(self.relative_specification_path) return True return False delete_candidates = map(attrgetter('msgid'), filter(is_delete_candidate, catalog.values())) for msgid, msgstr in translations.items(): msgid = msgid.decode('utf-8').replace('"', '\\"') msgstr = msgstr.decode('utf-8').replace('"', '\\"') if msgid in delete_candidates: delete_candidates.remove(msgid) if msgid in catalog: msg = catalog[msgid] msg.msgstr = msgstr if self.relative_specification_path not in msg.references: msg.references.append(self.relative_specification_path) msg.automatic_comments = filter( lambda text: not text.startswith('Default: '), msg.automatic_comments) msg.automatic_comments.append(u'Default: "{0}"'.format(msgstr)) else: catalog.add( msgid, msgstr=msgstr, references=[self.relative_specification_path], automatic_comments=[u'Default: "{0}"'.format(msgstr)]) if is_pot: catalog[msgid].msgstr = u'' for msgid in delete_candidates: if not catalog[msgid].references: del catalog[msgid] with open(catalog_path, 'w+') as catalog_file: writer = POWriter(catalog_file, catalog) writer.write(msgstrToComment=False, sync=True) cleanup_pofile(catalog_path)