def __init__(self, test_mode, langcode, htmlfile): self.langcode = langcode self.ietf_langcode = langcode_glib_to_ietf(langcode) self.pofile = os.path.join(translate_htmlconfig.get_sources_path(), PO_FOLDER, self.langcode + '.po') self.htmlfile = htmlfile self.test_mode = test_mode
def merge(self): """Does the actual merge operation and writes translated files to disk""" htmlfile_rel = self.htmlfile.replace( translate_htmlconfig.get_sources_path(), '..') with codecs.open(self.htmlfile, 'r', 'utf-8') as f: html_file = f.read() fname = os.path.basename(self.htmlfile) dirname = os.path.join(translate_htmlconfig.get_sources_path(), self.ietf_langcode) if not os.path.exists(dirname): os.makedirs(dirname) html_file_translated = os.path.join(dirname, fname) print >> sys.stderr, 'Translation written at:', \ html_file_translated with codecs.open(html_file_translated, 'w+', 'utf-8') as fd: po = polib.pofile(self.pofile) html_file_translated = html_file if self.test_mode: entry_list = po else: entry_list = po.translated_entries() for entry in entry_list: #FIXME: is this check too strict? (We're limiting merging # translations only if the original file listed in the # PO files source file comments exists) if (htmlfile_rel, '') in entry.occurrences: # Note that we preserve the leading and trailing space # to cater for words or sentences that have been split # and are part of a larger sentence. regex = re.compile(r'>(\s*)' + re.escape(entry.msgid) + r'(\s*)<') msgstr = self._mangle_po_entry(entry) replacement = r'>\g<1>' + msgstr + '\g<2><' html_file_translated = re.sub(regex, replacement, html_file_translated) html_file_translated = self.add_html_language( self.ietf_langcode, html_file_translated) fd.write(html_file_translated)
def _load_files(self): """Loads the files to extract strings from. They are expected to be listed in the POFILES.in file""" with open(translate_htmlconfig.get_source_file(PO_FOLDER, POTFILES)) as fp: file_list = [] for line in fp.readlines(): if not line.startswith("#"): line = os.path.join(translate_htmlconfig.get_sources_path(), line) file_list.append(line.strip()) return file_list
def _load_translations(self): """Loads the PO files to read translations from""" po_dir = os.path.join(translate_htmlconfig.get_sources_path(), PO_FOLDER) translations = [] for po_file in os.listdir(po_dir): fname, fext = os.path.splitext(po_file) if fext == '.po': translations.append(fname) return translations
def _load_files(self): """Gets the list of files to merge translations for""" with open(translate_htmlconfig.get_source_file(PO_FOLDER, POTFILES)) as fp: file_list = [] for line in fp.readlines(): if not line.startswith('#'): line = os.path.join( translate_htmlconfig.get_sources_path(), line) file_list.append(line.strip()) return file_list
def _load_files(self): """Loads the files to extract strings from. They are expected to be listed in the POFILES.in file""" with open(translate_htmlconfig.get_source_file(PO_FOLDER, POTFILES)) as fp: file_list = [] for line in fp.readlines(): if not line.startswith('#'): line = os.path.join( translate_htmlconfig.get_sources_path(), line) file_list.append(line.strip()) return file_list
def extract(self): htmlfile_rel = self.htmlfile.replace(translate_htmlconfig.get_sources_path(), "..") try: with codecs.open(self.htmlfile, "r", "utf-8") as fp: html_file = fp.read() extractor = HTMLStringParser() extractor.feed(html_file) extractor.close() messages = extractor.text() for message in messages: entry = polib.POEntry(occurrences=[(htmlfile_rel, 0)], msgid=message, msgstr=u"") self.potfile.append(entry) except: print_exc(file=stderr)
def extract(self): jsfile_rel = self.jsfile.replace(translate_htmlconfig.get_sources_path(), "..") with codecs.open(self.jsfile, "r", "utf-8") as fp: linecount = 0 for line in fp.readlines(): linecount += 1 if line.startswith("var"): var, message = line.split("=", 1) var = var.split()[1] message = message.strip() message = message[1:-2] entry = polib.POEntry(comment=var, occurrences=[(jsfile_rel, linecount)], msgid=message, msgstr=u"") self.potfile.append(entry)
def extract(self): htmlfile_rel = self.htmlfile.replace( translate_htmlconfig.get_sources_path(), '..') try: with codecs.open(self.htmlfile, 'r', 'utf-8') as fp: html_file = fp.read() extractor = HTMLStringParser() extractor.feed(html_file) extractor.close() messages = extractor.text() for message in messages: entry = polib.POEntry(occurrences=[(htmlfile_rel, 0)], msgid=message, msgstr=u'') self.potfile.append(entry) except: print_exc(file=stderr)
def extract(self): jsfile_rel = self.jsfile.replace( translate_htmlconfig.get_sources_path(), '..') with codecs.open(self.jsfile, 'r', 'utf-8') as fp: linecount = 0 for line in fp.readlines(): linecount += 1 if line.startswith('var'): var, message = line.split('=', 1) var = var.split()[1] message = message.strip() message = message[1:-2] entry = polib.POEntry(comment=var, occurrences=[(jsfile_rel, linecount) ], msgid=message, msgstr=u'') self.potfile.append(entry)
def _save_potfile(self): """Writes the resulting POT file to disk""" self.potfile.save(os.path.join( translate_htmlconfig.get_sources_path(), PO_FOLDER, GETTEXT_DOMAIN + '.pot'))
def _save_potfile(self): """Writes the resulting POT file to disk""" self.potfile.save( os.path.join(translate_htmlconfig.get_sources_path(), PO_FOLDER, GETTEXT_DOMAIN + '.pot'))