diff options
author | Mert Tumer <mert.tumer@collabora.com> | 2022-07-05 12:03:27 +0300 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2022-10-12 16:52:03 +0200 |
commit | e20d2de7836da52dbf9e528d1043b1e188097bfd (patch) | |
tree | cec9878830ff561c6d9eb492aa25eb8e8b49876a /svtools | |
parent | d75f27fd738eeb2c7dc6d22f198d55d3a877aa0b (diff) |
new uno command uno:Translate with deepl api
New Uno command added for translation
right now it is only using deepl translation api
There's a section in the options > language settings
for setting up the api url and auth key
uno:Translate is a menu button under Format tab
which will bring up Language Selection dialog for translation.
DeepL can accept html as the input for translation, this new
feature leverages that by exporting paragraphs/selections to
html and paste them back without losing the formatting (in theory)
This works good in general but we may lose formatting in very complex
styled sentences.
Translation works in two ways;
1) Whole document
when there is no selection, it assumes that we want to translate whole
document. Each paragraphs is sent one by one so that the output timeout
can be minimum for each paragraph.
2) Selection
Change-Id: Ia2d3ab2f6757faf565b939e1d670a7dedac33390
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/140624
Tested-by: Jenkins
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Diffstat (limited to 'svtools')
-rw-r--r-- | svtools/Library_svt.mk | 1 | ||||
-rw-r--r-- | svtools/source/config/deeplcfg.cxx | 124 |
2 files changed, 125 insertions, 0 deletions
diff --git a/svtools/Library_svt.mk b/svtools/Library_svt.mk index bc7d07b3bd48..a2bc1aad7ccf 100644 --- a/svtools/Library_svt.mk +++ b/svtools/Library_svt.mk @@ -84,6 +84,7 @@ $(eval $(call gb_Library_add_exception_objects,svt,\ svtools/source/config/fontsubstconfig \ svtools/source/config/htmlcfg \ svtools/source/config/languagetoolcfg \ + svtools/source/config/deeplcfg \ svtools/source/config/itemholder2 \ svtools/source/config/miscopt \ svtools/source/config/slidesorterbaropt \ diff --git a/svtools/source/config/deeplcfg.cxx b/svtools/source/config/deeplcfg.cxx new file mode 100644 index 000000000000..3b022b70804b --- /dev/null +++ b/svtools/source/config/deeplcfg.cxx @@ -0,0 +1,124 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/log.hxx> +#include <sal/config.h> +#include <svtools/deeplcfg.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <tools/debug.hxx> + +using namespace utl; +using namespace com::sun::star::uno; + +struct DeeplOptions_Impl +{ + OUString sAPIUrl; + OUString sAuthKey; +}; + +const Sequence<OUString>& SvxDeeplOptions::GetPropertyNames() +{ + static Sequence<OUString> const aNames{ + "Deepl/ApiURL", + "Deepl/AuthKey", + }; + return aNames; +} + +const OUString& SvxDeeplOptions::getAPIUrl() const { return pImpl->sAPIUrl; } + +void SvxDeeplOptions::setAPIUrl(const OUString& rVal) +{ + pImpl->sAPIUrl = rVal; + SetModified(); +} + +const OUString& SvxDeeplOptions::getAuthKey() const { return pImpl->sAuthKey; } + +void SvxDeeplOptions::setAuthKey(const OUString& rVal) +{ + pImpl->sAuthKey = rVal; + SetModified(); +} + +namespace +{ +class theSvxDeeplOptions : public rtl::Static<SvxDeeplOptions, theSvxDeeplOptions> +{ +}; +} + +SvxDeeplOptions& SvxDeeplOptions::Get() { return theSvxDeeplOptions::get(); } + +SvxDeeplOptions::SvxDeeplOptions() + : ConfigItem("Office.Linguistic/Translation") + , pImpl(new DeeplOptions_Impl) +{ + Load(GetPropertyNames()); +} + +SvxDeeplOptions::~SvxDeeplOptions() {} +void SvxDeeplOptions::Notify(const css::uno::Sequence<OUString>&) { Load(GetPropertyNames()); } + +void SvxDeeplOptions::Load(const css::uno::Sequence<OUString>& aNames) +{ + Sequence<Any> aValues = GetProperties(aNames); + const Any* pValues = aValues.getConstArray(); + DBG_ASSERT(aValues.getLength() == aNames.getLength(), "GetProperties failed"); + if (aValues.getLength() != aNames.getLength()) + return; + for (int nProp = 0; nProp < aNames.getLength(); nProp++) + { + if (!pValues[nProp].hasValue()) + continue; + switch (nProp) + { + case 0: + pValues[nProp] >>= pImpl->sAPIUrl; + break; + case 1: + pValues[nProp] >>= pImpl->sAuthKey; + break; + default: + break; + } + } +} + +void SvxDeeplOptions::ImplCommit() +{ + const Sequence<OUString>& aNames = GetPropertyNames(); + Sequence<Any> aValues(aNames.getLength()); + Any* pValues = aValues.getArray(); + for (int nProp = 0; nProp < aNames.getLength(); nProp++) + { + switch (nProp) + { + case 0: + pValues[nProp] <<= pImpl->sAPIUrl; + break; + case 1: + pValues[nProp] <<= pImpl->sAuthKey; + break; + default: + break; + } + } + PutProperties(aNames, aValues); +}
\ No newline at end of file |