From 67f5e404de7bccfeb7b7d72e1dfa5a0f8a209e18 Mon Sep 17 00:00:00 2001 From: Kaiyuan Li Date: Sat, 4 Oct 2014 01:48:02 -0700 Subject: [PATCH] Fix #476 Auto language changing when first launch - Freeseer will now scan the System language for the first time. If there is translation for the location, the interface language will be set to that language by default. Otherwise en_US will be used. - Unit test for this function is also added.. --- .../frontend/configtool/configtool.py | 3 +- src/freeseer/frontend/qtcommon/FreeseerApp.py | 6 +-- .../languages/{tr_ja.ts => tr_ja_JP.ts} | 0 src/freeseer/frontend/record/record.py | 3 +- .../frontend/reporteditor/reporteditor.py | 3 +- .../frontend/talkeditor/talkeditor.py | 3 +- src/freeseer/settings.py | 16 ++++++- src/freeseer/tests/test_settings.py | 43 +++++++++++++++++++ 8 files changed, 64 insertions(+), 13 deletions(-) rename src/freeseer/frontend/qtcommon/languages/{tr_ja.ts => tr_ja_JP.ts} (100%) create mode 100644 src/freeseer/tests/test_settings.py diff --git a/src/freeseer/frontend/configtool/configtool.py b/src/freeseer/frontend/configtool/configtool.py index 348fb582..34997220 100644 --- a/src/freeseer/frontend/configtool/configtool.py +++ b/src/freeseer/frontend/configtool/configtool.py @@ -55,11 +55,10 @@ class ConfigToolApp(FreeseerApp): ''' def __init__(self, profile, config): - FreeseerApp.__init__(self) + super(ConfigToolApp, self).__init__(config) # Load Config Stuff self.profile = profile - self.config = config icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/freeseer/logo.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off) diff --git a/src/freeseer/frontend/qtcommon/FreeseerApp.py b/src/freeseer/frontend/qtcommon/FreeseerApp.py index 19a34eca..df22a2db 100644 --- a/src/freeseer/frontend/qtcommon/FreeseerApp.py +++ b/src/freeseer/frontend/qtcommon/FreeseerApp.py @@ -55,8 +55,9 @@ class FreeseerApp(QMainWindow): - def __init__(self): + def __init__(self, config): super(FreeseerApp, self).__init__() + self.config = config self.icon = QIcon() self.icon.addPixmap(QPixmap(_fromUtf8(":/freeseer/logo.png")), QIcon.Normal, QIcon.Off) self.setWindowIcon(self.icon) @@ -193,6 +194,3 @@ def setupLanguageMenu(self): languageAction.setData(language) self.menuLanguage.addAction(languageAction) self.langActionGroup.addAction(languageAction) - - if self.current_language == str(language).strip("tr_").rstrip(".qm"): - languageAction.setChecked(True) diff --git a/src/freeseer/frontend/qtcommon/languages/tr_ja.ts b/src/freeseer/frontend/qtcommon/languages/tr_ja_JP.ts similarity index 100% rename from src/freeseer/frontend/qtcommon/languages/tr_ja.ts rename to src/freeseer/frontend/qtcommon/languages/tr_ja_JP.ts diff --git a/src/freeseer/frontend/record/record.py b/src/freeseer/frontend/record/record.py index d5da6748..726504ab 100644 --- a/src/freeseer/frontend/record/record.py +++ b/src/freeseer/frontend/record/record.py @@ -55,10 +55,9 @@ class RecordApp(FreeseerApp): """Freeseer's main GUI class.""" def __init__(self, profile, config): - FreeseerApp.__init__(self) + super(RecordApp, self).__init__(config) self.db = profile.get_database() - self.config = config self.controller = RecordingController(profile, self.db, self.config) self.recently_recorded_video = None diff --git a/src/freeseer/frontend/reporteditor/reporteditor.py b/src/freeseer/frontend/reporteditor/reporteditor.py index c34ace45..1c14f127 100644 --- a/src/freeseer/frontend/reporteditor/reporteditor.py +++ b/src/freeseer/frontend/reporteditor/reporteditor.py @@ -55,9 +55,8 @@ class ReportEditorApp(FreeseerApp): ''' def __init__(self, config, db): - FreeseerApp.__init__(self) + super(ReportEditorApp, self).__init__(config) - self.config = config self.db = db icon = QIcon() diff --git a/src/freeseer/frontend/talkeditor/talkeditor.py b/src/freeseer/frontend/talkeditor/talkeditor.py index a548abd0..8bfb68e8 100644 --- a/src/freeseer/frontend/talkeditor/talkeditor.py +++ b/src/freeseer/frontend/talkeditor/talkeditor.py @@ -59,9 +59,8 @@ class TalkEditorApp(FreeseerApp): '''Freeseer talk database editor main gui class''' def __init__(self, config, db): - FreeseerApp.__init__(self) + super(TalkEditorApp, self).__init__(config) - self.config = config self.db = db icon = QIcon() diff --git a/src/freeseer/settings.py b/src/freeseer/settings.py index 987fc18a..fa2e79c8 100644 --- a/src/freeseer/settings.py +++ b/src/freeseer/settings.py @@ -24,6 +24,8 @@ import os +from PyQt4.QtCore import QLocale + from freeseer.framework.config.core import Config from freeseer.framework.config.profile import ProfileManager import freeseer.framework.config.options as options @@ -36,6 +38,18 @@ profile_manager = ProfileManager(os.path.join(configdir, 'profiles')) +def detect_system_language(): + """Detect the system language""" + + translation = 'tr_{}'.format(QLocale.system().name()) + translation_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'frontend', 'qtcommon', 'languages') + translation_country_language = os.path.join(translation_path, '{}.ts'.format(translation)) + if os.path.isfile(translation_country_language): + return '{}.qm'.format(translation) + else: + return 'tr_en_US.qm' + + class FreeseerConfig(Config): """General Freeseer profile settings.""" @@ -51,4 +65,4 @@ class FreeseerConfig(Config): record_to_stream_plugin = options.StringOption('RTMP Streaming') audio_feedback = options.BooleanOption(False) video_preview = options.BooleanOption(True) - default_language = options.StringOption('tr_en_US.qm') + default_language = options.StringOption(detect_system_language()) diff --git a/src/freeseer/tests/test_settings.py b/src/freeseer/tests/test_settings.py new file mode 100644 index 00000000..4eaede8d --- /dev/null +++ b/src/freeseer/tests/test_settings.py @@ -0,0 +1,43 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# freeseer - vga/presentation capture software +# +# Copyright (C) 2014 Free and Open Source Software Learning Centre +# http://fosslc.org +# +# 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, either version 3 of the License, or +# (at your option) any later version. +# +# 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, see . + +# For support, questions, suggestions or any other inquiries, visit: +# http://wiki.github.com/Freeseer/freeseer/ + +from PyQt4.QtCore import QLocale + +from freeseer.settings import detect_system_language + + +class TestSettings: + """ Test Class for settings.py unit test""" + + def test_detect_system_language_translation_found(self, monkeypatch): + """Tests detect_system_language() returns the appropriate translation filename for the system language.""" + locales = ("ar_EG", "de_DE", "en_US", "fr_CA", "ja_JP", "nl_NL", "sv_SE", "zh_CN", "zh_HK") + for locale in locales: + monkeypatch.setattr(QLocale, "name", lambda x: locale) + assert detect_system_language() == "tr_{}.qm".format(locale) + + def test_detect_system_language_translation_not_found(self, monkeypatch): + """Tests detect_system_language() returns default if no translation found for system language.""" + monkeypatch.setattr(QLocale, "name", lambda x: "fr_FR") + assert detect_system_language() == "tr_en_US.qm"