Exemplo n.º 1
0
def module_xml2po( module, po_file, language ):
	"""Create a PO file the XML definition of an UMC module"""
	message_po = '%s/messages.po' % ( os.path.dirname( po_file ) or '.' )

	po = polib.POFile()
	po.header = PO_HEADER
	po.metadata = copy.copy( PO_METADATA )
	po.metadata[ 'Project-Id-Version' ] = module.package
	po.metadata[ 'POT-Creation-Date' ] = formatdate( localtime = True )
	po.metadata[ 'Language' ] = language

	if module.xml_definition and os.path.isfile( module.xml_definition ):
		tree = ET.ElementTree( file = module.xml_definition )
		_appendPoEntry( po, tree.find( 'module/name' ) )
		_appendPoEntry( po, tree.find( 'module/description' ) )
		for flavor in tree.findall( 'module/flavor' ):
			_appendPoEntry( po, flavor.find( 'name' ) )
			_appendPoEntry( po, flavor.find( 'description' ) )

	if module.xml_categories and os.path.isfile( module.xml_categories ):
		tree = ET.ElementTree( file = module.xml_categories )
		for cat in tree.findall( 'categories/category' ):
			_appendPoEntry( po, cat.find( 'name' ) )

	po.save( message_po )
	if os.path.isfile( po_file ):
		dh_ucs.doIt( 'msgmerge', '--update', '--sort-output', po_file, message_po )
		if os.path.isfile( message_po ):
			os.unlink( message_po )
	else:
		dh_ucs.doIt( 'mv', message_po, po_file )
Exemplo n.º 2
0
def module_xml2po(module, po_file, language):
    # type: (UMC_Module, str, str) -> None
    """
	Create a PO file the |XML| definition of an |UMC| module.

	:param module: |UMC| module.
	:param po_file: File name of the textual message catalog.
	:param language: 2-letter language code.
	"""
    message_po = '%s/messages.po' % (os.path.dirname(po_file) or '.')

    po = polib.POFile(check_for_duplicates=True)
    po.header = PO_HEADER
    po.metadata = copy.copy(PO_METADATA)
    po.metadata['Project-Id-Version'] = module.package
    po.metadata['POT-Creation-Date'] = formatdate(localtime=True)
    po.metadata['Language'] = language

    def _append_po_entry(xml_entry):
        """Helper function to access text property of XML elements and to find the
		corresponding po-entry."""
        if xml_entry is not None and xml_entry.text is not None:  # important to use "xml_entry is not None"!
            entry = polib.POEntry(msgid=xml_entry.text, msgstr='')
            try:
                po.append(entry)
            except ValueError as exc:  # Entry "..." already exists
                print >> sys.stderr, 'Warning: Appending %r to po file failed: %s' % (
                    xml_entry.text, exc)

    if module.xml_definition and os.path.isfile(module.xml_definition):
        tree = ET.ElementTree(file=module.xml_definition)
        _append_po_entry(tree.find('module/name'))
        _append_po_entry(tree.find('module/description'))
        _append_po_entry(tree.find('module/keywords'))
        for flavor in tree.findall('module/flavor'):
            _append_po_entry(flavor.find('name'))
            _append_po_entry(flavor.find('description'))
            _append_po_entry(flavor.find('keywords'))
        _append_po_entry(tree.find('link/name'))
        _append_po_entry(tree.find('link/description'))
        _append_po_entry(tree.find('link/url'))

    if module.xml_categories and os.path.isfile(module.xml_categories):
        tree = ET.ElementTree(file=module.xml_categories)
        for cat in tree.findall('categories/category'):
            _append_po_entry(cat.find('name'))

    po.save(message_po)
    if os.path.isfile(po_file):
        try:
            if dh_ucs.doIt('msgmerge', '--update', '--sort-output', po_file,
                           message_po):
                raise Error('Failed to merge module translations into %s.' %
                            (po_file, ))
        finally:
            if os.path.isfile(message_po):
                os.unlink(message_po)
    else:
        dh_ucs.doIt('mv', message_po, po_file)
Exemplo n.º 3
0
def create_po_file(po_file, package, files, language='python'):
    # type: (str, str, Union[str, Iterable[str]], str) -> None
    """
	Create a PO file for a defined set of files.

	:param po_file: File name of the textual message catalog.
	:param package: Name of the package.
	:param files: A single file name or a list of file names.
	:param language: Programming language name.
	"""
    message_po = '%s/messages.pot' % (os.path.dirname(po_file) or '.')

    if os.path.isfile(message_po):
        os.unlink(message_po)
    if isinstance(files, basestring):
        files = [files]
    xgettext = dh_ucs.doIt('xgettext', '--force-po', '--add-comments=i18n',
                           '--from-code=UTF-8', '--sort-output',
                           '--package-name=%s' % package,
                           '[email protected]',
                           '--copyright-holder=Univention GmbH', '--language',
                           language, '-o', message_po, *files)
    if xgettext:
        raise Error('xgettext failed for the files: %r' % (list(files), ))

    po = polib.pofile(message_po)
    po.header = PO_HEADER
    po.metadata['Content-Type'] = 'text/plain; charset=UTF-8'
    if po.metadata_is_fuzzy:  # xgettext always creates fuzzy metadata
        try:
            po.metadata_is_fuzzy.remove('fuzzy')
        except ValueError:
            pass
    po.save()

    if os.path.isfile(po_file):
        try:
            if dh_ucs.doIt('msgmerge', '--update', '--sort-output', po_file,
                           message_po):
                raise Error('Failed to merge translations into %s.' %
                            (po_file, ))
        finally:
            if os.path.isfile(message_po):
                os.unlink(message_po)
    else:
        dh_ucs.doIt('mv', message_po, po_file)
Exemplo n.º 4
0
def create_mo_file(po_file):
    # type: (str) -> None
    """
	Compile textual message catalog (`.po`) to binary message catalog (`.mo`).

	:param po_file: File name of the textual message catalog.
	"""
    if dh_ucs.doIt('msgfmt', '--check', '--output-file',
                   po_file.replace('.po', '.mo'), po_file):
        raise Error('Failed to compile translation file from %s.' %
                    (po_file, ))
Exemplo n.º 5
0
def create_po_file( po_file, package, files, language = 'python' ):
	"""Create a PO file for a defined set of files"""
	message_po = '%s/messages.po' % ( os.path.dirname( po_file ) or '.' )

	if os.path.isfile( message_po ):
		os.unlink( message_po )
	if isinstance( files, basestring ):
		files = [ files ]
	dh_ucs.doIt( 'xgettext', '--force-po', '--from-code=UTF-8', '--sort-output', '--package-name=%s' % package, '[email protected]', '--copyright-holder=Univention GmbH', '--language', language, '-o', message_po, *files )
	po = polib.pofile( message_po )
	po.header = PO_HEADER
	po.metadata[ 'Content-Type' ] = 'text/plain; charset=UTF-8'
	po.save()
	if os.path.isfile( po_file ):
		dh_ucs.doIt( 'msgmerge', '--update', '--sort-output', po_file, message_po )
		if os.path.isfile( message_po ):
			os.unlink( message_po )
	else:
		dh_ucs.doIt( 'mv', message_po, po_file )
Exemplo n.º 6
0
def create_mo_file(po_file):
    """Compile textual message catalog to binary message catalog."""
    if dh_ucs.doIt('msgfmt', '--check', '--output-file',
                   po_file.replace('.po', '.mo'), po_file):
        raise Error('Failed to compile translation file from %s.' %
                    (po_file, ))
Exemplo n.º 7
0
def create_mo_file( po_file ):
	dh_ucs.doIt( 'msgfmt', '--check', '--output-file', po_file.replace( '.po', '.mo' ), po_file )