00001 ############################################################################## 00002 # 00003 # Copyright (c) 2007-2008 Albert Cervera i Areny <albert@nan-tic.com> 00004 # 00005 # WARNING: This program as such is intended to be used by professional 00006 # programmers who take the whole responsability of assessing all potential 00007 # consequences resulting from its eventual inadequacies and bugs 00008 # End users who are looking for a ready-to-use solution with commercial 00009 # garantees and support are strongly adviced to contract a Free Software 00010 # Service Company 00011 # 00012 # This program is Free Software; you can redistribute it and/or 00013 # modify it under the terms of the GNU General Public License 00014 # as published by the Free Software Foundation; either version 2 00015 # of the License, or (at your option) any later version. 00016 # 00017 # This program is distributed in the hope that it will be useful, 00018 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 # GNU General Public License for more details. 00021 # 00022 # You should have received a copy of the GNU General Public License 00023 # along with this program; if not, write to the Free Software 00024 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00025 # 00026 ############################################################################## 00027 00028 import Paths 00029 import os 00030 00031 ## @brief Initializes gettext translation system. 00032 def initializeTranslations(language=None): 00033 import locale 00034 import gettext 00035 00036 name = 'koo' 00037 try: 00038 locale.setlocale(locale.LC_ALL, '') 00039 except: 00040 # If locale is not supported just continue 00041 # with default language 00042 print "Warning: Unsupported locale." 00043 00044 if not language: 00045 language, encoding = locale.getdefaultlocale() 00046 if not language: 00047 language = 'C' 00048 00049 # Set environment variables otherwise it doesn't properly 00050 # work on windows 00051 os.environ.setdefault('LANG', language) 00052 os.environ.setdefault('LANGUAGE', language) 00053 00054 00055 # First of all search the files in the l10n directory (in case Koo was 00056 # not installed in the system). 00057 directory = Paths.searchFile( 'l10n' ) 00058 if directory: 00059 try: 00060 lang = gettext.translation(name, directory, fallback=False) 00061 except: 00062 directory = None 00063 00064 if not directory: 00065 # If the first try didn't work try to search translation files 00066 # in standard directories 'share/locale' 00067 directory = Paths.searchFile( os.path.join('share','locale') ) 00068 lang = gettext.translation(name, directory, fallback=True) 00069 00070 lang.install(unicode=1) 00071 00072 ## @brief Initializes Qt translation system. 00073 def initializeQtTranslations(language=None): 00074 from PyQt4.QtCore import QTranslator, QCoreApplication, QLocale 00075 if not language: 00076 language = str(QLocale.system().name()) 00077 00078 # First we try to load the file with the same system language name 00079 # Usually in $LANG and looks something like ca_ES, de_DE, etc. 00080 file = Paths.searchFile( '%s.qm' % language, 'l10n' ) 00081 if not file: 00082 # If the first step didn't work try to remove country 00083 # information and try again. 00084 language = language.split('_')[0] 00085 file = Paths.searchFile( '%s.qm' % language, 'l10n' ) 00086 00087 # If no translation files were found, don't crash 00088 # but continue silently. 00089 if file: 00090 translator = QTranslator( QCoreApplication.instance() ) 00091 translator.load( file ) 00092 QCoreApplication.instance().installTranslator( translator ) 00093 00094 # First we try to load the file with the same system language name 00095 # Usually in $LANG and looks something like ca_ES, de_DE, etc. 00096 file = Paths.searchFile( 'qt_%s.qm' % language, 'l10n' ) 00097 if not file: 00098 # If the first step didn't work try to remove country 00099 # information and try again. 00100 language = language.split('_')[0] 00101 file = Paths.searchFile( 'qt_%s.qm' % language, 'l10n' ) 00102 # If no translation files were found, don't crash 00103 # but continue silently. 00104 if file: 00105 translator = QTranslator( QCoreApplication.instance() ) 00106 translator.load( file ) 00107 QCoreApplication.instance().installTranslator( translator ) 00108