| 1 | ## common/i18n.py |
|---|
| 2 | ## -*- coding: utf-8 -*- |
|---|
| 3 | ## Contributors for this file: |
|---|
| 4 | ## - Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 5 | ## - Nikos Kouremenos <kourem@gmail.com> |
|---|
| 6 | ## |
|---|
| 7 | ## Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 8 | ## Vincent Hanquez <tab@snarc.org> |
|---|
| 9 | ## Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 10 | ## Vincent Hanquez <tab@snarc.org> |
|---|
| 11 | ## Nikos Kouremenos <nkour@jabber.org> |
|---|
| 12 | ## Dimitur Kirov <dkirov@gmail.com> |
|---|
| 13 | ## Travis Shirk <travis@pobox.com> |
|---|
| 14 | ## Norman Rasmussen <norman@rasmussen.co.za> |
|---|
| 15 | ## |
|---|
| 16 | ## This program is free software; you can redistribute it and/or modify |
|---|
| 17 | ## it under the terms of the GNU General Public License as published |
|---|
| 18 | ## by the Free Software Foundation; version 2 only. |
|---|
| 19 | ## |
|---|
| 20 | ## This program is distributed in the hope that it will be useful, |
|---|
| 21 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 22 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 23 | ## GNU General Public License for more details. |
|---|
| 24 | ## |
|---|
| 25 | |
|---|
| 26 | import locale |
|---|
| 27 | import gettext |
|---|
| 28 | import os |
|---|
| 29 | |
|---|
| 30 | APP = 'gajim' |
|---|
| 31 | if os.path.isdir('../po'): |
|---|
| 32 | DIR = '../po' |
|---|
| 33 | else: |
|---|
| 34 | DIR = '../../locale' |
|---|
| 35 | |
|---|
| 36 | # set '' so each part of the locale that should be modified is set |
|---|
| 37 | # according to the environment variables |
|---|
| 38 | locale.setlocale(locale.LC_ALL, '') |
|---|
| 39 | _translation = None |
|---|
| 40 | |
|---|
| 41 | def init(): |
|---|
| 42 | global _translation |
|---|
| 43 | try: |
|---|
| 44 | _translation = gettext.translation(APP, DIR) |
|---|
| 45 | except IOError: |
|---|
| 46 | _translation = gettext.NullTranslations() |
|---|
| 47 | |
|---|
| 48 | init() |
|---|
| 49 | |
|---|
| 50 | def _(s): |
|---|
| 51 | if s == '': |
|---|
| 52 | return s |
|---|
| 53 | return _translation.ugettext(s) |
|---|
| 54 | |
|---|
| 55 | def Q_(s): |
|---|
| 56 | # Qualified translatable strings |
|---|
| 57 | # Some strings are too ambiguous to be easily translated. |
|---|
| 58 | # so we must use as: |
|---|
| 59 | # s = Q_('?vcard:Unknown') |
|---|
| 60 | # widget.set_text(s) |
|---|
| 61 | # Q_() removes the ?vcard: |
|---|
| 62 | # but gettext while parsing the file detects ?vcard:Unknown as a whole string. |
|---|
| 63 | # translator can either put the ?vcard: part or no (easier for him or her to no) |
|---|
| 64 | # nothing fails |
|---|
| 65 | s = _(s) |
|---|
| 66 | if s[0] == '?': |
|---|
| 67 | s = s[s.find(':')+1:] # remove ?abc: part |
|---|
| 68 | return s |
|---|
| 69 | |
|---|
| 70 | def ngettext(s_sing, s_plural, n, replace_sing = None, replace_plural = None): |
|---|
| 71 | '''use as: |
|---|
| 72 | i18n.ngettext('leave room %s', 'leave rooms %s', len(rooms), 'a', 'a, b, c') |
|---|
| 73 | |
|---|
| 74 | in other words this is a hack to ngettext() to support %s %d etc.. |
|---|
| 75 | ''' |
|---|
| 76 | text = _translation.ungettext(s_sing, s_plural, n) |
|---|
| 77 | if n == 1 and replace_sing is not None: |
|---|
| 78 | text = text % replace_sing |
|---|
| 79 | elif n > 1 and replace_plural is not None: |
|---|
| 80 | text = text % replace_plural |
|---|
| 81 | return text |
|---|