Back to index

moin  1.9.0~rc2
languages.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Language Statistics
00004 
00005     @copyright: 2002-2004 Juergen Hermann <jh@web.de>,  # Part of the code is
00006                 2007 MoinMoin:ThomasWaldmann,           # from useragents.py
00007                 2007 Nina Kuisma <ninnnu@gmail.com>
00008     @license: GNU GPL, see COPYING for details.
00009 """
00010 
00011 
00012 from MoinMoin import user, i18n
00013 
00014 
00015 def linkto(pagename, request, params=''):
00016     return used_languages(request)
00017 
00018 
00019 def get_data(request):
00020     _ = request.getText
00021     data = {}
00022 
00023     users = user.getUserList(request)
00024     for userID in users:
00025         current_user = user.User(request, userID)
00026         if current_user.language == u'':
00027             # User is using <Browser setting>, attempting to look up if we've managed to store the real language...
00028             try:
00029                 data[current_user.real_language] = data.get(current_user.real_language, 0) + 1
00030             except AttributeError: # Couldn't find the used language at all...
00031                 data[u''] = data.get(u'', 0) + 1
00032         else:
00033             data[current_user.language] = data.get(current_user.language, 0) + 1
00034     if u'' in data:
00035         data[u'browser'] = data.pop(u'') # In case we have users whose languages aren't detectable.
00036     data = [(cnt, current_user_language) for current_user_language, cnt in data.items()]
00037     data.sort()
00038     data.reverse()
00039     return data
00040 
00041 
00042 def used_languages(request):
00043     from MoinMoin.util.dataset import TupleDataset, Column
00044     from MoinMoin.widget.browser import DataBrowserWidget
00045 
00046     _ = request.getText
00047 
00048     data = get_data(request)
00049 
00050     total = 0.0
00051     for cnt, lang in data:
00052         total += cnt
00053 
00054 
00055     languages = TupleDataset()
00056     languages.columns = [Column('language', label=_("Language"), align='left'),
00057                          Column('value', label='%', align='right')]
00058 
00059     cnt_printed = 0
00060     data = data[:10]
00061 
00062     # Preparing "<Browser setting>"
00063     browserlang = _('<Browser setting>')
00064     browserlang = browserlang[1:len(browserlang) - 1].capitalize()
00065     if total:
00066         for cnt, lang in data:
00067             try:
00068                 if lang == u'browser':
00069                     languages.addRow((browserlang, "%(percent).2f%% (%(count)d)" % {
00070                         'percent': 100.0 * cnt / total,
00071                         'count': cnt}))
00072                 else:
00073                     lang = i18n.wikiLanguages()[lang]['x-language-in-english']
00074                     languages.addRow((lang, "%(percent).2f%% (%(count)d)" % {
00075                         'percent': 100.0 * cnt / total,
00076                         'count': cnt}))
00077                 cnt_printed += cnt
00078             except UnicodeError:
00079                 pass
00080         if total > cnt_printed:
00081             languages.addRow((_('Others'), "%(percent).2f%% (%(count)d)" % {
00082                 'percent': 100.0 * (total - cnt_printed) / total,
00083                 'count': total - cnt_printed}))
00084 
00085     else: # If we don't have any users, we can safely assume that the only real user is the visitor (who is normally ignored, though) who is using "Browser setting"
00086         languages.addRow((browserlang, "100% (1)"))
00087 
00088     table = DataBrowserWidget(request)
00089     table.setData(languages)
00090     return table.render(method="GET")
00091