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 ralphje / django-internationalflavor / scripts / mergemessages.py View on Github external
languages = settings.LANGUAGES

        for lc, language in languages:
            try:
                self.stdout.write("Parsing language %s [%s]" % (language, lc))

                # Get some files ready
                # The pofile is our combined file
                pofile = polib.pofile(os.path.join(LOCALE_PATH, lc, 'LC_MESSAGES', 'django.po'))
                # The cldrfile contain only messages from CLDR
                cldrfile = polib.pofile(os.path.join(LOCALE_PATH, lc, 'LC_MESSAGES', 'cldr.po'))
                # The djangofile will only contain messages not from CLDR
                try:
                    djangofile = polib.pofile(os.path.join(LOCALE_PATH, lc, 'LC_MESSAGES', 'django_only.po'))
                except IOError:
                    djangofile = polib.POFile()
                    djangofile.metadata = pofile.metadata
                    djangofile.header = pofile.header

                # Merge all non-django messages to the djangofile
                django_only_messages = polib.POFile()
                for entry in pofile:
                    if cldrfile.find(entry.msgid) is None and not entry.obsolete and not 'fuzzy' in entry.flags:
                        django_only_messages.append(entry)
                djangofile.merge(django_only_messages)
                djangofile.save(os.path.join(LOCALE_PATH, lc, 'LC_MESSAGES', 'django_only.po'))

                # Add all entries from the CLDR file to the combined file
                for entry in cldrfile:
                    e = pofile.find(entry.msgid)
                    if e is None:
                        e = polib.POEntry()
github dolphin-emu / www / dolweb / docs / views.py View on Github external
def faq_dyni18n_po(request):
    po = polib.POFile()
    po.metadata = {
        'Project-Id-Version': '1.0',
        'Report-Msgid-Bugs-To': 'contact@dolphin-emu.org',
        'MIME-Version': '1.0',
        'Content-Type': 'text/plain; charset=utf-8',
        'Content-Transfer-Encoding': '8bit',
    }

    for cat in FAQCategory.objects.order_by('display_order'):
        po.append(polib.POEntry(msgid=cat.title, msgstr='', msgctxt='Category title'))
        for q in cat.sorted_questions():
            po.append(polib.POEntry(msgid=q.title, msgstr='', msgctxt='Question title'))
            po.append(polib.POEntry(msgid=q.short_title, msgstr='',
                                    msgctxt='Question short title (displayed in the left column)'))
            po.append(polib.POEntry(msgid=q.text, msgstr='', msgctxt='Answer'))
    return HttpResponse(str(po), content_type='text/plain')
github Softcatala / translation-memory-tools / src / builder / converttmx.py View on Github external
def convert(self):
        pofile = polib.POFile()

        pofile.metadata = {
            'Project-Id-Version': '1.0',
            'Report-Msgid-Bugs-To': 'none',
            'POT-Creation-Date': '2007-10-18 14:00+0100',
            'PO-Revision-Date': '2007-10-18 14:00+0100',
            'Last-Translator': 'none@none.org',
            'Language-Team': 'Catalan ',
            'MIME-Version': '1.0',
            'Content-Type': 'text/plain; charset=utf-8',
            'Content-Transfer-Encoding': '8bit',
            'Plural-Forms': 'nplurals=2; plural=n != 1;',
        }

        tree = ET.parse(self.input_file)
        root = tree.getroot()
github dimagi / commcare-hq / corehq / apps / translations / generators.py View on Github external
def generate_translation_files(self):
        for file_name in self.translations:
            sheet_translations = self.translations[file_name]
            po = polib.POFile()
            po.check_for_duplicates = False
            po.metadata = self.metadata
            for translation in sheet_translations:
                source = translation.key
                if source:
                    entry = polib.POEntry(
                        msgid=translation.key,
                        msgstr=translation.translation or '',
                        occurrences=translation.occurrences,
                        msgctxt=translation.msgctxt
                    )
                    po.append(entry)
            temp_file = tempfile.NamedTemporaryFile(delete=False)
            po.save(temp_file.name)
            self._generated_files.append(POFileInfo(file_name, temp_file.name))
        return self._generated_files
github canonical-webteam-archive / tour.ubuntu.com / translate-html / translate_html / StringExtractor.py View on Github external
def __init__(self):
        self.files = self._load_files()
        self.potfile = polib.POFile()
        time_str = datetime.now().isoformat(' ')
        self.potfile.metadata = {
            'Project-Id-Version': '1.0',
            'Report-Msgid-Bugs-To': 'you@example.com',
            'POT-Creation-Date': time_str,
            'PO-Revision-Date': time_str,
            'Last-Translator': 'you ',
            'Language-Team': 'English ',
            'MIME-Version': '1.0',
            'Content-Type': 'text/plain; charset=utf-8',
            'Content-Transfer-Encoding': '8bit',
        }
github aromanovich / carcade / carcade / i18n.py View on Github external
def extract_translations(jinja2_env, target_pot_file):
    """Produces a `target_pot_file` which contains a list of all
    the translatable strings extracted from the templates.
    """
    po = polib.POFile()
    po.metadata = {'Content-Type': 'text/plain; charset=utf-8'}

    messages = defaultdict(list)
    for template in jinja2_env.list_templates():
        template_source = get_template_source(jinja2_env, template)
        for (lineno, _, message) in jinja2_env.extract_translations(template_source):
            message = unicode(message)
            occurence = (template, lineno)
            messages[message].append(occurence)

    for message, occurrences in messages.iteritems():
        entry = polib.POEntry(msgid=message, msgstr=message, occurrences=occurrences)
        po.append(entry)

    po.save(target_pot_file)
github Automattic / wp-translate / gtautoml_po_eval.py View on Github external
prediction_client = automl_v1beta1.PredictionServiceClient()
name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
params = {}
def get_prediction(content):
    payload = {'text_snippet': {'content': content}}
    request = prediction_client.predict(name, payload, params)
    return request.payload[0].translation.translated_content.content

total = 0
correct = 0
untranslated = 0
cnt = 0

#Prep output files
out_po = polib.POFile()
out_po.metadata = po.metadata

dfh = open( diff_file, 'w' )

entry = polib.POEntry(
    msgid=u'Welcome',
    msgstr=u'Bienvenue',
)
# po.append(entry)

print('Translating...')

for entry in po:
    if(cnt > 10):
        break
    if ( cnt % 100 == 0 ):
github COEXCZ / django-translation-manager / translation_manager / manager.py View on Github external
if not os.path.isdir(lang_dir_path):
                os.mkdir(lang_dir_path)
                os.mkdir(os.path.join(lang_dir_path, 'LC_MESSAGES'))

            pofile_path = os.path.join(get_settings('TRANSLATIONS_BASE_DIR'), locale_path, get_dirname_from_lang(lang),
                                       'LC_MESSAGES',
                                               "%s.po" % domain)
            mofile_path = os.path.join(get_settings('TRANSLATIONS_BASE_DIR'), locale_path, get_dirname_from_lang(lang),
                                       'LC_MESSAGES',
                                               "%s.mo" % domain)

            if not os.path.exists(pofile_path):
                logger.debug("Po file '{}' does't exists, it will be created".format(pofile_path))

            now = datetime.now()
            pofile = polib.POFile()
            pofile.metadata = {
                'Project-Id-Version': '0.1',
                'Report-Msgid-Bugs-To': '%s' % settings.DEFAULT_FROM_EMAIL,
                'POT-Creation-Date': now.strftime("%Y-%m-%d %H:%M:%S"),
                'PO-Revision-Date': now.strftime("%Y-%m-%d %H:%M:%S"),
                'Last-Translator': 'Server <%s>' % settings.SERVER_EMAIL,
                'Language-Team': 'English <%s>' % settings.DEFAULT_FROM_EMAIL,
                'MIME-Version': '1.0',
                'Content-Type': 'text/plain; charset=utf-8',
                'Content-Transfer-Encoding': '8bit',
            }

            for translation in translations.filter(locale_path=locale_path, domain=domain):
                entry = polib.POEntry(
                    msgid=translation.original,
                    msgstr=translation.translation,