Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network/toolkit/i18n.py
blob: 86d3caedc1c56208eed801a10fb310d483fc681e (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
# Copyright (C) 2014 Aleksey Lim
#
# 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/>.

import os
import logging
from gettext import translation


# To let `encode()` working properly, avoid msgids gettext'izing
# but still populate .po files parsing the source code
_ = lambda x: x

_logger = logging.getLogger('i18n')
_i18n = {}


def default_lang():
    """Default language to fallback for localized strings.

    :returns:
        string in format of HTTP's Accept-Language

    """
    return default_langs()[0]


def default_langs():
    """Default languages list, i.e., including all secondory languages.

    :returns:
        list of strings in format of HTTP's Accept-Language

    """
    global _default_langs

    if _default_langs is None:
        locales = os.environ.get('LANGUAGE')
        if locales:
            locales = [i for i in locales.split(':') if i.strip()]
        else:
            from locale import getdefaultlocale
            locales = [getdefaultlocale()[0]]
        if not locales:
            _default_langs = ['en']
        else:
            _default_langs = []
            for locale in locales:
                lang = locale.strip().split('.')[0].lower()
                if lang == 'c':
                    lang = 'en'
                elif '_' in lang:
                    lang, region = lang.split('_')
                    if lang != region:
                        lang = '-'.join([lang, region])
                _default_langs.append(lang)
        _logger.info('Default languages are %r', _default_langs)

    return _default_langs


def decode(value, accept_language=None):
    if not value:
        return ''
    if not isinstance(value, dict):
        return value

    if accept_language is None:
        accept_language = default_langs()
    elif isinstance(accept_language, basestring):
        accept_language = [accept_language]
    accept_language.append('en')

    stripped_value = None
    for lang in accept_language:
        result = value.get(lang)
        if result is not None:
            return result

        prime_lang = lang.split('-')[0]
        if prime_lang != lang:
            result = value.get(prime_lang)
            if result is not None:
                return result

        if stripped_value is None:
            stripped_value = {}
            for k, v in value.items():
                if '-' in k:
                    stripped_value[k.split('-', 1)[0]] = v
        result = stripped_value.get(prime_lang)
        if result is not None:
            return result

    return value[min(value.keys())]


def encode(msgid, *args, **kwargs):
    if not _i18n:
        from sugar_network.toolkit.languages import LANGUAGES
        for lang in LANGUAGES:
            _i18n[lang] = translation('sugar-network', languages=[lang])
    result = {}

    for lang, trans in _i18n.items():
        msgstr = trans.gettext(msgid)
        if args:
            msgargs = []
            for arg in args:
                msgargs.append(decode(arg, lang))
            msgstr = msgstr % tuple(msgargs)
        elif kwargs:
            msgargs = {}
            for key, value in kwargs.items():
                msgargs[key] = decode(value, lang)
            msgstr = msgstr % msgargs
        result[lang] = msgstr

    return result


_default_lang = None
_default_langs = None