File indexing completed on 2024-11-24 04:16:54

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "bergamotengineplugin.h"
0008 #include "bergamotmarianinterface.h"
0009 #include <KConfigGroup>
0010 #include <KLocalizedString>
0011 #include <KSharedConfig>
0012 
0013 BergamotEnginePlugin::BergamotEnginePlugin(QObject *parent)
0014     : TextTranslator::TranslatorEnginePlugin(parent)
0015     , mBergamotInterface(new BergamotMarianInterface(this))
0016 {
0017     loadSettings();
0018 
0019     connect(mBergamotInterface, &BergamotMarianInterface::errorText, this, [this](const QString &message) {
0020         Q_EMIT translateFailed(message);
0021     });
0022     connect(mBergamotInterface, &BergamotMarianInterface::translationReady, this, [&](Translation translation) {
0023         appendResult(translation.translation());
0024         Q_EMIT translateDone();
0025     });
0026     connect(this, &BergamotEnginePlugin::languagesChanged, this, &BergamotEnginePlugin::slotLanguagesChanged);
0027 }
0028 
0029 BergamotEnginePlugin::~BergamotEnginePlugin() = default;
0030 
0031 void BergamotEnginePlugin::translate()
0032 {
0033     clear();
0034     mBergamotInterface->translate(inputText());
0035 }
0036 
0037 void BergamotEnginePlugin::loadSettings()
0038 {
0039     mInstalledLanguages = BergamotEngineUtils::languageLocallyStored();
0040     mSettingInfo.loadSettingsInfo();
0041     updateBergamotModel();
0042 }
0043 
0044 void BergamotEnginePlugin::updateBergamotModel()
0045 {
0046     // qDebug() << "mInstalledLanguages  " << mInstalledLanguages << " from " << from() << " to() " << to();
0047     if (from().isEmpty() || to().isEmpty()) {
0048         return;
0049     }
0050     QString absolutePath;
0051     for (const auto &installed : std::as_const(mInstalledLanguages)) {
0052         if (installed.from == from() && installed.to == to()) {
0053             absolutePath = installed.absoluteLanguageModelPath;
0054             break;
0055         }
0056     }
0057     if (absolutePath.isEmpty()) {
0058         return;
0059     }
0060     qDebug() << " absolutePath " << absolutePath;
0061     if (QDir().exists(absolutePath)) {
0062         mBergamotInterface->setModel(absolutePath, mSettingInfo);
0063     }
0064 }
0065 
0066 void BergamotEnginePlugin::slotConfigureChanged()
0067 {
0068     loadSettings();
0069 }
0070 
0071 void BergamotEnginePlugin::slotLanguagesChanged()
0072 {
0073     updateBergamotModel();
0074 }
0075 
0076 #include "moc_bergamotengineplugin.cpp"