Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/i18n.py
blob: caf66126e7b1310274d07ae2e63992461e1d4e64 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 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 2 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
#


### SliderPuzzeUI
### TODO: Describe
### $Id: $
###
### author: Carlos Neves (cn (at) sueste.net)
### (c) 2007 World Wide Workshop Foundation

import os
import gettext
import locale

import gtk, gobject

_ = lambda x: x

# Images were taken from http://www.sodipodi.com/ 
# except for korea taken from http://zh.wikipedia.org/wiki/Image:Unification_flag_of_Korea.svg

lang_name_mapping = {
    'zh_cn':(None, _('Chinese (simplified)'), 'china'),
    'zh_tw':(None, _('Chinese (traditional)'), 'china'),
    'cs':(None, _('Czech'),'czech_republic'),
    'da':(None, _('Danish'),'denmark'),
    'nl':(None, _('Dutch'), 'netherlands'),
    'en':('English', _('English'),'united_states'),
    'en_gb':('English', _('English - Great Britain'),'united_kingdom'),
    'en_us':('English', _('English - U.S.'),'united_states'),
    'fi':(None, _('Finnish'),'finland'),
    'fr':('Français', _('French'),'france'),
    'de':(None, _('German'),'germany'),
    'hu':(None, _('Hungarian'),'hungary'),
    'it':(None, _('Italian'),'italy'),
    'ja':(None, _('Japanese'),'japan'),
    'ko':(None, _('Korean'),'korea'),
    'no':(None, _('Norwegian'),'norway'),
    'pl':(None, _('Polish'),'poland'),
    'pt':('Português', _('Portuguese'),'portugal'),
    'pt_br':('Português do Brasil', _('Portuguese - Brazilian'),'brazil'),
    'ru':(None, _('Russian'),'russian_federation'),
    'sk':(None, _('Slovak'),'slovenia'),
    'es':('Español', _('Spanish'),'spain'),
    'sv':(None, _('Swedish'),'sweden'),
    'tr':(None, _('Turkish'),'turkey'),
    }

class LangDetails (object):
    def __init__ (self, code, name, image):
        self.code = code
        self.country_code = self.code.split('_')[0]
        self.name = name
        self.image = image

    def guess_translation (self, fallback=False):
        self.gnutranslation = gettext.translation(
            'org.worldwideworkshop.PollBuilder', 'locale', 
            [self.code], fallback=fallback)

    def install (self):
        self.gnutranslation.install()

    def matches (self, code, exact=True):
        if exact:
            return code.lower() == self.code.lower()
        return code.split('_')[0].lower() == self.country_code.lower()

def get_lang_details (lang):
    mapping = lang_name_mapping.get(lang.lower(), None)
    if mapping is None:
        # Try just the country code
        lang = lang.split('_')[0]
        mapping = lang_name_mapping.get(lang.lower(), None)
        if mapping is None:
            return None
    if mapping[0] is None:
        return LangDetails(lang, mapping[1], mapping[2])
    return LangDetails(lang, mapping[0], mapping[2])

def list_available_translations ():
    rv = [get_lang_details('en')]
    rv[0].guess_translation(True)
    for i,x in enumerate([x for x in os.listdir('locale') if os.path.isdir('locale/' + x) and not x.startswith('.')]):
        try:
            details = get_lang_details(x)
            if details is not None:
                details.guess_translation()
                rv.append(details)
        except:
            raise
            pass
    return rv

class LanguageComboBox (gtk.ComboBox):
    def __init__ (self):
        liststore = gtk.ListStore(gobject.TYPE_STRING)
        gtk.ComboBox.__init__(self, liststore)

        self.cell = gtk.CellRendererText()
        self.pack_start(self.cell, True)
        self.add_attribute(self.cell, 'text', 0)

        self.translations = list_available_translations()
        for i,x in enumerate(self.translations):
            liststore.insert(i+1, (gettext.gettext(x.name), ))
        self.connect('changed', self.install)

    def modify_bg (self, state, color):
        setattr(self.cell, 'background-gdk',color)
        setattr(self.cell, 'background-set',True)

    def install (self, *args):
        if self.get_active() > -1:
            self.translations[self.get_active()].install()
        else:
            code, encoding = locale.getdefaultlocale()
            # Handle default locale of None:
            if code is None:
                self.set_active(0)
            if self.get_active() < 0:
                # Try to find the exact translation
                for i,t in enumerate(self.translations):
                    if t.matches(code):
                        self.set_active(i)
                        break
            if self.get_active() < 0:
                # Failed, try to get the translation based only in the country
                for i,t in enumerate(self.translations):
                    if t.matches(code, False):
                        self.set_active(i)
                        break
            if self.get_active() < 0:
                # nothing found, select first translation
                self.set_active(0)
        # Allow for other callbacks
        return False

###
def gather_other_translations ():
    from glob import glob
    entries = filter(lambda x: os.path.isdir(x), glob('images/*'))
    entries.extend(filter(lambda x: os.path.isdir(x), glob('lessons/*')))
    entries = map(lambda x: os.path.basename(x), entries)
    f = file('i18n_misc_strings.py', 'w')
    for e in entries:
        f.write('_("%s")\n' % e)
    f.close()

if __name__ == '__main__':
    gather_other_translations()