openlp/openlp/core/utils/languagemanager.py

112 lines
4.8 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 #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2010 Raoul Snyman #
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
# Gorven, Scott Guerrieri, Christian Richter, Maikel Stuivenberg, Martin #
# Thompson, Jon Tibble, Carsten Tinggaard #
# --------------------------------------------------------------------------- #
# 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 #
###############################################################################
import logging
import os
2010-04-16 22:06:28 +00:00
from PyQt4 import QtCore, QtGui
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()
2010-04-16 22:06:28 +00:00
class LanguageManager(object):
"""
Helper for Language selection
"""
__qmList__ = None
2010-04-19 19:12:34 +00:00
AutoLanguage = False
2010-04-16 22:06:28 +00:00
@staticmethod
2010-04-19 19:12:34 +00:00
def get_translator(language):
if LanguageManager.AutoLanguage :
2010-04-16 22:06:28 +00:00
language = QtCore.QLocale.system().name()
lang_Path = AppLocation.get_directory(AppLocation.AppDir)
lang_Path = os.path.join(lang_Path, u'resources', u'i18n')
appTranslator = QtCore.QTranslator()
if appTranslator.load("openlp_" + language, lang_Path):
return appTranslator
@staticmethod
2010-04-19 19:12:34 +00:00
def find_qm_files():
2010-04-16 22:06:28 +00:00
trans_dir = AppLocation.get_directory(AppLocation.AppDir)
trans_dir = QtCore.QDir(os.path.join(trans_dir, u'resources', u'i18n'))
fileNames = trans_dir.entryList(QtCore.QStringList("*.qm"),
QtCore.QDir.Files, QtCore.QDir.Name)
for i in fileNames:
fileNames.replaceInStrings(i, trans_dir.filePath(i))
return fileNames
@staticmethod
2010-04-19 19:12:34 +00:00
def language_name(qmFile):
2010-04-16 22:06:28 +00:00
translator = QtCore.QTranslator()
translator.load(qmFile)
return translator.translate(u'MainWindow', u'English')
@staticmethod
2010-04-19 19:12:34 +00:00
def get_language():
2010-05-10 17:56:29 +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-04-16 22:06:28 +00:00
regEx = QtCore.QRegExp("^\[(.*)\]")
if regEx.exactMatch(language):
2010-04-19 19:12:34 +00:00
LanguageManager.AutoLanguage = True
2010-04-16 22:06:28 +00:00
language = regEx.cap(1)
return language
@staticmethod
2010-04-19 19:12:34 +00:00
def set_language(action):
2010-04-16 22:06:28 +00:00
actionName = u'%s' % action.objectName()
2010-04-19 19:12:34 +00:00
qmList = LanguageManager.get_qm_list()
if LanguageManager.AutoLanguage :
2010-04-16 22:06:28 +00:00
language = u'[%s]' % qmList[actionName]
else:
language = u'%s' % qmList[actionName]
2010-04-30 19:23:02 +00:00
QtCore.QSettings().setValue(
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)
2010-04-20 19:09:14 +00:00
QtGui.QMessageBox.information(None,
translate(u'LanguageManager', u'Language'),
translate(u'LanguageManager',
u'After restart new Language settings will be used.'))
2010-04-16 22:06:28 +00:00
@staticmethod
2010-04-19 19:12:34 +00:00
def init_qm_list():
2010-04-16 22:06:28 +00:00
LanguageManager.__qmList__ = {}
2010-04-19 19:12:34 +00:00
qmFiles = LanguageManager.find_qm_files()
2010-04-16 22:06:28 +00:00
for i, qmf in enumerate(qmFiles):
regEx = QtCore.QRegExp("^.*openlp_(.*).qm")
if regEx.exactMatch(qmf):
langName = regEx.cap(1)
2010-04-30 19:23:02 +00:00
LanguageManager.__qmList__[u'%#2i %s' % (i+1,
2010-04-19 19:12:34 +00:00
LanguageManager.language_name(qmf))] = langName
2010-04-16 22:06:28 +00:00
@staticmethod
2010-04-19 19:12:34 +00:00
def get_qm_list():
2010-04-16 22:06:28 +00:00
if LanguageManager.__qmList__ == None:
2010-04-19 19:12:34 +00:00
LanguageManager.init_qm_list()
2010-04-20 19:09:14 +00:00
return LanguageManager.__qmList__