~doctormo/gdm-commmunity-greeter/tails-merge

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
#!/usr/bin/python
#
# Copyright 2011 Martin Owens <doctormo@gmail.com>
#
# 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, see <http://www.gnu.org/licenses/>
#
"""
Base classes for instantly translatable windows using GtkMe.
"""

import os
import logging
import babel
import locale
import gettext

from gi.repository import Gtk
from subprocess import Popen, PIPE
from gtkme import Window, FormWindow

MOFILES = '/usr/share/locale/'
DOMAIN  = 'gdm-community-greeter'
GDM_MOFILES = '/usr/share/locale-langpack/'

def get_supported_languages():
    """Runs locale -a to find supported languages"""
    unique = []
    cmd = Popen(['locale', '-a'], stdout=PIPE)
    for lang in str.split(cmd.communicate()[0]):
        if '_' in lang:
            lang = lang.split('_')[0]
            if lang not in unique:
                unique.append(lang)
                yield babel.Locale.parse(lang)

LANGS = list(get_supported_languages())
print LANGS

def get_texts(langs):
    result = {}
    for locale in langs:
        try:
            result[str(locale)] = gettext.translation(DOMAIN,MOFILES,[str(locale)])
        except IOError:
            pass
    return result

TEXTS = get_texts(LANGS)


class Translatable(object):
    """Provides functions for translating the window on the fly"""
    retain_focus = True

    def __init__(self):
        self.labels = []
        self.tips = []
        self.store_translations(self.window)

    def store_translations(self, widget):
        """Go through all widgets and store the translatable elements"""
        for child in widget.get_children():
            if isinstance(child, Gtk.Container):
                self.store_translations(child)
            if isinstance(child, Gtk.Label):
                self.labels.append( (child, child.get_label()) )
            if child.get_has_tooltip():
                self.tips.append( (child, child.get_tooltip_text()) )

    def language(self, lang):
        """Return normalised language for use in this process"""
        if '_' in lang:
            lang = lang.split('_')[0]
        if '.' in lang:
            lang = lang.split('.')[0]
        return lang.lower()

    def gettext(self, lang, text):
        """Return a translated string or string"""
        if lang:
            text = lang.gettext(text)
        return text

    def translate_to(self, lang):
        """Loop through everything and translate on the fly"""
        lang = TEXTS.get(self.language(lang), None)
        for (child, text) in self.labels:
            child.set_label(self.gettext(lang, text))
        for (child, text) in self.tips:
            child.set_tooltip_markup(self.gettext(lang, text))
        if self.window.get_sensitive() and self.retain_focus:
            self.window.present()

class TranslatableWindow(Translatable, Window):
    def __init__(self, *args, **kwargs):
        Window.__init__(self, *args, **kwargs)
        Translatable.__init__(self)

class TranslatableFormWindow(Translatable, FormWindow):
    def __init__(self, *args, **kwargs):
        FormWindow.__init__(self, *args, **kwargs)
        Translatable.__init__(self)