| 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 <kourem@gmail.com> |
|---|
| 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 | |
|---|
| 40 | ## For windows: set, if needed, a value in LANG environmental variable ## |
|---|
| 41 | if os.name == 'nt': |
|---|
| 42 | lang = os.getenv('LANG') |
|---|
| 43 | if lang is None: |
|---|
| 44 | default_lang = locale.getdefaultlocale()[0] # en_US, fr_FR, el_GR etc.. |
|---|
| 45 | if default_lang: |
|---|
| 46 | lang = default_lang |
|---|
| 47 | |
|---|
| 48 | if lang: |
|---|
| 49 | os.environ['LANG'] = lang |
|---|
| 50 | |
|---|
| 51 | gettext.install(APP, DIR, unicode = True) |
|---|
| 52 | if gettext._translations: |
|---|
| 53 | _translation = gettext._translations.values()[0] |
|---|
| 54 | else: |
|---|
| 55 | _translation = gettext.NullTranslations() |
|---|
| 56 | |
|---|
| 57 | def Q_(s): |
|---|
| 58 | # Qualified translatable strings |
|---|
| 59 | # Some strings are too ambiguous to be easily translated. |
|---|
| 60 | # so we must use as: |
|---|
| 61 | # s = Q_('?vcard:Unknown') |
|---|
| 62 | # widget.set_text(s) |
|---|
| 63 | # Q_() removes the ?vcard: |
|---|
| 64 | # but gettext while parsing the file detects ?vcard:Unknown as a whole string. |
|---|
| 65 | # translator can either put the ?vcard: part or no (easier for him or her to no) |
|---|
| 66 | # nothing fails |
|---|
| 67 | s = _(s) |
|---|
| 68 | if s[0] == '?': |
|---|
| 69 | s = s[s.find(':')+1:] # remove ?abc: part |
|---|
| 70 | return s |
|---|
| 71 | |
|---|
| 72 | def ngettext(s_sing, s_plural, n, replace_sing = None, replace_plural = None): |
|---|
| 73 | '''use as: |
|---|
| 74 | i18n.ngettext('leave room %s', 'leave rooms %s', len(rooms), 'a', 'a, b, c') |
|---|
| 75 | |
|---|
| 76 | in other words this is a hack to ngettext() to support %s %d etc.. |
|---|
| 77 | ''' |
|---|
| 78 | text = _translation.ungettext(s_sing, s_plural, n) |
|---|
| 79 | if n == 1 and replace_sing is not None: |
|---|
| 80 | text = text % replace_sing |
|---|
| 81 | elif n > 1 and replace_plural is not None: |
|---|
| 82 | text = text % replace_plural |
|---|
| 83 | return text |
|---|