Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ka_widget.py
blob: 0efa537d32d651dc21b399fcd5be8dd661447d7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# coding: UTF-8
# Copyright 2009 Thomas Jourdan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
import gtk
import gtk.glade
import os
import sys
import traceback
from gettext import gettext as _
import ka_controller
import ka_debug

class KandidWidget(object):
    """
    inv: self._widget_tree is not None
    """

    def __init__(self, main_view):
        # Create GUI and attach that widget to our window
        # Load Glade XML
        self._widget_tree = gtk.glade.XML("kandid.glade")
        
        # Get Window
        kandidWindow = self._widget_tree.get_widget('kandidWindow')
        # Get Windows child
        kandidWindow_child = kandidWindow.get_child()

        # Remove the widget's parent
        if kandidWindow_child.parent:
            kandidWindow_child.parent.remove(kandidWindow_child)
        
        # self.widget will be attached to the Activity
        # This can be any GTK widget except a window
        main_view.pack_start(kandidWindow_child)

        self._localizePopulation()
        # Display everything

    def getWidget_tree(self):
        return self._widget_tree

    widget_tree = property(getWidget_tree)
    
    def _localizePopulation(self):
        try:
            local = {'breedGenerationButton' : _('Breed'),
                     'randomGenerationButton' : _('Random'),
                     'flurryLabel' : _('Flurry rate:'),
                     'populationLabel' : _('Population'),
                     'zoomLabel' : _('Zoom'),
                     'introLabel' : _('Introduction'),
                     'statusLabel' : _('Status'),
                     }
            for key, label in local.iteritems():
                self._widget_tree.get_widget(key).set_label(label)
#                ka_debug.info('localize "%s" to "%s"' % (key, label)) 
    
            for cell_index in range(ka_controller.POPULATION_CAPACITY):
                local = {'favorite_menuitem_#' : _('My favorite'),
                         'awfull_menuitem_#' : _('Awful bore, replace it'),
                         'publishprotozoon_menuitem_#' : _('Publish to my friends'),
                         'zoomprotozoon_menuitem_#' : _('Zoom'),
                         }
                self._set_localized_child_text(local, cell_index)
    
            for cell_index in range(ka_controller.INCOMMING_CAPACITY):
                local = {'accept_menuitem_#' : _('Accept protozoon'),
                         'decline_menuitem_#' : _('Decline protozoon'),
                         }
                self._set_localized_child_text(local, cell_index)
        except:
            ka_debug.err('localize population failed [%s] [%s]' % \
                       (sys.exc_info()[0], sys.exc_info()[1]))
            traceback.print_exc(file=sys.__stderr__)

    def _set_localized_child_text(self, local, cell_index):
        strix = str(cell_index)
        for key, label in local.iteritems():
            ikey = key if cell_index < 0 else key.replace('#', strix)
            self._widget_tree.get_widget(ikey).get_child().set_text(label)
#            ka_debug.info('localize "%s" to "%s"' % (ikey, label)) 
            
    @staticmethod
    def get_localization():
        """ returns localization and territory
        post: len(__return__[0]) >= 2
        """
        languages = []
        territory, locale= '', ''
        for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
            val = os.environ.get(envar)
            if val:
                languages = val.split(':')
                for lang in languages:
                    pos = lang.find('.')
                    if pos >= 0:
                        lang = lang[:pos]
                    pos = lang.find('_')
                    if pos >= 0:
                        locale = lang[:pos]
                        territory = lang[pos+1:]
                    else:
                        locale = lang
                break
        ka_debug.info('languages: %s "%s" "%s"' % (languages, locale, territory)) 
        return locale, territory