openlp/openlp/core/utils/languagemanager.py

153 lines
6.0 KiB
Python
Raw Normal View History

2010-04-16 22:06:28 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2010-12-26 11:04:47 +00:00
# Copyright (c) 2008-2011 Raoul Snyman #
# Portions copyright (c) 2008-2011 Tim Bentley, Jonathan Corwin, Michael #
# Gorven, Scott Guerrieri, Meinert Jordan, Armin Köhler, Andreas Preikschat, #
# Christian Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon #
# Tibble, Carsten Tinggaard, Frode Woldsund #
2010-04-16 22:06:28 +00:00
# --------------------------------------------------------------------------- #
# This program 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 2 of the License. #
# #
# This program 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 this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
2010-06-29 07:07:03 +00:00
"""
The :mod:`languagemanager` module provides all the translation settings and
language file loading for OpenLP.
"""
2010-04-16 22:06:28 +00:00
import logging
from PyQt4 import QtCore, QtGui
2010-06-09 17:09:32 +00:00
2010-04-30 19:23:02 +00:00
from openlp.core.utils import AppLocation
2010-04-20 19:09:14 +00:00
from openlp.core.lib import translate
2010-04-19 19:12:34 +00:00
log = logging.getLogger(__name__)
2010-04-16 22:06:28 +00:00
class LanguageManager(object):
"""
2010-06-08 15:38:09 +00:00
Helper for Language selection
2010-04-16 22:06:28 +00:00
"""
__qm_list__ = {}
auto_language = False
2010-04-16 22:06:28 +00:00
@staticmethod
2010-04-19 19:12:34 +00:00
def get_translator(language):
2010-06-29 07:07:03 +00:00
"""
Set up a translator to use in this instance of OpenLP
``language``
The language to load into the translator
"""
if LanguageManager.auto_language:
2010-04-16 22:06:28 +00:00
language = QtCore.QLocale.system().name()
lang_path = AppLocation.get_directory(AppLocation.LanguageDir)
2010-06-29 07:07:03 +00:00
app_translator = QtCore.QTranslator()
if app_translator.load(language, lang_path):
2010-06-29 07:07:03 +00:00
return app_translator
2010-04-16 22:06:28 +00:00
@staticmethod
2010-04-19 19:12:34 +00:00
def find_qm_files():
2010-06-29 07:07:03 +00:00
"""
Find all available language files in this OpenLP install
"""
trans_dir = QtCore.QDir(AppLocation.get_directory(
AppLocation.LanguageDir))
file_names = trans_dir.entryList(QtCore.QStringList(u'*.qm'),
2010-04-16 22:06:28 +00:00
QtCore.QDir.Files, QtCore.QDir.Name)
2010-06-29 07:07:03 +00:00
for name in file_names:
file_names.replaceInStrings(name, trans_dir.filePath(name))
return file_names
2010-04-16 22:06:28 +00:00
@staticmethod
2010-06-29 07:07:03 +00:00
def language_name(qm_file):
"""
Load the language name from a language file
``qm_file``
The file to obtain the name from
"""
translator = QtCore.QTranslator()
2010-06-29 07:07:03 +00:00
translator.load(qm_file)
return translator.translate('OpenLP.MainWindow', 'English',
2010-09-23 17:13:57 +00:00
'Please add the name of your language here')
2010-04-16 22:06:28 +00:00
@staticmethod
2010-04-19 19:12:34 +00:00
def get_language():
2010-06-29 07:07:03 +00:00
"""
Retrieve a saved language to use from settings
"""
2011-03-06 14:15:35 +00:00
settings = QtCore.QSettings(u'OpenLP', u'OpenLP')
language = unicode(settings.value(
2010-04-30 19:23:02 +00:00
u'general/language', QtCore.QVariant(u'[en]')).toString())
2010-04-19 19:12:34 +00:00
log.info(u'Language file: \'%s\' Loaded from conf file' % language)
2010-06-29 07:07:03 +00:00
reg_ex = QtCore.QRegExp("^\[(.*)\]")
if reg_ex.exactMatch(language):
LanguageManager.auto_language = True
2010-06-29 07:07:03 +00:00
language = reg_ex.cap(1)
2010-04-16 22:06:28 +00:00
return language
@staticmethod
2011-02-27 09:53:47 +00:00
def set_language(action, message=True):
2010-06-29 07:07:03 +00:00
"""
Set the language to translate OpenLP into
``action``
The language menu option
2011-02-27 09:53:47 +00:00
``message``
Display the message option
2010-06-29 07:07:03 +00:00
"""
language = u'en'
if action:
2010-09-21 19:53:46 +00:00
action_name = u'%s' % action.objectName()
qm_list = LanguageManager.get_qm_list()
2010-06-29 07:07:03 +00:00
language = u'%s' % qm_list[action_name]
if LanguageManager.auto_language:
language = u'[%s]' % language
2011-02-27 14:29:19 +00:00
# This needs to be here for the setValue to work
settings = QtCore.QSettings(u'OpenLP', u'OpenLP')
settings.setValue(
2010-04-30 19:23:02 +00:00
u'general/language', QtCore.QVariant(language))
2010-04-19 19:12:34 +00:00
log.info(u'Language file: \'%s\' written to conf file' % language)
2011-02-27 09:53:47 +00:00
if message:
QtGui.QMessageBox.information(None,
translate('OpenLP.LanguageManager', 'Language'),
translate('OpenLP.LanguageManager',
'Please restart OpenLP to use your new language setting.'))
2010-04-16 22:06:28 +00:00
@staticmethod
2010-04-19 19:12:34 +00:00
def init_qm_list():
2010-06-29 07:07:03 +00:00
"""
Initialise the list of available translations
"""
LanguageManager.__qm_list__ = {}
2010-06-29 07:07:03 +00:00
qm_files = LanguageManager.find_qm_files()
for counter, qmf in enumerate(qm_files):
reg_ex = QtCore.QRegExp("^.*i18n/(.*).qm")
if reg_ex.exactMatch(qmf):
name = u'%s' % reg_ex.cap(1)
LanguageManager.__qm_list__[u'%#2i %s' % (counter + 1,
LanguageManager.language_name(qmf))] = name
2010-04-16 22:06:28 +00:00
@staticmethod
2010-04-19 19:12:34 +00:00
def get_qm_list():
2010-06-29 07:07:03 +00:00
"""
Return the list of available translations
"""
if not LanguageManager.__qm_list__:
2010-04-19 19:12:34 +00:00
LanguageManager.init_qm_list()
return LanguageManager.__qm_list__