How to use the polib.pofile function in polib

To help you get started, we’ve selected a few polib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github django / django-localflavor / scripts / port.py View on Github external
locale_locale_po_file = join(country_locale_subdir,
                                         'LC_MESSAGES', 'django.po')
            # get locale to update from dir name
            _, subdir_locale = split(country_locale_subdir)

            # build path to new destination locale po file, create dirs
            locale_subdir = join(locale_dir, subdir_locale, 'LC_MESSAGES')
            dest_po_path = join(locale_subdir, 'django.po')
            mkdir_p(locale_subdir)

            print 'merging', locale_locale_po_file, 'into', dest_po_path

            # either load existing file and add the entries of the current source file
            if exists(dest_po_path):
                dest_po = polib.pofile(dest_po_path, encoding='utf-8', check_for_duplicates=True)
                src_po = polib.pofile(locale_locale_po_file, encoding='utf-8')
                merge(src_po, dest_po)
            # or just load the source file directly
            else:
                dest_po = polib.pofile(locale_locale_po_file, encoding='utf-8')

            # update the project-id-version to be consistent
            dest_po.metadata['Project-Id-Version'] = 'django-localflavor'
            # save new po file
            dest_po.save(dest_po_path)
            # and compile it
            dest_po.save_as_mofile(join(locale_subdir, 'django.mo'))
            # print 'percent translated for', dest_po_path, po.percent_translated()

        # throw away the old locale subdir
        if exists(country_locale_dir):
github mozilla / kitsune / scripts / l10n_completion.py View on Github external
def get_completion_data_for_file(fn):
    """Parses .po file and returns completion data for that .po file

    :returns: dict with keys total, translated and percent

    """
    app_to_translations = {}

    lang = get_language(fn)

    try:
        pofile = polib.pofile(fn)
    except IOError as ioe:
        print 'Error opening file: {fn}'.format(fn=fn)
        print ioe.message
        return 1

    for poentry in pofile:
        if poentry.obsolete:
            continue

        for occ in poentry.occurrences:
            path = occ[0].split(os.sep)
            if path[0] == 'kitsune':
                path = path[1]
            else:
                path = 'vendor/' + path[2]
            app_to_translations.setdefault(
github atareao / pomodoro-indicator / setup.py View on Github external
def get_entry(filein, msgid):
    try:
        po = polib.pofile(filein)
        print(po.metadata['Content-Type'])
        for entry in po:
            if entry.msgid == msgid:
                return entry.msgstr
    except Exception as e:
        print(filein, e)
    return None
github phpmyadmin / phpmyadmin / scripts / mergepo.py View on Github external
#!/usr/bin/python

import polib
import sys

po = polib.pofile(sys.argv[1])
poupdate = polib.pofile(sys.argv[2])

for origentry in po.fuzzy_entries():
    for updateentry in poupdate.translated_entries():
        if origentry.msgctxt is None and origentry.msgid == updateentry.msgid:
            origentry.msgstr = updateentry.msgstr
            origentry.flags.remove('fuzzy')
            break
        if origentry.msgctxt == updateentry.msgctxt and origentry.msgid == updateentry.msgid:
            origentry.msgstr = updateentry.msgstr
            origentry.flags.remove('fuzzy')
            break
for origentry in po.untranslated_entries():
    for updateentry in poupdate.translated_entries():
        if origentry.msgctxt is None and origentry.msgid == updateentry.msgid:
            origentry.msgstr = updateentry.msgstr
github aromanovich / carcade / carcade / i18n.py View on Github external
def get_translations(po_file_path):
    """Creates :class:`gettext.GNUTranslations` from PO file `po_file_path`."""
    po_file = polib.pofile(po_file_path)
    with tempfile.NamedTemporaryFile() as mo_file:
        po_file.save_as_mofile(mo_file.name)
        return gettext.GNUTranslations(mo_file)
github singularity / singularity / utils / data-translations.py View on Github external
#
msgid ""
msgstr ""
"Project-Id-Version: singularity 1\\n"
"Report-Msgid-Bugs-To: \\n"
"POT-Creation-Date: 2019-08-21 09:52+0200\\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
"Last-Translator: FULL NAME \\n"
"Language-Team: LANGUAGE \\n"
"Language: \\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=CHARSET\\n"
"Content-Transfer-Encoding: 8bit\\n"
""")

    po = polib.pofile(output_file)
    for text, ctxt, comment in po_entries:
        entry = po.find(text, msgctxt=ctxt)

        if not entry:
            entry = polib.POEntry()
            po.append(entry)

        entry.msgid = text
        entry.msgctxt = ctxt
        entry.msgstr = ""
        entry.comment = comment

    po.save(output_file)
github hsoft / moneyguru / hscommon / loc.py View on Github external
def merge_pots_into_pos(folder):
    # We're going to take all pot files in `folder` and for each lang, merge it with the po file
    # with the same name.
    potfiles = files_with_ext(folder, '.pot')
    for potfile in potfiles:
        refpot = polib.pofile(potfile)
        refname = op.splitext(op.basename(potfile))[0]
        for lang in get_langs(folder):
            po = polib.pofile(op.join(folder, lang, LC_MESSAGES, refname + '.po'))
            po.merge(refpot)
            po.save()
github hsoft / moneyguru / hscommon / loc.py View on Github external
def strings2pot(target, dest):
    with open(target, 'rt', encoding='utf-8') as fp:
        contents = fp.read()
    # We're reading an en.lproj file. We only care about the righthand part of the translation.
    re_trans = re.compile(r'".*" = "(.*)";')
    strings = re_trans.findall(contents)
    if op.exists(dest):
        po = polib.pofile(dest)
    else:
        po = polib.POFile()
    for s in dedupe(strings):
        s = unescape_cocoa_strings(s)
        entry = po.find(s)
        if entry is None:
            entry = polib.POEntry(msgid=s)
            po.append(entry)
        # we don't know or care about a line number so we put 0
        entry.occurrences.append((target, '0'))
        entry.occurrences = dedupe(entry.occurrences)
    po.save(dest)
github distrochooser / distrochooser / backend / distrochooser / constants.py View on Github external
def parseTranslation(langCode: str, poFile: str) -> dict:
  if langCode not in LOCALES:
    raise Exception("Language not installed")
  po = polib.pofile(poFile)
  result = {}
  for entry in po:
    result[entry.msgid] = entry.msgstr
  return result
github burke-software / schooldriver / api / translations / views.py View on Github external
def list(self, request):
        lang = self.request.QUERY_PARAMS.get('lang', None)
        if lang is not None:
            try:
                po_file_path = "ecwsp/admissions/locale/%s/LC_MESSAGES/django.po" % lang
                po = polib.pofile(po_file_path)
                data = {}
                for entry in po:
                    if entry.msgstr != '':
                        # only populate data with non-blank translations,
                        # this will help our angular app negotiate missing
                        # translations by detecting missing keys
                        data[entry.msgid] = entry.msgstr
                return Response(data)
            except Exception as e:
                return Response({"error": str(e)}, 500)
        else:
            return Response(
                {"error": "need to specify a language with ?lang="}, 400
                )