view data/plugin/action/CheckTranslation.py @ 385:cbc739f4e5eb

CheckTranslation: no link for translated page if it is in the set of not_translated_system_pages
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sun, 12 Jul 2009 11:54:00 +0200
parents 7e2f4d3c1007
children fc9cdf513432
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - CheckTranslation.py

    shows a table of System Pages and their translations to the users language
    if the users language is anything else than en

    @copyright: 2009 MoinMoin:ReimarBauer,
                2009 MoinMoin:ThomasWaldmann
    @license: GNU GPL, see COPYING for details.
"""

from MoinMoin import i18n
from MoinMoin.i18n import strings
i18n.strings = strings

from MoinMoin.Page import Page
from MoinMoin.util.dataset import TupleDataset, Column
from MoinMoin.widget.browser import DataBrowserWidget

# for this action's output, we want everything RED that does not exist
css_style_defs = """
<style type="text/css">
a.nonexistent:link, a.nonexistent:visited, a.nonexistent:hover { color: red; }
</style>
"""

def execute(pagename, request):
    _ = request.getText

    pageset_name = request.values.get('pageset')
    if pageset_name not in i18n.strings.pagesets:
        pageset_name = 'all_pages'
    pageset = getattr(i18n.strings, pageset_name)
    not_translated_system_pages_set = getattr(i18n.strings, "not_translated_system_pages")

    if pagename.startswith(u"MoinI18n/"):
        # if we get called from one of the pages on MoinMaster that contain
        # the .po file data, we assume that user wants to check THAT language:
        lang_default = pagename[9:]
    else:
        # use browser/settings language
        lang_default = request.lang or 'en'
    lang = request.values.get('language') or lang_default
    wiki_languages = sorted(i18n.wikiLanguages().keys())
    if lang not in wiki_languages:
        msg = _("We have no translation for '%s', falling back to 'en'!") % lang
        request.theme.add_msg(msg, "err")
        lang = 'en'

    def trans(text, request=request, lang=lang, **kw):
        return i18n.getText(text, request, lang, **kw)

    data = TupleDataset()
    data.columns = [
           Column('en', label=_('Original page') + " " + _("(Link, Size, Last-Edit)")),
           Column(lang, label=_('Translated page') + " " + _("(Link, Size, Last-Edit)")),
        ]

    edit_querystr = {'action': 'edit', 'editor': 'text'}
    edit_attrs = {'name': 'editlink', 'rel': 'nofollow', }
    raw_querystr = {'action': 'raw'}

    for orig_page_name in pageset:
        trans_page_name = trans(orig_page_name)
        orig_page = Page(request, orig_page_name)
        trans_page = Page(request, trans_page_name)

        orig_status = [
            orig_page.link_to(request, orig_page_name) + " " +
            orig_page.link_to(request, "[raw]", querystr=raw_querystr) + " " +
            orig_page.link_to(request, "[edit]", querystr=edit_querystr, **edit_attrs)
        ]
        info = "%d" % orig_page.size()
        last_edit = orig_page.lastEditInfo()
        if last_edit:
            info += " %(editor)s %(time)s" % last_edit
        orig_status.append(info)
        trans_status = [trans_page_name]
        if trans_page_name not in not_translated_system_pages_set:
            trans_status = [
                            trans_page.link_to(request, trans_page_name) + " " +
                            trans_page.link_to(request, "[raw]", querystr=raw_querystr) + " " +
                            trans_page.link_to(request, "[edit]", querystr=edit_querystr, **edit_attrs)
                            ]
            info = "%d" % trans_page.size()
            last_edit = trans_page.lastEditInfo()
            if last_edit:
                info += " %(editor)s %(time)s" % last_edit
            trans_status.append(info)

        data.addRow(("<br>".join(orig_status), "<br>".join(trans_status)))

    table = DataBrowserWidget(request)
    table.setData(data)
    page_table = ''.join(table.format(method='GET'))

    fmt = request.formatter
    this_page = Page(request, pagename)
    lang_links = [this_page.link_to_raw(request, _lang,
                                        querystr={'action': 'CheckTranslation',
                                                  'language': _lang,
                                                  'pageset': pageset_name, })
                  for _lang in wiki_languages if _lang != "en"]


    lang_selector = u''.join([fmt.paragraph(1), _("Choose:"), ' ', ' '.join(lang_links), fmt.paragraph(0)])

    pageset_links = [this_page.link_to_raw(request, _pageset_name,
                                           querystr={'action': 'CheckTranslation',
                                                     'language': lang,
                                                     'pageset': _pageset_name, })
                     for _pageset_name in i18n.strings.pagesets]

    po_pagename = "MoinI18n/%s" % lang
    po_page = Page(request, po_pagename)
    edit_po_page = ""
    if po_page.exists():
        edit_po_page = po_page.link_to(request, "[edit]", querystr=edit_querystr, **edit_attrs)
    lang_i18n = u''.join([fmt.paragraph(1), _("Based on: %s ") % po_pagename, edit_po_page, fmt.paragraph(0)])

    pageset_selector = u''.join([fmt.paragraph(1), _("Choose:"), ' ', ' '.join(pageset_links), fmt.paragraph(0)])
    title = _("Checking translation '%s' for page set '%s'") % (lang, pageset_name)
    request.theme.send_title(title, page=request.page, pagename=pagename)
    request.write(request.formatter.rawHTML(css_style_defs))
    request.write(request.formatter.startContent("content"))
    request.write(lang_i18n)
    request.write(lang_selector)
    request.write(pageset_selector)
    request.write(page_table)
    request.write(request.formatter.endContent())
    request.theme.send_footer(pagename)
    request.theme.send_closing_html()