KTextAddons

bergamotengineplugin.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "bergamotengineplugin.h"
8#include "bergamotmarianinterface.h"
9#include <KLocalizedString>
10#include <KSharedConfig>
11
12BergamotEnginePlugin::BergamotEnginePlugin(QObject *parent)
13 : TextTranslator::TranslatorEnginePlugin(parent)
14 , mBergamotInterface(new BergamotMarianInterface(this))
15{
16 loadSettings();
17
18 connect(mBergamotInterface, &BergamotMarianInterface::errorText, this, [this](const QString &message) {
19 Q_EMIT translateFailed(message);
20 });
21 connect(mBergamotInterface, &BergamotMarianInterface::translationReady, this, [&](Translation translation) {
22 appendResult(translation.translation());
23 Q_EMIT translateDone();
24 });
25 connect(this, &BergamotEnginePlugin::languagesChanged, this, &BergamotEnginePlugin::slotLanguagesChanged);
26}
27
28BergamotEnginePlugin::~BergamotEnginePlugin() = default;
29
30void BergamotEnginePlugin::translate()
31{
32 clear();
33 mBergamotInterface->translate(inputText());
34}
35
36void BergamotEnginePlugin::loadSettings()
37{
38 mInstalledLanguages = BergamotEngineUtils::languageLocallyStored();
39 mSettingInfo.loadSettingsInfo();
40 updateBergamotModel();
41}
42
43void BergamotEnginePlugin::updateBergamotModel()
44{
45 // qDebug() << "mInstalledLanguages " << mInstalledLanguages << " from " << from() << " to() " << to();
46 if (from().isEmpty() || to().isEmpty()) {
47 return;
48 }
49 QString absolutePath;
50 for (const auto &installed : std::as_const(mInstalledLanguages)) {
51 if (installed.from == from() && installed.to == to()) {
52 absolutePath = installed.absoluteLanguageModelPath;
53 break;
54 }
55 }
56 if (absolutePath.isEmpty()) {
57 return;
58 }
59 qDebug() << " absolutePath " << absolutePath;
60 if (QDir().exists(absolutePath)) {
61 mBergamotInterface->setModel(absolutePath, mSettingInfo);
62 }
63}
64
65void BergamotEnginePlugin::slotConfigureChanged()
66{
67 loadSettings();
68}
69
70void BergamotEnginePlugin::slotLanguagesChanged()
71{
72 updateBergamotModel();
73}
74
75#include "moc_bergamotengineplugin.cpp"
Wrapper around a translation response from the bergamot service.
Definition translation.h:26
QString translation() const
Translation result.
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:48:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.