示例#1
0
    def test_case1(self):
        """Test for newlines."""
        content = 'msgid "Add"\n' \
                  'msgstr "Ajouter\\n"\n'
        po = POFile()
        po.load_state_from_string(content)

        self.assertEqual(po.get_msgstr('Add'), u'Ajouter\n')
示例#2
0
    def test_case3(self):
        """Test for double quotes."""
        content = 'msgid "test"\n' \
                  'msgstr "Esto es una \\"prueba\\""\n'
        po = POFile()
        po.load_state_from_string(content)

        self.assertEqual(po.get_msgstr('test'), u'Esto es una "prueba"')
示例#3
0
    def test_case3(self):
        """Test for double quotes."""
        content = 'msgid "test"\n' \
                  'msgstr "Esto es una \\"prueba\\""\n'
        po = POFile()
        po.load_state_from_string(content)

        self.assertEqual(po.get_msgstr('test'), u'Esto es una "prueba"')
示例#4
0
    def test_case1(self):
        """Test for newlines."""
        content = 'msgid "Add"\n' \
                  'msgstr "Ajouter\\n"\n'
        po = POFile()
        po.load_state_from_string(content)

        self.assertEqual(po.get_msgstr('Add'), u'Ajouter\n')
示例#5
0
    def test_end_comment(self):
        """Test end comment."""
        content = '#, fuzzy\n' \
                  '#~ msgid "Hello"\n' \
                  '#~ msgstr "Hola"\n'
        po = POFile()
        po.load_state_from_string(content)

        translation = po.gettext(((TEXT, 'Hello'),))
        self.assertEqual(translation,((TEXT, 'Hello'),))
示例#6
0
    def test_end_comment(self):
        """Test end comment."""
        content = '#, fuzzy\n' \
                  '#~ msgid "Hello"\n' \
                  '#~ msgstr "Hola"\n'
        po = POFile()
        po.load_state_from_string(content)

        translation = po.gettext(((TEXT, 'Hello'), ))
        self.assertEqual(translation, ((TEXT, 'Hello'), ))
示例#7
0
    def test_case2(self):
        """Test for multiple lines."""
        content = 'msgid "Hello world"\n' \
                  'msgstr ""\n' \
                  '"Hola "\n' \
                  '"mundo"\n'
        po = POFile()
        po.load_state_from_string(content)

        self.assertEqual(po.get_msgstr('Hello world'), u'Hola mundo')
示例#8
0
    def test_case2(self):
        """Test for multiple lines."""
        content = 'msgid "Hello world"\n' \
                  'msgstr ""\n' \
                  '"Hola "\n' \
                  '"mundo"\n'
        po = POFile()
        po.load_state_from_string(content)

        self.assertEqual(po.get_msgstr('Hello world'), u'Hola mundo')
示例#9
0
    def test_output(self):
        """Test output."""
        content = '# Comment\n' \
                  '#: pouet.py:45\n' \
                  '#, fuzzy\n' \
                  'msgid "Hello"\n' \
                  'msgstr ""\n' \
                  '"Hola\\n"\n'
        po = POFile()
        po.load_state_from_string(content)

        self.assertEqual((po.get_units())[0].to_str(), content)
示例#10
0
    def test_output(self):
        """Test output."""
        content = '# Comment\n' \
                  '#: pouet.py:45\n' \
                  '#, fuzzy\n' \
                  'msgid "Hello"\n' \
                  'msgstr ""\n' \
                  '"Hola\\n"\n'
        po = POFile()
        po.load_state_from_string(content)

        self.assertEqual((po.get_units())[0].to_str(), content)
示例#11
0
    def test_fuzzy(self):
        """Test fuzzy."""
        content = '# Comment\n' \
                  '#: pouet.py:45\n' \
                  '#, fuzzy\n' \
                  'msgid "Hello"\n' \
                  'msgstr ""\n' \
                  '"Hola\\n"\n'
        po = POFile()
        po.load_state_from_string(content)

        translation = po.gettext(((TEXT, 'Hello'), ))
        self.assertEqual(translation, ((TEXT, 'Hello'), ))
示例#12
0
    def test_fuzzy(self):
        """Test fuzzy."""
        content = '# Comment\n' \
                  '#: pouet.py:45\n' \
                  '#, fuzzy\n' \
                  'msgid "Hello"\n' \
                  'msgstr ""\n' \
                  '"Hola\\n"\n'
        po = POFile()
        po.load_state_from_string(content)

        translation = po.gettext(((TEXT, 'Hello'),))
        self.assertEqual(translation, ((TEXT, 'Hello'),))
示例#13
0
    def po_import(self, lang, data):
        """ """
        messages = self._messages

        # Load the data
        po = POFile(string=data)
        for msgid in po.get_msgids():
            if msgid:
                msgstr = po.get_msgstr(msgid) or ""
                if not messages.has_key(msgid):
                    messages[msgid] = PersistentMapping()
                messages[msgid][lang] = msgstr

        # Set the encoding (the full header should be loaded XXX)
        self.update_po_header(lang, charset=po.get_encoding())
示例#14
0
    def test_translation(self):
        # Translate the document
        str = ('msgid "Hello <text:span text:style-name="T1">world</text:span>'
               ' !"\nmsgstr "Hola '
               '<text:span text:style-name="T1">mundo</text:span> !"\n')
        po = POFile(string=str)
        # Get the message of the translate document
        translated_doc = self.doc.translate(po)
        translated_doc = ODTFile(string=translated_doc)
        messages = [unit[0] for unit in translated_doc.get_units()]
        # Check if allright
        expected = [
            # content.xml
            ((TEXT, u'Hello '), (START_FORMAT, 1), (TEXT, u'world'),
             (END_FORMAT, 1), (TEXT, u' !')),
            # meta.xml
            (
                (TEXT, u'Hello world Document'), ),
            ((TEXT, u"it's a very good document"), ),
            ((TEXT, u'Itools test'), ),
            ((TEXT, u'itools'), ),
            ((TEXT, u'odt'), ),
            ((TEXT, u'odf'), )
        ]

        self.assertEqual(messages, expected)
示例#15
0
文件: ws_odf.py 项目: hforge/hforge
    def action_odf2tr(self, resource, context, form):
        odf_file_name, odf_file_mime_type, odf_file_data = form['odf_file']
        srx_file = form['srx_file']
        output_type = form['output_type']

        # Not a too big file
        if check_size(odf_file_data, context):
            return

        # Get the good "get_units"
        odf_handler = get_handler_class_by_mimetype(odf_file_mime_type)
        try:
            get_units = odf_handler(string=odf_file_data).get_units
        except AttributeError:
            context.message = ERROR(u'malformed ODF file')
            return

        # a SRX file ?
        if srx_file is not None:
            srx_file_data = srx_file[2]
            try:
                srx_handler = SRXFile(string=srx_file_data)
            except XMLError:
                context.message = ERROR(u'unexpected error, please verify '
                                        u'your input files')
                return
        else:
            srx_handler = None

        # The good handler for the output
        if output_type == 'PO':
            extension = 'po'
            out_handler = POFile()
        else:
            extension = 'xlf'
            out_handler = XLFFile()
        name = FileName.decode(odf_file_name)[0]
        out_filename = FileName.encode((name, extension, None))

        # Make the output
        for source, source_context, line in get_units(srx_handler=srx_handler):
            out_handler.add_unit(odf_file_name, source, source_context, line)

        # Return the result
        context.set_content_type(out_handler.class_mimetypes[0])
        context.set_content_disposition('inline', out_filename)
        return out_handler.to_str()
示例#16
0
    def test_case4(self):
        """Test translation of an element content"""
        doc = HTMLFile(string='<p>hello world</p>')

        p = POFile(string='msgctxt "paragraph"\n'
                   'msgid "hello world"\n'
                   'msgstr "hola mundo"')

        self.assertEqual(doc.translate(p), '<p>hola mundo</p>')
示例#17
0
    def test_translation1(self):
        """Test translation with surrounding tags"""

        doc = HTMLFile(string='<em>hello world</em>')

        p = POFile(string='msgctxt "emphasis"\n'
                   'msgid "hello world"\n'
                   'msgstr "hola mundo"')

        self.assertEqual(doc.translate(p), '<em>hola mundo</em>')
示例#18
0
    def test_case5(self):
        """Test translation of an element content"""
        po = POFile(string='msgctxt "img[alt]"\n'
                    'msgid "The beach"\n'
                    'msgstr "La playa"')
        xhtml = XHTMLFile(string=self.template %
                          '<img alt="The beach" src="beach.jpg" />')

        html = xhtml.translate(po)
        xhtml = XHTMLFile(string=html)

        messages = [unit[0] for unit in xhtml.get_units()]
        self.assertEqual(messages, [((TEXT, u'La playa'), )])
示例#19
0
    def test_case5(self):
        """Test translation of an element content"""
        doc = HTMLFile(string='<img alt="The beach" src="beach.jpg">')

        po = POFile(string='msgctxt "img[alt]"\n'
                    'msgid "The beach"\n'
                    'msgstr "La playa"')

        string = doc.translate(po)
        output = HTMLFile(string=string)

        expected = HTMLFile(string='<img alt="La playa" src="beach.jpg">')
        self.assertEqual(output, expected)
示例#20
0
    def test_case6(self):
        """Test translation of an element content"""
        doc = HTMLFile(string='<input type="text" name="id">'
                       '<input type="submit" value="Change">')

        p = POFile(string='msgctxt "button"\n'
                   'msgid "Change"\n'
                   'msgstr "Cambiar"')

        output = HTMLFile(string=doc.translate(p))

        expected = HTMLFile(string='<input type="text" name="id">'
                            '<input type="submit" value="Cambiar">')
        self.assertEqual(output.to_str(), expected.to_str())
示例#21
0
    def test_case4(self):
        """Test translation of an element content"""
        string = ('msgctxt "paragraph"\n'
                  'msgid "hello world"\n'
                  'msgstr "hola mundo"\n')
        p = POFile(string=string)

        string = self.template % '<p>hello world</p>'
        source = XHTMLFile(string=string)

        string = source.translate(p)
        xhtml = XHTMLFile(string=string)

        messages = [unit[0] for unit in xhtml.get_units()]
        self.assertEqual(messages, [((TEXT, u'hola mundo'), )])
示例#22
0
    def test_translation2(self):
        """Test translation with surrounding tags (2)"""

        doc = HTMLFile(string='Say: <em>hello world. It\'s me.</em>')

        p = POFile(string='msgid "Say:"\n'
                   'msgstr "Dice:"\n\n'
                   'msgctxt "emphasis"\n'
                   'msgid "hello world."\n'
                   'msgstr "hola mundo."\n\n'
                   'msgctxt "emphasis"\n'
                   'msgid "It\'s me."\n'
                   'msgstr "Es me."')

        self.assertEqual(doc.translate(p), 'Dice: <em>hola mundo. Es me.</em>')
示例#23
0
    def test_translation_paragraph(self):
        """Test translation of an element content"""
        po = POFile(string='msgctxt "paragraph"\n'
                    'msgid "hello world"\n'
                    'msgstr "hola mundo"\n')
        content = ('<office:text>'
                   '<text:p text:style-name="Standard">'
                   'hello world'
                   '</text:p>'
                   '</office:text>')

        content = odt_template % content
        messages = XMLParser(content)
        messages = translate(messages, po)
        messages = [unit[0] for unit in get_units(messages)]
        self.assertEqual(messages, [((TEXT, u'hola mundo'), )])
示例#24
0
文件: build.py 项目: nkhine/itools
def ipkg_build(worktree, manifest, config):
    from itools.html import XHTMLFile, HTMLFile
    package_root = config.get_value('package_root')
    rules = [('.po', '.mo', po2mo, None)]
    # Pre-load PO files
    po_files = {}
    for dst_lang in config.get_value('target_languages'):
        po = POFile('%s/locale/%s.po' % (package_root, dst_lang))
        po_files[dst_lang] = po
    # Templates
    src_lang = config.get_value('source_language', default='en')
    for dst_lang in config.get_value('target_languages'):
        rules.append(('.xml.%s' % src_lang, '.xml.%s' % dst_lang,
                      make_template, XHTMLFile))
        rules.append(('.xhtml.%s' % src_lang, '.xhtml.%s' % dst_lang,
                      make_template, XHTMLFile))
        rules.append(('.html.%s' % src_lang, '.html.%s' % dst_lang,
                      make_template, HTMLFile))
    # Make
    make(worktree, rules, manifest, package_root, po_files)
示例#25
0
文件: build.py 项目: Ramel/itools
def make_template(package_root, source, target, handler_cls):
    # Import some packages so we can compile templates
    from itools.xmlfile.errors import TranslationError
    import itools.gettext
    import itools.stl
    import itools.pdf
    # Get file
    source_handler = handler_cls(source)
    language = target.rsplit('.', 1)[1]
    po = POFile('%s/locale/%s.po' % (package_root, language))
    print source_handler, source, target
    try:
        data = source_handler.translate(po)
    except TranslationError as e:
        # Override source and language
        raise TranslationError(line=e.line,
                               source_file=source,
                               language=language)
    with open(target, 'w') as f:
        f.write(data)
示例#26
0
    def test_translation4(self):
        """Test translation with surrounding tags (4)"""

        doc = HTMLFile(string='Say: <em>   hello world. It\'s me.</em>'
                       '      Do you remember me ? ')

        p = POFile(string='msgid "Say:"\n'
                   'msgstr "Dites:"\n\n'
                   'msgctxt "emphasis"\n'
                   'msgid "hello world."\n'
                   'msgstr "Bonjour monde."\n\n'
                   'msgctxt "emphasis"\n'
                   'msgid "It\'s me."\n'
                   'msgstr "C\'est moi."\n\n'
                   'msgid "Do you remember me ?"\n'
                   'msgstr "Vous vous rappelez de moi ?"')

        self.assertEqual(
            doc.translate(p), 'Dites: '
            '<em>   Bonjour monde. C\'est moi.</em>'
            '      Vous vous rappelez de moi ? ')
示例#27
0
    options, args = parser.parse_args()
    if len(args) != 0:
        parser.error('incorrect number of arguments')

    # Read configuration for languages
    config = get_config()
    src_language = config.get_value('source_language', default='en')

    # The SRX file
    if options.srx is not None:
        srx_handler = ro_database.get_handler(options.srx)
    else:
        srx_handler = None

    # Initialize message catalog
    po = POFile()
    lines = []
    for line in open('MANIFEST').readlines():
        line = line.strip()
        if line.split(sep)[0] not in ('archive', 'docs', 'skeleton', 'test'):
            lines.append(line)

    # Process Python and HTML files
    write('* Extract text strings')
    extensions = ['.py', '.xhtml.%s' % src_language, '.xml.%s' % src_language]

    for path in lines:
        # Filter files
        for extension in extensions:
            if path.endswith(extension):
                break
示例#28
0
def update_locale(srx_handler, exclude_folders, no_wrap=False):
    # Read configuration for languages
    config = get_config()
    src_language = config.get_value('source_language', default='en')

    # Get local folder
    package_root = config.get_value('package_root')
    if lfs.exists(package_root):
        locale_folder_path = Path('{0}/locale'.format(package_root))
    else:
        locale_folder_path = Path('locale/')
    locale_folder = lfs.open(locale_folder_path)

    # Initialize message catalog
    po = POFile()
    lines = []
    for line in open('MANIFEST').readlines():
        line = line.strip()
        exclude_folder = False
        for x in exclude_folders:
            if line.startswith(x):
                exclude_folder = True
                break
        if exclude_folder is False:
            lines.append(line)

    # Process Python and HTML files
    write('* Extract text strings')
    extensions = [
        '.py', '.js',
        '.xhtml.%s' % src_language,
        '.xml.%s' % src_language,
        '.html.%s' % src_language
    ]

    for path in lines:
        # Filter files
        for extension in extensions:
            if path.endswith(extension):
                break
        else:
            continue
        # Get the units
        write('.')
        try:
            handler = ro_database.get_handler(path)
        except Exception:
            print
            print '*'
            print '* Error:', path
            print '*'
            raise
        try:
            units = handler.get_units(srx_handler=srx_handler)
            units = list(units)
        except Exception:
            print
            print '*'
            print '* Error:', path
            print '*'
            raise

        relative_path = locale_folder_path.get_pathto(path)
        for source, context, line in units:
            po.add_unit(relative_path, source, context, line)
    print

    write('* Update PO template ')
    data = po.to_str()

    # Write the po into the locale.pot
    try:
        locale_pot = locale_folder.open('locale.pot', WRITE)
    except IOError:
        # The locale.pot file does not exist create and open
        locale_pot = locale_folder.make_file('locale.pot')
    else:
        with locale_pot:
            locale_pot.write(data)

    # Update PO files
    filenames = set([x for x in locale_folder.get_names() if x[-3:] == '.po'])
    filenames.add('%s.po' % src_language)
    for language in config.get_value('target_languages'):
        filenames.add('%s.po' % language)
    filenames = list(filenames)
    filenames.sort()

    print '* Update PO files:'
    locale_pot_path = locale_folder.get_absolute_path('locale.pot')
    for filename in filenames:
        if locale_folder.exists(filename):
            write('  %s ' % filename)
            file_path = locale_folder.get_absolute_path(filename)
            if no_wrap:
                call([
                    'msgmerge', '--no-wrap', '-U', '-s', file_path,
                    locale_pot_path
                ])
            else:
                call(['msgmerge', '-U', '-s', file_path, locale_pot_path])
        else:
            print '  %s (new)' % filename
            file_path = locale_folder.get_absolute_path(filename)
            lfs.copy(locale_pot_path, file_path)
    print
示例#29
0
def update_locale(srx_handler, exclude_folders, no_wrap=False):
    # Read configuration for languages
    config = get_config()
    src_language = config.get_value('source_language', default='en')

    # Get local folder
    package_root = config.get_value('package_root')
    if lfs.exists(package_root):
        locale_folder_path = Path('{0}/locale'.format(package_root))
    else:
        locale_folder_path = Path('locale/')
    locale_folder = lfs.open(locale_folder_path)

    # Initialize message catalog
    po = POFile()
    lines = []
    for line in open('MANIFEST').readlines():
        line = line.strip()
        exclude_folder = False
        for x in exclude_folders:
            if line.startswith(x):
                exclude_folder = True
                break
        if exclude_folder is False:
            lines.append(line)

    # Process Python and HTML files
    write('* Extract text strings')
    extensions = [
        '.py',
        '.js',
        '.xhtml.%s' % src_language,
        '.xml.%s' % src_language,
        '.html.%s' % src_language]

    for path in lines:
        # Filter files
        for extension in extensions:
            if path.endswith(extension):
                break
        else:
            continue
        # Get the units
        write('.')
        try:
            handler = ro_database.get_handler(path)
        except Exception:
            print
            print '*'
            print '* Error:', path
            print '*'
            raise
        try:
            units = handler.get_units(srx_handler=srx_handler)
            units = list(units)
        except Exception:
            print
            print '*'
            print '* Error:', path
            print '*'
            raise

        relative_path = locale_folder_path.get_pathto(path)
        for source, context, line in units:
            po.add_unit(relative_path, source, context, line)
    print

    write('* Update PO template ')
    data = po.to_str()

    # Write the po into the locale.pot
    try:
        locale_pot = locale_folder.open('locale.pot', WRITE)
    except IOError:
        # The locale.pot file does not exist create and open
        locale_pot = locale_folder.make_file('locale.pot')
    else:
        with locale_pot:
            locale_pot.write(data)

    # Update PO files
    filenames = set([ x for x in locale_folder.get_names() if x[-3:] == '.po' ])
    filenames.add('%s.po' % src_language)
    for language in config.get_value('target_languages'):
        filenames.add('%s.po' % language)
    filenames = list(filenames)
    filenames.sort()

    print '* Update PO files:'
    locale_pot_path = locale_folder.get_absolute_path('locale.pot')
    for filename in filenames:
        if locale_folder.exists(filename):
            write('  %s ' % filename)
            file_path = locale_folder.get_absolute_path(filename)
            if no_wrap:
                call(['msgmerge', '--no-wrap', '-U', '-s', file_path, locale_pot_path])
            else:
                call(['msgmerge', '-U', '-s', file_path, locale_pot_path])
        else:
            print '  %s (new)' % filename
            file_path = locale_folder.get_absolute_path(filename)
            lfs.copy(locale_pot_path, file_path)
    print
示例#30
0
        filenames.append(filename)

        f = open(filename, 'w')

        aux = []
        for message in messages:
            if message not in aux:
                f.write('msgid "%s"\n' % message)
                f.write('msgstr ""\n')
                f.write('\n')
            aux.append(message)
        f.close()

    # Parse the rest of the files
    if filenames2:
        po = POFile()
        for filename in filenames2:
            handler = get_handler(filename)
            for msgid, line_number in handler.get_messages():
                po.set_message(msgid, references={filename: [line_number]})
        filename = mktemp('.po')
        filenames.append(filename)
        open(filename, 'w').write(po.to_str())

    # Create the POT file
    if filenames:
        filename = mktemp('.po')
        cmd = 'msgcat -s --output-file=%s %s' % (filename, ' '.join(filenames))
        system(cmd)
        system('msgmerge -U locale/locale.pot %s' % filename)
示例#31
0
    # Get local folder
    package_root = config.get_value('package_root')
    if lfs.exists(package_root):
        locale_folder_path = Path('{0}/locale'.format(package_root))
    else:
        locale_folder_path = Path('locale/')
    locale_folder = lfs.open(locale_folder_path)

    # The SRX file
    if options.srx is not None:
        srx_handler = ro_database.get_handler(options.srx)
    else:
        srx_handler = None

    # Initialize message catalog
    po = POFile()
    lines = []
    for line in open('MANIFEST').readlines():
        line = line.strip()
        if line.split(sep)[0] not in ('archive', 'docs', 'skeleton', 'test'):
            lines.append(line)

    # Process Python and HTML files
    write('* Extract text strings')
    extensions = ['.py', '.xhtml.%s' % src_language, '.xml.%s' % src_language]

    for path in lines:
        # Filter files
        for extension in extensions:
            if path.endswith(extension):
                break
示例#32
0
        filenames.append(filename)

        f = open(filename, 'w')

        aux = []
        for message in messages:
            if message not in aux:
                f.write('msgid "%s"\n' % message)
                f.write('msgstr ""\n')
                f.write('\n')
            aux.append(message)
        f.close()

    # Parse the rest of the files
    if filenames2:
        po = POFile()
        for filename in filenames2:
            handler = get_handler(filename)
            for source, context, line in handler.get_units():
                po.add_unit(filename, source, context, line)
        filename = mktemp('.po')
        filenames.append(filename)
        open(filename, 'w').write(po.to_str())

    # Create the POT file
    if filenames:
        filename = mktemp('.po')
        cmd = 'msgcat -s --output-file=%s %s' % (filename, ' '.join(filenames))
        system(cmd)
        system('msgmerge -U locale/locale.pot %s' % filename)