From e20d2de7836da52dbf9e528d1043b1e188097bfd Mon Sep 17 00:00:00 2001 From: Mert Tumer Date: Tue, 5 Jul 2022 12:03:27 +0300 Subject: 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 --- vcl/Library_vcl.mk | 1 + vcl/jsdialog/enabled.cxx | 1 + vcl/source/app/htmltransferable.cxx | 78 +++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 vcl/source/app/htmltransferable.cxx (limited to 'vcl') diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk index a432ceb09146..4b1751005bf0 100644 --- a/vcl/Library_vcl.mk +++ b/vcl/Library_vcl.mk @@ -412,6 +412,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/app/svmain \ vcl/source/app/timer \ vcl/source/app/unohelp2 \ + vcl/source/app/htmltransferable \ vcl/source/app/unohelp \ vcl/source/app/vclevent \ vcl/source/app/watchdog \ diff --git a/vcl/jsdialog/enabled.cxx b/vcl/jsdialog/enabled.cxx index 6dbc32cdaf2f..f260691a64d7 100644 --- a/vcl/jsdialog/enabled.cxx +++ b/vcl/jsdialog/enabled.cxx @@ -57,6 +57,7 @@ bool isBuilderEnabled(std::u16string_view rUIFile, bool bMobile) || rUIFile == u"xmlsec/ui/digitalsignaturesdialog.ui" || rUIFile == u"xmlsec/ui/viewcertdialog.ui" || rUIFile == u"xmlsec/ui/certgeneral.ui" || rUIFile == u"xmlsec/ui/certpage.ui" || rUIFile == u"svx/ui/accessibilitycheckdialog.ui" + || rUIFile == u"modules/swriter/ui/translationdialog.ui" || rUIFile == u"svx/ui/accessibilitycheckentry.ui") { return true; diff --git a/vcl/source/app/htmltransferable.cxx b/vcl/source/app/htmltransferable.cxx new file mode 100644 index 000000000000..24f65fe929b1 --- /dev/null +++ b/vcl/source/app/htmltransferable.cxx @@ -0,0 +1,78 @@ +/* -*- 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 +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace ::com::sun::star; + +namespace vcl::unohelper +{ +HtmlTransferable::HtmlTransferable(OString sData) + : data(sData) +{ +} + +HtmlTransferable::~HtmlTransferable() {} + +// css::uno::XInterface +uno::Any HtmlTransferable::queryInterface(const uno::Type& rType) +{ + uno::Any aRet = ::cppu::queryInterface(rType, static_cast(this)); + return (aRet.hasValue() ? aRet : OWeakObject::queryInterface(rType)); +} + +// css::datatransfer::XTransferable +uno::Any HtmlTransferable::getTransferData(const datatransfer::DataFlavor& rFlavor) +{ + SotClipboardFormatId nT = SotExchange::GetFormat(rFlavor); + if (nT != SotClipboardFormatId::HTML) + { + throw datatransfer::UnsupportedFlavorException(); + } + size_t size = data.getLength(); + uno::Sequence sData(size); + std::memcpy(sData.getArray(), data.getStr(), size); + return uno::Any(sData); +} + +uno::Sequence HtmlTransferable::getTransferDataFlavors() +{ + uno::Sequence aDataFlavors(1); + auto ref = aDataFlavors.getArray()[0]; + ref.MimeType = "text/html"; + ref.DataType = cppu::UnoType>::get(); + SotExchange::GetFormatDataFlavor(SotClipboardFormatId::HTML, aDataFlavors.getArray()[0]); + return aDataFlavors; +} + +sal_Bool HtmlTransferable::isDataFlavorSupported(const datatransfer::DataFlavor& rFlavor) +{ + SotClipboardFormatId nT = SotExchange::GetFormat(rFlavor); + return (nT == SotClipboardFormatId::HTML); +} + +} // namespace vcl::unohelper -- cgit