|
| 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 [email protected] ! |
| 22 | +# |
| 23 | + |
| 24 | +import os |
| 25 | +import gettext |
| 26 | +import locale |
| 27 | +import gi |
| 28 | +gi.require_version('Gtk', '3.0') |
| 29 | +from gi.repository import Gtk |
| 30 | +from gi.repository import GObject |
| 31 | + |
| 32 | + |
| 33 | +def _(x): return x |
| 34 | + |
| 35 | +# Images were taken from http://www.sodipodi.com/ |
| 36 | +# except for korea taken from http://zh.wikipedia.org/wiki/Image:Unification_flag_of_Korea.svg |
| 37 | + |
| 38 | + |
| 39 | +lang_name_mapping = { |
| 40 | + 'zh_cn': (None, _('Chinese (simplified)'), 'china'), |
| 41 | + 'zh_tw': (None, _('Chinese (traditional)'), 'china'), |
| 42 | + 'cs': (None, _('Czech'), 'czech_republic'), |
| 43 | + 'da': (None, _('Danish'), 'denmark'), |
| 44 | + 'nl': (None, _('Dutch'), 'netherlands'), |
| 45 | + 'en': ('English', _('English'), 'united_states'), |
| 46 | + 'en_gb': ('English', _('English - Great Britain'), 'united_kingdom'), |
| 47 | + 'en_us': ('English', _('English - U.S.'), 'united_states'), |
| 48 | + 'fi': (None, _('Finnish'), 'finland'), |
| 49 | + 'fr': ('Français', _('French'), 'france'), |
| 50 | + 'de': (None, _('German'), 'germany'), |
| 51 | + 'hu': (None, _('Hungarian'), 'hungary'), |
| 52 | + 'it': (None, _('Italian'), 'italy'), |
| 53 | + 'ja': (None, _('Japanese'), 'japan'), |
| 54 | + 'ko': (None, _('Korean'), 'korea'), |
| 55 | + 'no': (None, _('Norwegian'), 'norway'), |
| 56 | + 'pl': (None, _('Polish'), 'poland'), |
| 57 | + 'pt': ('Português', _('Portuguese'), 'portugal'), |
| 58 | + 'pt_br': ('Português do Brasil', _('Portuguese - Brazilian'), 'brazil'), |
| 59 | + 'ru': (None, _('Russian'), 'russian_federation'), |
| 60 | + 'sk': (None, _('Slovak'), 'slovenia'), |
| 61 | + 'es': ('Español', _('Spanish'), 'spain'), |
| 62 | + 'sv': (None, _('Swedish'), 'sweden'), |
| 63 | + 'tr': (None, _('Turkish'), 'turkey'), |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +class LangDetails (object): |
| 68 | + def __init__(self, code, name, image, domain): |
| 69 | + self.code = code |
| 70 | + self.country_code = self.code.split('_')[0] |
| 71 | + self.name = name |
| 72 | + self.image = image |
| 73 | + self.domain = domain |
| 74 | + |
| 75 | + def guess_translation(self, fallback=False): |
| 76 | + try: |
| 77 | + self.gnutranslation = gettext.translation(self.domain, 'locale', |
| 78 | + languages=[self.code], fallback=fallback) |
| 79 | + return True |
| 80 | + except: |
| 81 | + return False |
| 82 | + |
| 83 | + def install(self): |
| 84 | + self.gnutranslation.install() |
| 85 | + |
| 86 | + def matches(self, code, exact=True): |
| 87 | + if exact: |
| 88 | + return code.lower() == self.code.lower() |
| 89 | + return code.split('_')[0].lower() == self.country_code.lower() |
| 90 | + |
| 91 | + |
| 92 | +def get_lang_details(lang, domain): |
| 93 | + mapping = lang_name_mapping.get(lang.lower(), None) |
| 94 | + if mapping is None: |
| 95 | + # Try just the country code |
| 96 | + lang = lang.split('_')[0] |
| 97 | + mapping = lang_name_mapping.get(lang.lower(), None) |
| 98 | + if mapping is None: |
| 99 | + return None |
| 100 | + if mapping[0] is None: |
| 101 | + return LangDetails(lang, mapping[1], mapping[2], domain) |
| 102 | + return LangDetails(lang, mapping[0], mapping[2], domain) |
| 103 | + |
| 104 | + |
| 105 | +def list_available_translations(domain): |
| 106 | + rv = [get_lang_details('en', domain)] |
| 107 | + rv[0].guess_translation(True) |
| 108 | + if not os.path.isdir('locale'): |
| 109 | + return rv |
| 110 | + for i, x in enumerate([x for x in os.listdir('locale') if os.path.isdir('locale/' + x) and not x.startswith('.')]): |
| 111 | + try: |
| 112 | + details = get_lang_details(x, domain) |
| 113 | + if details is not None: |
| 114 | + if details.guess_translation(): |
| 115 | + rv.append(details) |
| 116 | + except: |
| 117 | + raise |
| 118 | + pass |
| 119 | + return rv |
| 120 | + |
| 121 | + |
| 122 | +class LanguageComboBox (Gtk.ComboBox): |
| 123 | + def __init__(self, domain): |
| 124 | + liststore = Gtk.ListStore(GObject.TYPE_STRING) |
| 125 | + Gtk.ComboBox.__init__(self) |
| 126 | + |
| 127 | + self.set_model(liststore) |
| 128 | + self.cell = Gtk.CellRendererText() |
| 129 | + self.pack_start(self.cell, True) |
| 130 | + self.add_attribute(self.cell, 'text', 0) |
| 131 | + |
| 132 | + self.translations = list_available_translations(domain) |
| 133 | + for i, x in enumerate(self.translations): |
| 134 | + liststore.insert(i + 1, (gettext.gettext(x.name), )) |
| 135 | + self.connect('changed', self.install) |
| 136 | + |
| 137 | + def modify_bg(self, state, color): |
| 138 | + setattr(self.cell, 'background-gdk', color) |
| 139 | + setattr(self.cell, 'background-set', True) |
| 140 | + |
| 141 | + def install(self, *args): |
| 142 | + if self.get_active() > -1: |
| 143 | + self.translations[self.get_active()].install() |
| 144 | + else: |
| 145 | + code, encoding = locale.getdefaultlocale() |
| 146 | + if code is None: |
| 147 | + code = 'en' |
| 148 | + # Try to find the exact translation |
| 149 | + for i, t in enumerate(self.translations): |
| 150 | + if t.matches(code): |
| 151 | + self.set_active(i) |
| 152 | + break |
| 153 | + if self.get_active() < 0: |
| 154 | + # Failed, try to get the translation based only in the country |
| 155 | + for i, t in enumerate(self.translations): |
| 156 | + if t.matches(code, False): |
| 157 | + self.set_active(i) |
| 158 | + break |
| 159 | + if self.get_active() < 0: |
| 160 | + # nothing found, select first translation |
| 161 | + self.set_active(0) |
| 162 | + # Allow for other callbacks |
| 163 | + return False |
| 164 | + |
| 165 | +### |
| 166 | + |
| 167 | + |
| 168 | +def gather_other_translations(): |
| 169 | + from glob import glob |
| 170 | + lessons = filter(lambda x: os.path.isdir(x), glob('lessons/*')) |
| 171 | + lessons = map(lambda x: os.path.basename(x), lessons) |
| 172 | + lessons = map(lambda x: x[0].isdigit() and x[1:] or x, lessons) |
| 173 | + images = filter(lambda x: os.path.isdir(x), glob('images/*')) |
| 174 | + images = map(lambda x: os.path.basename(x), images) |
| 175 | + f = file('i18n_misc_strings.py', 'w') |
| 176 | + for e in images + lessons: |
| 177 | + f.write('_("%s")\n' % e) |
| 178 | + f.close() |
0 commit comments