From 64dc8cad728f6680166df0aa7b6ba797e1aa0c2b Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Tue, 11 Apr 2023 14:09:46 +0900 Subject: Added logic for language translation Signed-off-by: Michele Calgaro --- config.h.cmake | 3 + src/MainWindow.cpp | 241 +++++++++++++++++++++++++------------------------- src/MainWindow.h | 11 ++- src/UiGuiSettings.cpp | 4 + src/UiGuiSettings.h | 2 +- 5 files changed, 134 insertions(+), 127 deletions(-) diff --git a/config.h.cmake b/config.h.cmake index 1ee445a..0d0af75 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -18,3 +18,6 @@ // strstream support #cmakedefine HAVE_STRSTREAM + +// Need to undefine this to let TQt3's translator work properly +#undef TQT_NO_TRANSLATION diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 496636d..06bc2d9 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -32,11 +32,14 @@ ///-- #include "UiGuiHighlighter.h" ///-- #include "IndentHandler.h" -#include #include +#include #include +#include +#include #include #include +#include ///-- #include ///-- #include ///-- #include @@ -50,8 +53,6 @@ ///-- #include ///-- #include ///-- #include -///-- #include -///-- #include ///-- #include ///-- #include ///-- #include @@ -65,6 +66,7 @@ // \defgroup grp_MainWindow All concerning main window functionality. + /* \class MainWindow \ingroup grp_MainWindow @@ -79,24 +81,25 @@ \brief Constructs the main window. */ MainWindow::MainWindow(TQString file2OpenOnStart, TQWidget *parent) : - MainWindowBase(parent), m_aboutDialog(NULL), m_qSciSourceCodeEditor(NULL) -///- _mainWindowForm(NULL), _settings(NULL), -///- _highlighter(NULL), _textEditVScrollBar(NULL), _aboutDialogGraphicsView( -///- NULL), _settingsDialog(NULL), _encodingActionGroup(NULL), _saveEncodedActionGroup(NULL), -///- _highlighterActionGroup(NULL), _uiGuiTranslator(NULL), _qTTranslator(NULL), _toolBarWidget( -///- NULL), _indentHandler(NULL), _textEditLineColumnInfoLabel(NULL) + MainWindowBase(parent), m_aboutDialog(nullptr), m_qSciSourceCodeEditor(nullptr), + m_qTranslator(nullptr), m_uiGuiTranslator(nullptr) +///- _mainWindowForm(nullptr), _settings(nullptr), _saveEncodedActionGroup(nullptr), +///- _highlighter(nullptr), _textEditVScrollBar(nullptr), _aboutDialogGraphicsView( +///- nullptr), _settingsDialog(nullptr), _encodingActionGroup(nullptr) +///- _highlighterActionGroup(nullptr), _toolBarWidget( +///- nullptr), _indentHandler(nullptr), _textEditLineColumnInfoLabel(nullptr) { // Init of some variables. m_sourceCodeChanged = false; // Create the _settings object, which loads all UiGui settings from a file. m_settings = UiGuiSettings::getInstance(); -///-- -///-- // Initialize the language of the application. -///-- initApplicationLanguage(); + + // Initialize the language of the application. + initApplicationLanguage(); // Creates the main window and initializes it. - initMainWindow(); + initMainWindow(); // Create toolbar and insert it into the main window. initToolBar(); @@ -224,7 +227,6 @@ void MainWindow::initMainWindow() connect(m_settings, SIGNAL(whiteSpaceIsVisible(bool)), actionWhiteSpaceIsVisible, SLOT(setOn(bool))); actionWhiteSpaceIsVisible->setOn(m_settings->getValueByName("WhiteSpaceIsVisible").toBool()); - // Tell the TQScintilla editor if it has to show white space. connect(m_settings, SIGNAL(whiteSpaceIsVisible(bool)), this, SLOT(setWhiteSpaceVisibility(bool))); @@ -356,76 +358,75 @@ void MainWindow::initToolBar() ///-- _settings->registerObjectProperty(actionEnableSyntaxHighlighting, "checked", ///-- "SyntaxHighlightingEnabled"); ///-- } -///-- -///-- /* -///-- \brief Initializes the language of UniversalIndentGUI. -///-- -///-- If the program language is defined in the _settings, the corresponding language -///-- file will be loaded and set for the application. If not set there, the system -///-- default language will be set, if a translation file for that language exists. -///-- Returns true, if the translation file could be loaded. Otherwise it returns -///-- false and uses the default language, which is English. -///-- */ -///-- bool MainWindow::initApplicationLanguage() -///-- { -///-- TQString languageShort; -///-- -///-- // Get the language _settings from the _settings object. -///-- int languageIndex = _settings->getValueByName("language").toInt(); -///-- -///-- // If no language was set, indicated by a negative index, use the system language. -///-- if (languageIndex < 0) -///-- { -///-- languageShort = TQLocale::system().name(); -///-- -///-- // Chinese and Japanese language consist of country and language code. -///-- // For all others the language code will be cut off. -///-- if (languageShort.left(2) != "zh" && languageShort.left(2) != "ja") -///-- { -///-- languageShort = languageShort.left(2); -///-- } -///-- -///-- // If no translation file for the systems local language exist, fall back to English. -///-- if (_settings->getAvailableTranslations().indexOf(languageShort) < 0) -///-- { -///-- languageShort = "en"; -///-- } -///-- -///-- // Set the language setting to the new language. -///-- _settings->setValueByName("language", -///-- _settings->getAvailableTranslations().indexOf(languageShort)); -///-- } -///-- // If a language was defined in the _settings, get this language mnemonic. -///-- else -///-- { -///-- languageShort = _settings->getAvailableTranslations().at(languageIndex); -///-- } -///-- -///-- // Load the TQt own translation file and set it for the application. -///-- _qTTranslator = new TQTranslator(); -///-- bool translationFileLoaded; -///-- translationFileLoaded = _qTTranslator->load( -///-- SettingsPaths::getGlobalFilesPath() + "/translations/qt_" + languageShort); -///-- if (translationFileLoaded) -///-- { -///-- tqApp->installTranslator(_qTTranslator); -///-- } -///-- -///-- // Load the uigui translation file and set it for the application. -///-- _uiGuiTranslator = new TQTranslator(); -///-- translationFileLoaded = _uiGuiTranslator->load( -///-- SettingsPaths::getGlobalFilesPath() + "/translations/universalindent_" + languageShort); -///-- if (translationFileLoaded) -///-- { -///-- tqApp->installTranslator(_uiGuiTranslator); -///-- } -///-- -///-- //connect( _settings, SIGNAL(language(int)), this, SLOT(languageChanged(int)) ); -///-- _settings->registerObjectSlot(this, "languageChanged(int)", "language"); -///-- -///-- return translationFileLoaded; -///-- } -///-- + +/* + \brief Initializes the language of UniversalIndentGUI. + + If the program language is defined in the _settings, the corresponding language + file will be loaded and set for the application. If not set there, the system + default language will be set, if a translation file for that language exists. + Returns true, if the translation file could be loaded. Otherwise it returns + false and uses the default language, which is English. + */ +bool MainWindow::initApplicationLanguage() +{ + TQString languageShort; + + // Get the language settings from the settings object. + int languageIndex = m_settings->getValueByName("Language").toInt(); + + // If no language was set, indicated by a negative index, use the system language. + if (languageIndex < 0) + { + languageShort = TQLocale::system().name(); + + // Chinese and Japanese language consist of country and language code. + // For all others the language code will be cut off. + if (languageShort.left(2) != "zh" && languageShort.left(2) != "ja") + { + languageShort = languageShort.left(2); + } + + // If no translation file for the systems local language exist, fall back to English. + if (m_settings->getAvailableTranslations().findIndex(languageShort) < 0) + { + languageShort = "en"; + } + + // Set the language setting to the new language. + m_settings->setValueByName("Language", + m_settings->getAvailableTranslations().findIndex(languageShort)); + } + // If a language was defined in the m_settings, get this language mnemonic. + else + { + languageShort = m_settings->getAvailableTranslations()[languageIndex]; + } + + // Load the TQt own translation file and set it for the application. + m_qTranslator = new TQTranslator(); + bool translationFileLoaded; + translationFileLoaded = m_qTranslator->load(SettingsPaths::getGlobalFilesPath() + + "/translations/qt_" + languageShort); + if (translationFileLoaded) + { + tqApp->installTranslator(m_qTranslator); + } + + // Load the uigui translation file and set it for the application. + m_uiGuiTranslator = new TQTranslator(); + translationFileLoaded = m_uiGuiTranslator->load(SettingsPaths::getGlobalFilesPath() + + "/translations/universalindent_" + languageShort); + if (translationFileLoaded) + { + tqApp->installTranslator(m_uiGuiTranslator); + } + + connect(m_settings, SIGNAL(language(int)), this, SLOT(languageChanged(int)) ); + + return translationFileLoaded; +} + ///-- /* ///-- \brief Creates and initializes the indenter. ///-- */ @@ -1166,42 +1167,42 @@ bool MainWindow::maybeSave() return true; } -///-- /* -///-- \brief This slot is called whenever a language is selected in the menu. It tries to find the -///-- corresponding action in the languageInfoList and sets the language. -///-- */ -///-- void MainWindow::languageChanged(int languageIndex) -///-- { -///-- if (languageIndex < _settings->getAvailableTranslations().size()) -///-- { -///-- // Get the mnemonic of the new selected language. -///-- TQString languageShort = _settings->getAvailableTranslations().at(languageIndex); -///-- -///-- // Remove the old qt translation. -///-- tqApp->removeTranslator(_qTTranslator); -///-- -///-- // Remove the old uigui translation. -///-- tqApp->removeTranslator(_uiGuiTranslator); -///-- -///-- // Load the TQt own translation file and set it for the application. -///-- bool translationFileLoaded; -///-- translationFileLoaded = _qTTranslator->load( -///-- SettingsPaths::getGlobalFilesPath() + "/translations/qt_" + languageShort); -///-- if (translationFileLoaded) -///-- { -///-- tqApp->installTranslator(_qTTranslator); -///-- } -///-- -///-- // Load the uigui translation file and set it for the application. -///-- translationFileLoaded = _uiGuiTranslator->load( -///-- SettingsPaths::getGlobalFilesPath() + "/translations/universalindent_" + languageShort); -///-- if (translationFileLoaded) -///-- { -///-- tqApp->installTranslator(_uiGuiTranslator); -///-- } -///-- } -///-- } -///-- +/* + \brief This slot is called whenever a language is selected in the menu. It tries to find the + corresponding action in the languageInfoList and sets the language. + */ +void MainWindow::languageChanged(int languageIndex) +{ + if (languageIndex >= 0 && languageIndex < m_settings->getAvailableTranslations().size()) + { + // Get the mnemonic of the new selected language. + TQString languageShort = m_settings->getAvailableTranslations()[languageIndex]; + + // Remove the old qt translation. + tqApp->removeTranslator(m_qTranslator); + + // Remove the old uigui translation. + tqApp->removeTranslator(m_uiGuiTranslator); + + // Load the TQt own translation file and set it for the application. + bool translationFileLoaded; + translationFileLoaded = m_qTranslator->load(SettingsPaths::getGlobalFilesPath() + + "/translations/qt_" + languageShort); + if (translationFileLoaded) + { + tqApp->installTranslator(m_qTranslator); + } + + // Load the uigui translation file and set it for the application. + translationFileLoaded = m_uiGuiTranslator->load(SettingsPaths::getGlobalFilesPath() + + "/translations/universalindent_" + languageShort); + if (translationFileLoaded) + { + tqApp->installTranslator(m_uiGuiTranslator); + } + } +} + ///-- /* ///-- \brief Creates a menu entries in the file menu for opening and saving a file with different encodings. ///-- */ diff --git a/src/MainWindow.h b/src/MainWindow.h index 8453399..6c4c82a 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -35,9 +35,8 @@ class ToolBarWidget; /// class TQLabel; /// class TQScrollBar; /// class TQActionGroup; -/// class TQTranslator; -/// class TQextScintilla; +class TQTranslator; class MainWindow : public MainWindowBase @@ -68,7 +67,7 @@ class MainWindow : public MainWindowBase void previewTurnedOnOff(bool turnOn); void exportToPDF(); void exportToHTML(); -///-- void languageChanged(int languageIndex); + void languageChanged(int languageIndex); ///-- void encodingChanged(TQAction *encodingAction); ///-- void numberOfLinesChanged(); void updateRecentlyOpenedList(); @@ -86,7 +85,7 @@ class MainWindow : public MainWindowBase bool maybeSave(); ///-- void createEncodingMenu(); ///-- void createHighlighterMenu(); -///-- bool initApplicationLanguage(); + bool initApplicationLanguage(); void initMainWindow(); void initToolBar(); ///-- void initTextEditor(); @@ -118,8 +117,8 @@ class MainWindow : public MainWindowBase ///-- TQActionGroup *_encodingActionGroup; ///-- TQActionGroup *_saveEncodedActionGroup; ///-- TQActionGroup *_highlighterActionGroup; -///-- TQTranslator *_uiGuiTranslator; -///-- TQTranslator *_qTTranslator; + TQTranslator *m_uiGuiTranslator; + TQTranslator *m_qTranslator; ///-- bool m_sourceCodeChanged; ///-- bool _indentSettingsChanged; diff --git a/src/UiGuiSettings.cpp b/src/UiGuiSettings.cpp index e4ab510..b19de88 100644 --- a/src/UiGuiSettings.cpp +++ b/src/UiGuiSettings.cpp @@ -361,6 +361,10 @@ void UiGuiSettings::emitSignalForSetting(TQString settingName) { emit whiteSpaceIsVisible(m_settings[settingName].toBool()); } + else if (settingName == "Language") + { + emit language(m_settings[settingName].toInt()); + } } /* diff --git a/src/UiGuiSettings.h b/src/UiGuiSettings.h index 6fd6428..7f316f9 100644 --- a/src/UiGuiSettings.h +++ b/src/UiGuiSettings.h @@ -94,7 +94,7 @@ class UiGuiSettings : public TQObject void whiteSpaceIsVisible(bool value); // [++] void indenterParameterTooltipsEnabled(bool value); // [++] void tabWidth(int value); -// [++] void language(int value); + void language(int value); // [++] void lastUpdateCheck(TQDate value); // [++] void mainWindowState(TQByteArray value); -- cgit v1.2.1