def test_markup(mofile): mo = polib.mofile(mofile) for entry in mo.translated_entries(): if is_markup(entry.msgid): # If this is a plural, check each of the plural translations if entry.msgid_plural: xlations = entry.msgstr_plural else: xlations = {None: entry.msgstr} for plural_id, msgstr in xlations.items(): # Check if the markup is valid at all try: # pylint: disable=unescaped-markup ET.fromstring('<markup>%s</markup>' % msgstr) except ET.ParseError: if entry.msgid_plural: raise AssertionError("Invalid markup translation for %d translation of msgid %s" % (plural_id, entry.msgid)) else: raise AssertionError("Invalid markup translation for msgid %s" % entry.msgid) # Check if the markup has the same number and kind of tags if not markup_match(entry.msgid, msgstr): if entry.msgid_plural: raise AssertionError("Markup does not match for %d translation of msgid %s" % (plural_id, entry.msgid)) else: raise AssertionError("Markup does not match for msgid %s" % entry.msgid)
def test_translated_messages(self, locale): message_dir = LOCALE_DIR / locale / 'LC_MESSAGES' for pofile_name in self.PO_FILES: pofile_path = message_dir / pofile_name pofile = polib.pofile(pofile_path) mofile = polib.mofile(pofile_path.stripext() + '.mo') po_entries = {entry.msgid: entry for entry in pofile.translated_entries()} mo_entries = {entry.msgid: entry for entry in mofile.translated_entries()} # Check that there are no entries in po that aren't in mo, and vice-versa self.assertEquals(po_entries.viewkeys(), mo_entries.viewkeys()) for entry_id, po_entry in po_entries.iteritems(): mo_entry = mo_entries[entry_id] for attr in ('msgstr', 'msgid_plural', 'msgstr_plural', 'msgctxt', 'obsolete', 'encoding'): po_attr = getattr(po_entry, attr) mo_attr = getattr(mo_entry, attr) # The msgstr_plural in the mo_file is keyed on ints, but in the po_file it's # keyed on strings. This normalizes them. if attr == 'msgstr_plural': po_attr = {int(key): val for (key, val) in po_attr.items()} self.assertEquals( po_attr, mo_attr, "When comparing {} for entry {!r}, {!r} from the .po file doesn't match {!r} from the .mo file".format( attr, entry_id, po_attr, mo_attr, ) )
def load( self, locale = None, domain = None ): """ Tries to load the translation file specified by the given locale and domain. If the given locale could not be found the method tries to find the translation domain for the systems default locale. No translation is provided when this fails too. :param str locale: the locale to provide :param str domain: the translation domain to use """ if locale is not None: self.locale = locale if domain is not None: self.domain = domain if self.locale is None or self.domain is None: LOCALE.info( 'Locale or domain missing. Stopped loading of translation' ) return LOCALE.info( 'Loading locale %s for domain %s' % ( self.locale, self.domain ) ) filename = os.path.join( I18N.LOCALE_DIR, self.locale.language, '%s.mo' % self.domain ) if not os.path.isfile( filename ): filename = os.path.join( I18N.LOCALE_DIR, '%s_%s' % ( self.locale.language, self.locale.territory ), '%s.mo' % self.domain ) if not os.path.isfile( filename ): LOCALE.warn( ' Could not find translation file' ) self.mofile = None return LOCALE.info( 'Found translation file %s' % filename ) self.mofile = polib.mofile( filename )
def test_mofile_with_subclass(self): """ Test that the mofile function correctly returns an instance of the passed in class """ class CustomMOFile(polib.MOFile): pass mofile = polib.mofile('tests/test_utf8.mo', klass=CustomMOFile) self.assertEqual(mofile.__class__, CustomMOFile)
def load_trans_mo(fn): '''Загрузка перевода из файла .mo с помощью библиотеки polib''' import polib result = {} mofile = polib.mofile(fn) for i in mofile: if i.msgid.startswith(": "): result[i.msgid[2:]] = i.msgstr[2:] result[i.msgid] = i.msgstr return result
def test_no_header(self): mo = polib.mofile('tests/test_no_header.mo') expected = u('''msgid "" msgstr "" msgid "bar" msgstr "rab" msgid "foo" msgstr "oof" ''') self.assertEqual(mo.__unicode__(), expected)
def test_markup(mofile): mo = polib.mofile(mofile) for entry in mo.translated_entries(): if is_markup(entry.msgid): # If this is a plural, check each of the plural translations if entry.msgid_plural: for plural_id, msgstr in entry.msgstr_plural.items(): if not markup_match(entry.msgid, msgstr): raise AssertionError( "Markup does not match for %d translation of msgid %s" % (plural_id, entry.msgid) ) elif not markup_match(entry.msgid, entry.msgstr): raise AssertionError("Markup does not match for msgid %s" % entry.msgid)
def _load_contents(self, polib, resource): """ Parses machine object (MO) format using polib @type resource: str @param resource: resource @rtype: list """ import struct try: return polib.mofile(resource) except (ValueError, AttributeError, struct.error) as e: self.rethrow( "Invalid resource {0}".format(resource), InvalidResourceException)
def test_msgctxt(self): #import pdb; pdb.set_trace() mo = polib.mofile('tests/test_msgctxt.mo') expected = u('''msgid "" msgstr "Content-Type: text/plain; charset=UTF-8\u005cn" msgctxt "Some message context" msgid "some string" msgstr "une cha\u00eene" msgctxt "Some other message context" msgid "singular" msgid_plural "plural" msgstr[0] "singulier" msgstr[1] "pluriel" ''') self.assertEqual(mo.__unicode__(), expected)
def load_files(self): self.clear() for root, subFolders, files in os.walk(os.getcwd(),topdown=False): for file in files: if self.type == MO_EXT: if file.endswith(MO_EXT): path, junk = os.path.split(root) path, locale = os.path.split(path) mo_inst = polib.mofile(os.path.join(root, file)) mo = Mo(mo_inst, locale, os.path.join(root, file)) self.check_file(mo) else: if file.endswith(PO_EXT): mo_inst = polib.pofile(os.path.join(root, file)) mo = Mo(mo_inst, file, os.path.join(root, file)) self.check_file(mo) self.progress.set_fraction(1.0)
def test_save_as_pofile(self): """ Test for the MOFile.save_as_pofile() method. """ fd, tmpfile = tempfile.mkstemp() os.close(fd) mo = polib.mofile('tests/test_utf8.mo', wrapwidth=78) mo.save_as_pofile(tmpfile) try: f = open(tmpfile) s1 = f.read() f.close() f = open('tests/test_save_as_pofile.po') s2 = f.read() f.close() self.assertEqual(s1, s2) finally: os.remove(tmpfile)
def _get_translations_dict(self): """Get translations dictionary and list of languages. It reads the translations from a predefined file and returns a dictionary with the translated strings and a list of the available languages. """ strings_dict = {} langs = [] for lang in os.listdir(LOCALES_DIR): mo_file_path = os.path.join(LOCALES_DIR, lang, 'LC_MESSAGES', 'eos-shell-content.mo') if not os.path.exists(mo_file_path): continue langs.append(lang) mo_file = polib.mofile(mo_file_path) strings_dict[lang] = {(entry.msgid, entry.msgctxt): entry.msgstr for entry in mo_file} return (strings_dict, langs)
def load_dictionary(fn): import polib polibfile = None name, ext = splitext(fn) if ext == ".po": polibfile = polib.pofile(fn) elif ext == ".mo": polibfile = polib.mofile(fn) else: raise ValueError("Неизвестный тип файла, необходимы файлы перевода po или mo") strings_dict = {} if polibfile != None: for i in polibfile: strings_dict[i.msgid.replace("\n", " ")] = i.msgstr.replace("\n", " ") return strings_dict
def test_translated_messages(self, locale): message_dir = LOCALE_DIR / locale / 'LC_MESSAGES' for pofile_name in self.PO_FILES: pofile_path = message_dir / pofile_name pofile = polib.pofile(pofile_path) mofile = polib.mofile(pofile_path.stripext() + '.mo') po_entries = { entry.msgid: entry for entry in pofile.translated_entries() } mo_entries = { entry.msgid: entry for entry in mofile.translated_entries() } # Check that there are no entries in po that aren't in mo, and vice-versa self.assertEquals(po_entries.viewkeys(), mo_entries.viewkeys()) for entry_id, po_entry in po_entries.iteritems(): mo_entry = mo_entries[entry_id] for attr in ('msgstr', 'msgid_plural', 'msgstr_plural', 'msgctxt', 'obsolete', 'encoding'): po_attr = getattr(po_entry, attr) mo_attr = getattr(mo_entry, attr) # The msgstr_plural in the mo_file is keyed on ints, but in the po_file it's # keyed on strings. This normalizes them. if attr == 'msgstr_plural': po_attr = { int(key): val for (key, val) in po_attr.items() } self.assertEquals( po_attr, mo_attr, "When comparing {} for entry {!r}, {!r} from the .po file doesn't match {!r} from the .mo file" .format( attr, entry_id, po_attr, mo_attr, ))
def test_msgctxt(self): # import pdb; pdb.set_trace() mo = polib.mofile("tests/test_msgctxt.mo") expected = """msgid "" msgstr "Content-Type: text/plain; charset=UTF-8\u005cn" msgctxt "Some message context" msgid "some string" msgstr "une cha\u00eene avec contexte" msgctxt "Some other message context" msgid "singular" msgid_plural "plural" msgstr[0] "singulier" msgstr[1] "pluriel" msgid "some string" msgstr "une cha\u00eene sans contexte" """ self.assertEqual(mo.__str__(), expected)
def __init__(self): self.keyfiles = [] for i in range(len(modules)): try: mod = modules[i].Module(None) keyfile = KeyFile(mod) self.keyfiles.append(keyfile) except: print "Failed to load module %s" % modules[i] import traceback traceback.print_exc() self.mo_files = {} if len(self.keyfiles) > 0: for root, subFolders, files in os.walk("/usr/share/cinnamon/locale"): for file in files: if file == "cinnamon.mo": path, junk = os.path.split(root) path, locale = os.path.split(path) self.mo_files[locale] = polib.mofile(os.path.join(root, file)) if len(self.mo_files) > 0: for locale in self.mo_files.keys(): for entry in self.mo_files[locale]: self.check_name(locale, entry) for locale in self.mo_files.keys(): for entry in self.mo_files[locale]: self.check_comment(locale, entry) for kf in self.keyfiles: action_path = os.path.join("files", "usr", "share", "applications", kf.kf_name) outstring, length = kf.kf.to_data() if os.path.exists(action_path): os.remove(action_path) outfile = open(action_path, 'w') outfile.write(outstring) outfile.close() print "Cinnamon settings desktop file generation complete."
def load_files(self): self.clear() for root, subFolders, files in os.walk(os.getcwd(), topdown=False): for file in files: if self.type == MO_EXT: if file.endswith(MO_EXT): path, junk = os.path.split(root) path, locale = os.path.split(path) mo_inst = polib.mofile(os.path.join(root, file)) mo = Mo(mo_inst, locale, os.path.join(root, file)) self.check_file(mo) else: if file.endswith(PO_EXT): locale = file.split("-")[-1].replace(".po", "") if locale in ["yi"]: # Don't check PO files for some of the locales (right-to-left languages for instance, or languages where it's hard for us to verify the arguments) continue mo_inst = polib.pofile(os.path.join(root, file)) mo = Mo(mo_inst, file, os.path.join(root, file)) self.check_file(mo) self.progress.set_fraction(1.0)
def load_files(self): self.clear() for root, subFolders, files in os.walk(os.getcwd(),topdown=False): for file in files: if self.type == MO_EXT: if file.endswith(MO_EXT): path, junk = os.path.split(root) path, locale = os.path.split(path) mo_inst = polib.mofile(os.path.join(root, file)) mo = Mo(mo_inst, locale, os.path.join(root, file)) self.check_file(mo) else: if file.endswith(PO_EXT): locale = file.split("-")[-1].replace(".po", "") if locale in ["yi"]: # Don't check PO files for some of the locales (right-to-left languages for instance, or languages where it's hard for us to verify the arguments) continue mo_inst = polib.pofile(os.path.join(root, file)) mo = Mo(mo_inst, file, os.path.join(root, file)) self.check_file(mo) self.progress.set_fraction(1.0)
def create_mofile_with_dummy_strings(filecontents, filename): logging.debug("Creating %s if it does not exist yet." % MO_FILE_LOCATION) ensure_dir(MO_FILE_LOCATION) # create the language metadata file. Needed for KA Lite to # actually detect the language barebones_metadata = { "code": TARGET_LANGUAGE_PACK, 'software_version': VERSION, 'language_pack_version': 1, 'percent_translated': 100, 'subtitle_count': 0, "name": "DEBUG", 'native_name': 'DEBUG', } logging.debug("Creating fake metadata json for %s." % TARGET_LANGUAGE_PACK) with open(TARGET_LANGUAGE_METADATA_PATH, "w") as f: json.dump(barebones_metadata, f) # Now create the actual MO files mo_file_path = os.path.join(MO_FILE_LOCATION, filename) po_file_path = "{po_path}.po".format( po_path=os.path.splitext(mo_file_path)[0]) logging.debug("Creating accented %s for %s." % (filename, TARGET_LANGUAGE_PACK)) with open(mo_file_path, "w") as f: f.write(filecontents) mofile = polib.mofile(mo_file_path) for moentry in mofile: accenting.convert_msg(moentry) mofile.save(fpath=mo_file_path) mofile.save_as_pofile(fpath=po_file_path) logging.debug("Finished creating %s for %s." % (filename, TARGET_LANGUAGE_PACK))
def load(self, locale=None, domain=None): """ Tries to load the translation file specified by the given locale and domain. If the given locale could not be found the method tries to find the translation domain for the systems default locale. No translation is provided when this fails too. :param str locale: the locale to provide :param str domain: the translation domain to use """ if locale is not None: self.locale = locale if domain is not None: self.domain = domain if self.locale is None or self.domain is None: LOCALE.info('Locale or domain missing. Stopped loading of translation') return LOCALE.info('Loading locale %s for domain %s' % (self.locale, self.domain)) filename = os.path.join(I18N.LOCALE_DIR, self.locale.language, '%s.mo' % self.domain) if not os.path.isfile(filename): filename = os.path.join(I18N.LOCALE_DIR, '%s_%s' % (self.locale.language, self.locale.territory), '%s.mo' % self.domain) if not os.path.isfile(filename): LOCALE.warn('Could not find translation file: %r' % (os.path.basename(filename),)) self.mofile = None return LOCALE.info('Found translation file %s' % (filename,)) self.mofile = None try: self.mofile = polib.mofile(filename) except (ValueError, MemoryError) as exc: LOCALE.error('Corrupt translation file %r: %s' % (filename, exc)) except (KeyboardInterrupt, SystemExit, SyntaxError): raise except Exception as exc: LOCALE.error('Corrupt translation file %r: %s' % (filename, exc)) LOCALE.error(traceback.format_exc())
def create_mofile_with_dummy_strings(filecontents, filename): logging.debug("Creating %s if it does not exist yet." % MO_FILE_LOCATION) ensure_dir(MO_FILE_LOCATION) # create the language metadata file. Needed for KA Lite to # actually detect the language barebones_metadata = { "code": TARGET_LANGUAGE_PACK, 'software_version': VERSION, 'language_pack_version': 1, 'percent_translated': 100, 'subtitle_count': 0, "name": "DEBUG", 'native_name': 'DEBUG', } logging.debug("Creating fake metadata json for %s." % TARGET_LANGUAGE_PACK) with open(TARGET_LANGUAGE_METADATA_PATH, "w") as f: json.dump(barebones_metadata, f) # Now create the actual MO files mo_file_path = os.path.join(MO_FILE_LOCATION, filename) po_file_path = "{po_path}.po".format(po_path=os.path.splitext(mo_file_path)[0]) logging.debug("Creating accented %s for %s." % (filename, TARGET_LANGUAGE_PACK)) with open(mo_file_path, "w") as f: f.write(filecontents) mofile = polib.mofile(mo_file_path) for moentry in mofile: accenting.convert_msg(moentry) mofile.save(fpath=mo_file_path) mofile.save_as_pofile(fpath=po_file_path) logging.debug("Finished creating %s for %s." % (filename, TARGET_LANGUAGE_PACK))
def load(self, locale=None, domain=None): """ Tries to load the translation file specified by the given locale and domain. If the given locale could not be found the method tries to find the translation domain for the systems default locale. No translation is provided when this fails too. :param str locale: the locale to provide :param str domain: the translation domain to use """ if locale is not None: self.locale = locale if domain is not None: self.domain = domain if self.locale is None or self.domain is None: LOCALE.info( 'Locale or domain missing. Stopped loading of translation') return LOCALE.info('Loading locale %s for domain %s' % (self.locale, self.domain)) filename = os.path.join(I18N.LOCALE_DIR, self.locale.language, '%s.mo' % self.domain) if not os.path.isfile(filename): filename = os.path.join( I18N.LOCALE_DIR, '%s_%s' % (self.locale.language, self.locale.territory), '%s.mo' % self.domain) if not os.path.isfile(filename): LOCALE.warn('Could not find translation file: %r' % (os.path.basename(filename), )) self.mofile = None return LOCALE.info('Found translation file %s' % filename) self.mofile = polib.mofile(filename)
def test_save_as_pofile(self): """ Test for the MOFile.save_as_pofile() method. """ fd, tmpfile = tempfile.mkstemp() os.close(fd) mo = polib.mofile('tests/test_utf8.mo', wrapwidth=78) mo.save_as_pofile(tmpfile) try: if polib.PY3: f = open(tmpfile, encoding='utf-8') else: f = open(tmpfile) s1 = f.read() f.close() if polib.PY3: f = open('tests/test_save_as_pofile.po', encoding='utf-8') else: f = open('tests/test_save_as_pofile.po') s2 = f.read() f.close() self.assertEqual(s1, s2) finally: os.remove(tmpfile)
def step_impl(context, locale): file = Path( os.path.join(settings.BASE_DIR, "locale", locale, "LC_MESSAGES", "django.mo")) polib.mofile(file) context.test.assertTrue(file.is_file())
if (os.path.isfile(path) == False or "global.mo" not in path): print("ERROR: Unable to find global.mo") time.sleep(5) sys.exit() # ask user: what should be replaced: print() replaceA = input( "Enter the name / string that should be replaced (e.g 'Yamato'): ") replaceB = input( "Enter the name / string that it should be replaced with (e.g 'Tomato'): ") # load the language file print("Working...") if (path == ""): mo_file = polib.mofile(mo_files[0]) else: mo_file = polib.mofile(path) # check line by line and replace text if present: replaced = 0 #print("-------------------------------------------------------------") for line in mo_file: line.msgid_with_context = line.msgid #somehow needed to save the file (not sure what it does) if (replaceA in line.msgstr): replaced += 1 line.msgstr = line.msgstr.replace(replaceA, replaceB) #print(line.msgstr) #print("-------------------------------------------------------------") # Save the file, and diplay how often the text was replaced
def test_pofile_and_mofile3(self): """ Test that the mofile function returns a MOFile instance. """ mo = polib.mofile('tests/test_utf8.mo') self.assertTrue(isinstance(mo, polib.MOFile))
args) if not cur.rowcount: cur.execute("insert into mofiles (package, path) values (%(package)s, %(path)s)", args) cur.execute("select id from mofiles where package = %(package)s and path = %(path)s", args) mofile_id = cur.fetchone()[0] path, mofile = os.path.split(mofile_path) path, lc_part = os.path.split(path) path, language = os.path.split(path) assert lc_part.startswith("LC_") language_id = insert_language(language) mofile = polib.mofile(mofile_path) charset = mofile.charset() for key, value in mofile.metadata.iteritems(): cur.execute("insert into mofile_metadatas (mofile, metadata, value) values (%(mofile)s, %(metadata)s, %(value)s)", {"mofile": mofile_id, "metadata": insert_metadata(key.decode(charset)), "value": value.decode(charset)}) for item in mofile: msgid = item.msgid.decode(charset) msgstr = item.msgstr.decode(charset) msgid_id = insert_msgid(msgid) msgstr_id = insert_msgstr(msgstr)
def test_invalid_version(self): self.assertRaises(polib.MOParseError, polib.mofile, "tests/test_invalid_version.mo") polib.mofile("tests/test_version_1.1.mo")
def ReturnMOObject(mofile): return polib.mofile(mofile)
def test_pofile_and_mofile3(self): """ Test that the mofile function returns a MOFile instance. """ mo = polib.mofile("tests/test_utf8.mo") self.assertTrue(isinstance(mo, polib.MOFile))
import polib ## MADE BY GAMBIE mo = polib.mofile('global.mo') mo.save_as_pofile('IGNORE.po') po = polib.pofile('IGNORE.po.') poNEW = polib.POFile() rename="TEST" USin = open("US.txt", "r",encoding='UTF-8') USSRin = open("USSR.txt", "r",encoding='UTF-8') UKin = open("UK.txt", "r",encoding='UTF-8') GERMin = open("KRIEG.txt", "r",encoding='UTF-8') PAin = open("AIS.txt", "r",encoding='UTF-8') IJNin = open("IJN.txt", "r",encoding='UTF-8') FRin = open("FR.txt", "r",encoding='UTF-8') PANAMin = open("PANUS.txt", "r",encoding='UTF-8') ITALin = open("ITAL.txt", "r",encoding='UTF-8') EUin = open("EU.txt", "r",encoding='UTF-8') COMin = open("COMM.txt", "r",encoding='UTF-8') ##
def run(directory): f = polib.mofile(directory) data = {} for line in f: data[line.msgid] = line.msgstr return data
def test_invalid_version(self): self.assertRaises(IOError, polib.mofile, 'tests/test_invalid_version.mo') polib.mofile('tests/test_version_1.1.mo')
def __init__(self, path): self.cache = {} for filename in os.listdir(path): self.cache[os.path.splitext(filename)[0]] = polib.mofile( os.path.join(path, filename))
# Define localization strings locale_re = re.compile(r''' Localize(?:dFormat)? # Methods begin with Localize or LocalizedFormat \s*\(\s*" # Possible whitespaces after method name ((?:[^"]|"")*) # Match anything but ", though do allow "" " # The search is over when a single " is found ''',re.VERBOSE|re.IGNORECASE) for language in os.listdir(locales_dir): # Load localizations print('Processing language %s' % language) mopo_objects = [] lang_dir = os.path.join(locales_dir,language)+os.path.sep if locale_filetype == 'mo': mopo_objects = [ polib.mofile(lang_dir+file) for file in \ [f for f in os.listdir(lang_dir) if f[-3:]=='.mo']] elif locale_filetype == 'po': mopo_objects = [ polib.pofile(lang_dir+file) for file in \ [f for f in os.listdir(lang_dir) if f[-3:]=='.po'] if file] # Move the project's locale file to the front, # so it has the highest priority mopo_file = project_name+'.'+locale_filetype if mopo_file in mopo_objects: mopo_objects = mopo_objects.insert(0, mopo_objects.pop(mopo_objects.index(mopo_file))) print('Building translation dictionary') translations = create_translation_dict(mopo_objects) for source_file_name in source_files: