| 
									
										
										
										
											2008-08-15 17:31:51 +00:00
										 |  |  | # -*- coding:utf-8 -*- | 
					
						
							| 
									
										
										
										
											2008-08-15 03:20:23 +00:00
										 |  |  | ## src/common/i18n.py | 
					
						
							| 
									
										
										
										
											2004-05-16 23:47:14 +00:00
										 |  |  | ## | 
					
						
							| 
									
										
										
										
											2014-01-02 12:33:54 +04:00
										 |  |  | ## Copyright (C) 2003-2014 Yann Leboulanger <asterix AT lagaule.org> | 
					
						
							| 
									
										
										
										
											2008-08-15 03:20:23 +00:00
										 |  |  | ## Copyright (C) 2004 Vincent Hanquez <tab AT snarc.org> | 
					
						
							|  |  |  | ## Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com> | 
					
						
							| 
									
										
										
										
											2009-09-25 17:11:38 +02:00
										 |  |  | ## Copyright (C) 2009 Benjamin Richter <br AT waldteufel-online.net> | 
					
						
							| 
									
										
										
										
											2004-05-16 23:47:14 +00:00
										 |  |  | ## | 
					
						
							| 
									
										
										
										
											2007-10-22 11:13:13 +00:00
										 |  |  | ## This file is part of Gajim. | 
					
						
							|  |  |  | ## | 
					
						
							|  |  |  | ## Gajim is free software; you can redistribute it and/or modify | 
					
						
							| 
									
										
										
										
											2004-05-16 23:47:14 +00:00
										 |  |  | ## it under the terms of the GNU General Public License as published | 
					
						
							| 
									
										
										
										
											2007-10-22 11:13:13 +00:00
										 |  |  | ## by the Free Software Foundation; version 3 only. | 
					
						
							| 
									
										
										
										
											2004-05-16 23:47:14 +00:00
										 |  |  | ## | 
					
						
							| 
									
										
										
										
											2007-10-22 11:13:13 +00:00
										 |  |  | ## Gajim is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2004-05-16 23:47:14 +00:00
										 |  |  | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							| 
									
										
										
										
											2008-08-15 03:20:23 +00:00
										 |  |  | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
					
						
							| 
									
										
										
										
											2004-05-16 23:47:14 +00:00
										 |  |  | ## GNU General Public License for more details. | 
					
						
							|  |  |  | ## | 
					
						
							| 
									
										
										
										
											2007-10-22 11:13:13 +00:00
										 |  |  | ## You should have received a copy of the GNU General Public License | 
					
						
							| 
									
										
										
										
											2008-08-15 03:20:23 +00:00
										 |  |  | ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2007-10-22 11:13:13 +00:00
										 |  |  | ## | 
					
						
							| 
									
										
										
										
											2004-05-16 23:47:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-05-31 17:53:28 +00:00
										 |  |  | import locale | 
					
						
							|  |  |  | import gettext | 
					
						
							| 
									
										
										
										
											2005-09-05 15:32:26 +00:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2009-09-25 17:11:38 +02:00
										 |  |  | import unicodedata | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-23 17:55:24 +04:00
										 |  |  | # May be changed after GTK is imported | 
					
						
							| 
									
										
										
										
											2013-08-01 19:05:00 +02:00
										 |  |  | direction_mark = '\u200E' | 
					
						
							| 
									
										
										
										
											2013-07-23 17:55:24 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-25 17:11:38 +02:00
										 |  |  | def paragraph_direction_mark(text): | 
					
						
							| 
									
										
										
										
											2010-02-08 15:08:40 +01:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     Determine paragraph writing direction according to | 
					
						
							|  |  |  |     http://www.unicode.org/reports/tr9/#The_Paragraph_Level | 
					
						
							| 
									
										
										
										
											2009-11-26 13:58:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-08 15:08:40 +01:00
										 |  |  |     Returns either Unicode LTR mark or RTL mark. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     for char in text: | 
					
						
							|  |  |  |         bidi = unicodedata.bidirectional(char) | 
					
						
							|  |  |  |         if bidi == 'L': | 
					
						
							| 
									
										
										
										
											2013-01-01 19:44:25 +01:00
										 |  |  |             return '\u200E' | 
					
						
							| 
									
										
										
										
											2010-02-08 15:08:40 +01:00
										 |  |  |         elif bidi == 'AL' or bidi == 'R': | 
					
						
							| 
									
										
										
										
											2013-01-01 19:44:25 +01:00
										 |  |  |             return '\u200F' | 
					
						
							| 
									
										
										
										
											2009-09-25 17:11:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-01 19:44:25 +01:00
										 |  |  |     return '\u200E' | 
					
						
							| 
									
										
										
										
											2005-09-05 15:32:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | APP = 'gajim' | 
					
						
							| 
									
										
										
										
											2004-05-16 23:47:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-10 16:19:58 +00:00
										 |  |  | # set '' so each part of the locale that should be modified is set | 
					
						
							|  |  |  | # according to the environment variables | 
					
						
							| 
									
										
										
										
											2004-11-15 09:43:13 +00:00
										 |  |  | locale.setlocale(locale.LC_ALL, '') | 
					
						
							| 
									
										
										
										
											2006-02-21 11:12:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-24 21:05:05 +00:00
										 |  |  | ## For windows: set, if needed, a value in LANG environmental variable ## | 
					
						
							| 
									
										
										
										
											2006-02-24 15:04:06 +00:00
										 |  |  | if os.name == 'nt': | 
					
						
							| 
									
										
										
										
											2010-02-08 15:08:40 +01:00
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2006-02-21 11:12:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-08 15:08:40 +01:00
										 |  |  |     if lang: | 
					
						
							|  |  |  |         os.environ['LANG'] = lang | 
					
						
							| 
									
										
										
										
											2006-02-21 11:12:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-08 00:41:30 +02:00
										 |  |  |     localedir = "../po" | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     # try to find domain in localedir | 
					
						
							|  |  |  |     path = gettext.find(APP) | 
					
						
							|  |  |  |     if path: | 
					
						
							|  |  |  |         # extract localedir from localedir/language/LC_MESSAGES/domain.mo | 
					
						
							|  |  |  |         path, tail = os.path.split(path) | 
					
						
							|  |  |  |         path, tail = os.path.split(path) | 
					
						
							|  |  |  |         localedir, tail = os.path.split(path) | 
					
						
							|  |  |  |     else: # fallback to user locale | 
					
						
							|  |  |  |         base = os.getenv('XDG_DATA_HOME') | 
					
						
							|  |  |  |         if base is None or base[0] != '/': | 
					
						
							|  |  |  |             base = os.path.expanduser('~/.local/share') | 
					
						
							|  |  |  |         localedir = os.path.join(base, "locale") | 
					
						
							| 
									
										
										
										
											2017-10-17 16:28:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if hasattr(locale, 'bindtextdomain'): | 
					
						
							|  |  |  |         locale.bindtextdomain(APP, localedir) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-08 00:41:30 +02:00
										 |  |  | gettext.install(APP, localedir) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-15 06:49:04 +00:00
										 |  |  | if gettext._translations: | 
					
						
							| 
									
										
										
										
											2013-01-02 13:54:02 +01:00
										 |  |  |     _translation = list(gettext._translations.values())[0] | 
					
						
							| 
									
										
										
										
											2006-06-15 06:49:04 +00:00
										 |  |  | else: | 
					
						
							| 
									
										
										
										
											2010-02-08 15:08:40 +01:00
										 |  |  |     _translation = gettext.NullTranslations() | 
					
						
							| 
									
										
										
										
											2005-08-12 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-07 04:09:58 +03:00
										 |  |  | def Q_(text): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Translate the given text, optionally qualified with a special | 
					
						
							|  |  |  |     construction, which will help translators to disambiguate between | 
					
						
							|  |  |  |     same terms, but in different contexts. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     When translated text is returned - this rudimentary construction | 
					
						
							|  |  |  |     will be stripped off, if it's present. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Here is the construction to use: | 
					
						
							|  |  |  |         Q_("?vcard:Unknown") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Everything between ? and : - is the qualifier to convey the context | 
					
						
							|  |  |  |     to the translators. Everything after : - is the text itself. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     text = _(text) | 
					
						
							|  |  |  |     if text.startswith('?'): | 
					
						
							|  |  |  |         qualifier, text = text.split(':', 1) | 
					
						
							|  |  |  |     return text | 
					
						
							| 
									
										
										
										
											2005-08-24 11:54:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def ngettext(s_sing, s_plural, n, replace_sing = None, replace_plural = None): | 
					
						
							| 
									
										
										
										
											2010-02-08 15:08:40 +01:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     Use as: | 
					
						
							|  |  |  |             i18n.ngettext('leave room %s', 'leave rooms %s', len(rooms), 'a', 'a, b, c') | 
					
						
							| 
									
										
										
										
											2008-12-03 21:56:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-08 15:08:40 +01:00
										 |  |  |     In other words this is a hack to ngettext() to support %s %d etc.. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2013-01-03 13:41:52 +04:00
										 |  |  |     text = _translation.ngettext(s_sing, s_plural, n) | 
					
						
							| 
									
										
										
										
											2010-02-08 15:08:40 +01:00
										 |  |  |     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 |