def get_label_list(po_file_path): listitems = [] try: po_file_content = codecs.open(po_file_path, "r", "utf-8").read() po = polib.pofile(po_file_content) for entry in po: string = {"id": entry.msgctxt, "line": entry.linenum, "comment": entry.comment, "tcomment": entry.tcomment, "string": entry.msgid, "file": po_file_path, "native_string": entry.msgstr} listitems.append(string) return listitems except Exception as e: answer = yesno_dialog("Error in %s:\n %s" % (po_file_path, str(e)), "Open") if answer: import sublime line = re.search(r"\(line ([0-9]+?)\)", str(e)) if line: sublime.active_window().open_file("%s:%s" % (po_file_path, int(line.group(1))), sublime.ENCODED_POSITION) else: sublime.active_window().open_file(po_file_path) return []
def get_translation_dict(): files = os.listdir(PATH_TO_PO) po_files = [] for addon_file in files: if addon_file.find(".po") != -1: po_files.append(addon_file) multilang_dict = {} for po_file in po_files: po = polib.pofile(os.path.join(PATH_TO_PO, po_file)) lang = os.path.splitext(po_file)[0] for entry in po: if entry.msgid in multilang_dict: multilang_dict[entry.msgid]["msgctxt"].append((lang, entry.msgstr)) else: if entry.msgctxt: context = entry.msgctxt else: context = "*" multilang_dict[entry.msgid] = { "ctxt" : context, "msgctxt" : [(lang, entry.msgstr)] } translations_dict = {} for ident in multilang_dict: key = (multilang_dict[ident]["ctxt"], ident) for trans in multilang_dict[ident]["msgctxt"]: if trans[1]: translations_dict.setdefault(trans[0], {})[key] = trans[1] return translations_dict
def updateTranslations(self, namespace): self._console.debug("Updating namespace: %s" % namespace) self._console.indent() self._console.debug("Looking up relevant class files...") content = [] classes = self._classes for classId in classes: if classes[classId]["namespace"] == namespace: content.append(classId) self._console.debug("Compiling filter...") pot = self.getPotFile(content) pot.sort() self._console.debug("Updating translations...") self._console.indent() files = self._translation[namespace] for name in files: self._console.debug("Processing: %s" % name) entry = files[name] po = polib.pofile(entry["path"]) po.merge(pot) po.sort() po.save(entry["path"]) self._console.outdent() self._console.outdent()
def translationsFromPofiles(pofiles, pot, statsObj=None): for path in pofiles: self._console.debug("Reading file: %s" % path) # .po files are only read-accessed cacheId = "pofile-%s" % path po, _ = self._cache.read(cacheId, path, memory=True) if po == None: po = polib.pofile(path) self._cache.write(cacheId, po, memory=True) extractTranslations(pot, po, statsObj) return pot
def get_translation_dict(): translations_dict = {} lang = "ru_RU" if os.path.isfile(PATH_TO_PO): po = polib.pofile(PATH_TO_PO) for entry in po: if entry.msgctxt: ctxt = entry.msgctxt else: ctxt = "*" key = (ctxt, entry.msgid) translations_dict.setdefault(lang, {})[key] = entry.msgstr return translations_dict
def generatePackageData(self, content, variants, locales): # Generate POT file to filter PO files self._console.debug("Compiling filter...") pot = self.getPotFile(content, variants) if len(pot) == 0: return {} # Find all influenced namespaces namespaces = {} for classId in content: ns = self._classes[classId]["namespace"] namespaces[ns] = True # Create a map of locale => files data = {} for namespace in namespaces: files = self._translation[namespace] for entry in locales: if files.has_key(entry): if not data.has_key(entry): data[entry] = [] data[entry].append(files[entry]["path"]) # Load po files blocks = {} for entry in data: self._console.debug("Processing translation: %s" % entry) self._console.indent() result = {} for path in data[entry]: self._console.debug("Reading file: %s" % path) self._console.indent() po = polib.pofile(path) po.merge(pot) translated = po.translated_entries() percent = po.percent_translated() self._console.debug("%s translated entries (%s%%)" % (len(translated), percent)) self._console.outdent() result.update(self.entriesToDict(translated)) self._console.debug("Formatting %s entries" % len(result)) blocks[entry] = self.msgfmt(result) self._console.outdent() return blocks
def getTranslationData(self, classList, variants, targetLocales): # Generate POT file to filter PO files self._console.debug("Compiling filter...") pot = self.getPotFile(classList, variants) if len(pot) == 0: return {} # Find all influenced namespaces libnames = {} for classId in classList: ns = self._classes[classId]["namespace"] libnames[ns] = True # Create a map of locale => [pofiles] PoFiles = {} for libname in libnames: liblocales = self._translation[libname] # {"en": <translationEntry>, ...} for locale in targetLocales: if locale in liblocales: if not locale in PoFiles: PoFiles[locale] = [] PoFiles[locale].append(liblocales[locale]["path"]) # collect all .po files for a given locale # Load po files blocks = {} for locale in PoFiles: self._console.debug("Processing translation: %s" % locale) self._console.indent() result = {} for path in PoFiles[locale]: self._console.debug("Reading file: %s" % path) po = polib.pofile(path) po.merge(pot) translated = po.translated_entries() result.update(self.entriesToDict(translated)) self._console.debug("Formatting %s entries" % len(result)) blocks[locale] = result self._console.outdent() return blocks
def updateTranslations(self, namespace, translationDir, localesList=None): self._console.info("Updating namespace: %s" % namespace) self._console.indent() self._console.debug("Looking up relevant class files...") classList = [] classes = self._classes for classId in classes: if classes[classId]["namespace"] == namespace: classList.append(classId) self._console.debug("Compiling filter...") pot = self.getPotFile(classList) pot.sort() allfiles = self._translation[namespace] if localesList == None: filenames = allfiles.keys() else: filenames = localesList for name in filenames: if name not in allfiles: path = os.path.join(translationDir, name + ".po") f = open(path, 'w') # create stanza file pof = self.createPoFile() f.write(str(pof)) f.close() allfiles[name] = LibraryPath.translationEntry(name, path, namespace) self._console.info("Updating %d translations..." % len(filenames)) self._console.indent() for name in filenames: self._console.debug("Processing: %s" % name) entry = allfiles[name] po = polib.pofile(entry["path"]) po.merge(pot) po.sort() po.save(entry["path"]) self._console.outdent() self._console.outdent()
def updatePoFiles(self, namespace, content): self._console.debug("Generating pot file...") pot = self.getPotFile(content) pot.sort() self._console.debug("Process po files...") self._console.indent() files = self._translation[namespace] for name in files: self._console.debug("Updating: %s" % name) entry = files[name] po = polib.pofile(entry["path"]) po.merge(pot) po.sort() po.save(entry["path"]) self._console.outdent()
def updateTranslations(self, namespace, translationDir, localesList=None): def parsePOEntryStrings(poset): for poentry in poset: poentry.msgid = self.parseAsUnicodeString(poentry.msgid) poentry.msgid_plural = self.parseAsUnicodeString(poentry.msgid_plural) if poentry.msgstr_plural: for pos in poentry.msgstr_plural: poentry.msgstr_plural[pos] = self.parseAsUnicodeString(poentry.msgstr_plural[pos]) def unescapeMsgIds(poset): for poentry in poset: if poentry.msgid.find(r'\\') > -1: poentry.msgid = self.recoverBackslashEscapes(poentry.msgid) # ---------------------------------------------------------------------- self._console.info("Updating namespace: %s" % namespace) self._console.indent() self._console.debug("Looking up relevant class files...") classList = [] classes = self._classesObj for classId in classes: if classes[classId].library.namespace == namespace: classList.append(classId) self._console.debug("Compiling filter...") pot = self.getPotFile(classList) # pot: translation keys from the source code pot.sort() allLocales = self._translation[namespace] if localesList == None: selectedLocales = allLocales.keys() else: selectedLocales = localesList for locale in selectedLocales: if locale not in allLocales: path = os.path.join(translationDir, locale + ".po") f = open(path, 'w') # create stanza file pof = self.createPoFile() f.write(str(pof)) f.close() allLocales[locale] = Library.translationEntry(locale, path, namespace) self._console.info("Updating %d translations..." % len(selectedLocales)) self._console.indent() for locale in selectedLocales: self._console.debug("Processing: %s" % locale) self._console.indent() entry = allLocales[locale] po = polib.pofile(entry["path"]) # po: .po file from disk po.merge(pot) po.sort() self._console.debug("Percent translated: %d" % (po.percent_translated(),)) #po.save(entry["path"]) poString = str(po) #poString = self.recoverBackslashEscapes(poString) filetool.save(entry["path"], poString) self._console.outdent() self._console.outdent() self._console.outdent()
def getTranslationData(self, classList, variants, targetLocales, addUntranslatedEntries=False): def extractTranslations(pot, po): po.getIdIndex() for potentry in pot: #otherentry = po.find(potentry.msgid) # this is slower on average than my own functions (bec. 'getattr') otherentry = po.indexFind(potentry.msgid) if otherentry: potentry.msgstr = otherentry.msgstr #potentry.msgid_plural remains if otherentry.msgstr_plural: for pos in otherentry.msgstr_plural: potentry.msgstr_plural[pos] = otherentry.msgstr_plural[pos] return # ------------------------------------------------------------------------- # Find all influenced namespaces libnames = {} for classId in classList: ns = self._classes[classId]["namespace"] libnames[ns] = True # Create a map of locale => [pofiles] PoFiles = {} for libname in libnames: liblocales = self._translation[libname] # {"en": <translationEntry>, ...} for locale in targetLocales: if locale in liblocales: if not locale in PoFiles: PoFiles[locale] = [] PoFiles[locale].append(liblocales[locale]["path"]) # collect all .po files for a given locale across libraries # Load po files and process their content blocks = {} mainpot = self.getPotFile(classList, variants) # pot file for this package mainpotS = pickle.dumps(mainpot) # loop through locales for locale in PoFiles: # ---------------------------------------------------------------------- # Generate POT file to filter PO files self._console.debug("Compiling filter...") # need a fresh pot, as it will be modified pot = pickle.loads(mainpotS) # copy.deepcopy(mainpot) chokes on overridden Array.append if len(pot) == 0: return {} self._console.debug("Processing translation: %s" % locale) self._console.indent() result = {} # loop through .po files for path in PoFiles[locale]: self._console.debug("Reading file: %s" % path) # .po files are only read-accessed cacheId = "pofile-%s" % path po, _ = self._cache.read(cacheId, path, memory=True) if po == None: po = polib.pofile(path) self._cache.write(cacheId, po, memory=True) extractTranslations(pot, po) poentries = pot.translated_entries() if addUntranslatedEntries: poentries.extend(pot.untranslated_entries()) result.update(self.entriesToDict(poentries)) self._console.debug("Formatting %s entries" % len(result)) blocks[locale] = result self._console.outdent() return blocks
def getTranslationData(self, classList, variants, targetLocales, addUntranslatedEntries=False): def extractTranslations(pot, po): po.getIdIndex() for potentry in pot: #otherentry = po.find(potentry.msgid) # this is slower on average than my own functions (bec. 'getattr') otherentry = po.indexFind(potentry.msgid) if otherentry: potentry.msgstr = otherentry.msgstr #potentry.msgid_plural remains if otherentry.msgstr_plural: for pos in otherentry.msgstr_plural: potentry.msgstr_plural[ pos] = otherentry.msgstr_plural[pos] return # ------------------------------------------------------------------------- # Find all influenced namespaces libnames = {} for classId in classList: ns = self._classes[classId]["namespace"] libnames[ns] = True # Create a map of locale => [pofiles] PoFiles = {} for libname in libnames: liblocales = self._translation[ libname] # {"en": <translationEntry>, ...} for locale in targetLocales: if locale in liblocales: if not locale in PoFiles: PoFiles[locale] = [] PoFiles[locale].append( liblocales[locale]["path"] ) # collect all .po files for a given locale across libraries # Load po files and process their content blocks = {} mainpot = self.getPotFile(classList, variants) # pot file for this package mainpotS = pickle.dumps(mainpot) # loop through locales for locale in PoFiles: # ---------------------------------------------------------------------- # Generate POT file to filter PO files self._console.debug("Compiling filter...") # need a fresh pot, as it will be modified pot = pickle.loads( mainpotS ) # copy.deepcopy(mainpot) chokes on overridden Array.append if len(pot) == 0: return {} self._console.debug("Processing translation: %s" % locale) self._console.indent() result = {} # loop through .po files for path in PoFiles[locale]: self._console.debug("Reading file: %s" % path) # .po files are only read-accessed cacheId = "pofile-%s" % path po, _ = self._cache.read(cacheId, path, memory=True) if po == None: po = polib.pofile(path) self._cache.write(cacheId, po, memory=True) extractTranslations(pot, po) poentries = pot.translated_entries() if addUntranslatedEntries: poentries.extend(pot.untranslated_entries()) result.update(self.entriesToDict(poentries)) self._console.debug("Formatting %s entries" % len(result)) blocks[locale] = result self._console.outdent() return blocks
def getTranslationData_1(self, classList, variants, targetLocales): def extractTranslations(pot,po): po.getIdIndex() for potentry in pot: #otherentry = po.find(potentry.msgid) # this is slower on average than my own functions (bec. 'getattr') otherentry = po.indexFind(potentry.msgid) if otherentry: potentry.msgstr = otherentry.msgstr #potentry.msgid_plural remains if otherentry.msgstr_plural: for pos in otherentry.msgstr_plural: potentry.msgstr_plural[pos] = otherentry.msgstr_plural[pos] return # Find all influenced namespaces libnames = {} for classId in classList: ns = self._classes[classId]["namespace"] libnames[ns] = True # Create a map of locale => [pofiles] PoFiles = {} for libname in libnames: liblocales = self._translation[libname] # {"en": <translationEntry>, ...} for locale in targetLocales: if locale in liblocales: if not locale in PoFiles: PoFiles[locale] = [] PoFiles[locale].append(liblocales[locale]["path"]) # collect all .po files for a given locale across libraries # Load po files and process their content blocks = {} # loop through locales for locale in PoFiles: # ---------------------------------------------------------------------- # Generate POT file to filter PO files self._console.debug("Compiling filter...") pot = self.getPotFile(classList, variants) # pot file for this package if len(pot) == 0: return {} self._console.debug("Processing translation: %s" % locale) self._console.indent() result = {} # loop through .po files for path in PoFiles[locale]: self._console.debug("Reading file: %s" % path) po = polib.pofile(path) extractTranslations(pot,po) translated = pot.translated_entries() result.update(self.entriesToDict(translated)) self._console.debug("Formatting %s entries" % len(result)) blocks[locale] = result self._console.outdent() return blocks