Welcome to mirror list, hosted at ThFree Co, Russian Federation.

i18n.py « common « src - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68b77aea304b2348dcb0d964fb1e910f151a2c7b (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
# -*- coding:utf-8 -*-
## src/common/i18n.py
##
## Copyright (C) 2003-2010 Yann Leboulanger <asterix AT lagaule.org>
## Copyright (C) 2004 Vincent Hanquez <tab AT snarc.org>
## Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com>
## Copyright (C) 2009 Benjamin Richter <br AT waldteufel-online.net>
##
## This file is part of Gajim.
##
## Gajim 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; version 3 only.
##
## Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
##

import locale
import gettext
import os
import defs
import unicodedata

def paragraph_direction_mark(text):
    """
    Determine paragraph writing direction according to
    http://www.unicode.org/reports/tr9/#The_Paragraph_Level

    Returns either Unicode LTR mark or RTL mark.
    """
    for char in text:
        bidi = unicodedata.bidirectional(char)
        if bidi == 'L':
            return u'\u200E'
        elif bidi == 'AL' or bidi == 'R':
            return u'\u200F'

    return u'\u200E'

APP = 'gajim'
DIR = defs.localedir

# set '' so each part of the locale that should be modified is set
# according to the environment variables
locale.setlocale(locale.LC_ALL, '')

## For windows: set, if needed, a value in LANG environmental variable ##
if os.name == 'nt':
    lang = os.getenv('LANG')
    if lang is None:
        default_lang = locale.getdefaultlocale()[0] # en_US, fr_FR, el_GR etc..
        if default_lang:
            lang = default_lang

    if lang:
        os.environ['LANG'] = lang

gettext.install(APP, DIR, unicode = True)
if gettext._translations:
    _translation = gettext._translations.values()[0]
else:
    _translation = gettext.NullTranslations()

def Q_(s):
    # Qualified translatable strings
    # Some strings are too ambiguous to be easily translated.
    # so we must use as:
    # s = Q_('?vcard:Unknown')
    # widget.set_text(s)
    # Q_() removes the ?vcard:
    # but gettext while parsing the file detects ?vcard:Unknown as a whole string.
    # translator can either put the ?vcard: part or no (easier for him or her to no)
    # nothing fails
    s = _(s)
    if s[0] == '?':
        s = s[s.find(':')+1:] # remove ?abc: part
    return s

def ngettext(s_sing, s_plural, n, replace_sing = None, replace_plural = None):
    """
    Use as:
            i18n.ngettext('leave room %s', 'leave rooms %s', len(rooms), 'a', 'a, b, c')

    In other words this is a hack to ngettext() to support %s %d etc..
    """
    text = _translation.ungettext(s_sing, s_plural, n)
    if n == 1 and replace_sing is not None:
        text = text % replace_sing
    elif n > 1 and replace_plural is not None:
        text = text % replace_plural
    return text