~ubuntu-branches/ubuntu/trusty/sugar-jigsawpuzzle-activity/trusty-proposed

« back to all changes in this revision

Viewing changes to mmm_modules/i18n.py

  • Committer: Bazaar Package Importer
  • Author(s): Luke Faraone
  • Date: 2010-07-29 17:47:23 UTC
  • Revision ID: james.westby@ubuntu.com-20100729174723-5gvrue5dnddqoou0
Tags: upstream-8
Import upstream version 8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# Copyright 2007 World Wide Workshop Foundation
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
#
 
20
# If you find this activity useful or end up using parts of it in one of your
 
21
# own creations we would love to hear from you at info@WorldWideWorkshop.org !
 
22
#
 
23
 
 
24
import os
 
25
import gettext
 
26
import locale
 
27
 
 
28
import gtk, gobject
 
29
 
 
30
_ = lambda x: x
 
31
 
 
32
# Images were taken from http://www.sodipodi.com/ 
 
33
# except for korea taken from http://zh.wikipedia.org/wiki/Image:Unification_flag_of_Korea.svg
 
34
 
 
35
lang_name_mapping = {
 
36
    'zh_cn':(None, _('Chinese (simplified)'), 'china'),
 
37
    'zh_tw':(None, _('Chinese (traditional)'), 'china'),
 
38
    'cs':(None, _('Czech'),'czech_republic'),
 
39
    'da':(None, _('Danish'),'denmark'),
 
40
    'nl':(None, _('Dutch'), 'netherlands'),
 
41
    'en':('English', _('English'),'united_states'),
 
42
    'en_gb':('English', _('English - Great Britain'),'united_kingdom'),
 
43
    'en_us':('English', _('English - U.S.'),'united_states'),
 
44
    'fi':(None, _('Finnish'),'finland'),
 
45
    'fr':('Français', _('French'),'france'),
 
46
    'de':(None, _('German'),'germany'),
 
47
    'hu':(None, _('Hungarian'),'hungary'),
 
48
    'it':(None, _('Italian'),'italy'),
 
49
    'ja':(None, _('Japanese'),'japan'),
 
50
    'ko':(None, _('Korean'),'korea'),
 
51
    'no':(None, _('Norwegian'),'norway'),
 
52
    'pl':(None, _('Polish'),'poland'),
 
53
    'pt':('Português', _('Portuguese'),'portugal'),
 
54
    'pt_br':('Português do Brasil', _('Portuguese - Brazilian'),'brazil'),
 
55
    'ru':(None, _('Russian'),'russian_federation'),
 
56
    'sk':(None, _('Slovak'),'slovenia'),
 
57
    'es':('Español', _('Spanish'),'spain'),
 
58
    'sv':(None, _('Swedish'),'sweden'),
 
59
    'tr':(None, _('Turkish'),'turkey'),
 
60
    }
 
61
 
 
62
class LangDetails (object):
 
63
    def __init__ (self, code, name, image, domain):
 
64
        self.code = code
 
65
        self.country_code = self.code.split('_')[0]
 
66
        self.name = name
 
67
        self.image = image
 
68
        self.domain = domain
 
69
 
 
70
    def guess_translation (self, fallback=False):
 
71
        try:
 
72
            self.gnutranslation = gettext.translation(self.domain, 'locale',
 
73
                    languages=[self.code], fallback=fallback)
 
74
            return True
 
75
        except:
 
76
            return False
 
77
 
 
78
    def install (self):
 
79
        self.gnutranslation.install()
 
80
 
 
81
    def matches (self, code, exact=True):
 
82
        if exact:
 
83
            return code.lower() == self.code.lower()
 
84
        return code.split('_')[0].lower() == self.country_code.lower()
 
85
 
 
86
def get_lang_details (lang, domain):
 
87
    mapping = lang_name_mapping.get(lang.lower(), None)
 
88
    if mapping is None:
 
89
        # Try just the country code
 
90
        lang = lang.split('_')[0]
 
91
        mapping = lang_name_mapping.get(lang.lower(), None)
 
92
        if mapping is None:
 
93
            return None
 
94
    if mapping[0] is None:
 
95
        return LangDetails(lang, mapping[1], mapping[2], domain)
 
96
    return LangDetails(lang, mapping[0], mapping[2], domain)
 
97
 
 
98
def list_available_translations (domain):
 
99
    rv = [get_lang_details('en', domain)]
 
100
    rv[0].guess_translation(True)
 
101
    if not os.path.isdir('locale'):
 
102
        return rv
 
103
    for i,x in enumerate([x for x in os.listdir('locale') if os.path.isdir('locale/' + x) and not x.startswith('.')]):
 
104
        try:
 
105
            details = get_lang_details(x, domain)
 
106
            if details is not None:
 
107
                if details.guess_translation():
 
108
                    rv.append(details)
 
109
        except:
 
110
            raise
 
111
            pass
 
112
    return rv
 
113
 
 
114
class LanguageComboBox (gtk.ComboBox):
 
115
    def __init__ (self, domain):
 
116
        liststore = gtk.ListStore(gobject.TYPE_STRING)
 
117
        gtk.ComboBox.__init__(self, liststore)
 
118
 
 
119
        self.cell = gtk.CellRendererText()
 
120
        self.pack_start(self.cell, True)
 
121
        self.add_attribute(self.cell, 'text', 0)
 
122
 
 
123
        self.translations = list_available_translations(domain)
 
124
        for i,x in enumerate(self.translations):
 
125
            liststore.insert(i+1, (gettext.gettext(x.name), ))
 
126
        self.connect('changed', self.install)
 
127
 
 
128
    def modify_bg (self, state, color):
 
129
        setattr(self.cell, 'background-gdk',color)
 
130
        setattr(self.cell, 'background-set',True)
 
131
 
 
132
    def install (self, *args):
 
133
        if self.get_active() > -1:
 
134
            self.translations[self.get_active()].install()
 
135
        else:
 
136
            code, encoding = locale.getdefaultlocale()
 
137
            if code is None:
 
138
                code = 'en'
 
139
            # Try to find the exact translation
 
140
            for i,t in enumerate(self.translations):
 
141
                if t.matches(code):
 
142
                    self.set_active(i)
 
143
                    break
 
144
            if self.get_active() < 0:
 
145
                # Failed, try to get the translation based only in the country
 
146
                for i,t in enumerate(self.translations):
 
147
                    if t.matches(code, False):
 
148
                        self.set_active(i)
 
149
                        break
 
150
            if self.get_active() < 0:
 
151
                # nothing found, select first translation
 
152
                self.set_active(0)
 
153
        # Allow for other callbacks
 
154
        return False
 
155
 
 
156
###
 
157
def gather_other_translations ():
 
158
    from glob import glob
 
159
    lessons = filter(lambda x: os.path.isdir(x), glob('lessons/*'))
 
160
    lessons = map(lambda x: os.path.basename(x), lessons)
 
161
    lessons = map(lambda x: x[0].isdigit() and x[1:] or x, lessons)
 
162
    images = filter(lambda x: os.path.isdir(x), glob('images/*'))
 
163
    images = map(lambda x: os.path.basename(x), images)
 
164
    f = file('i18n_misc_strings.py', 'w')
 
165
    for e in images+lessons:
 
166
        f.write('_("%s")\n' % e)
 
167
    f.close()
 
168