1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/*
Copyright (©) 2003-2025 Teus Benschop.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <system/googletranslate.h>
#include <assets/view.h>
#include <assets/page.h>
#include <assets/header.h>
#include <assets/external.h>
#include <filter/roles.h>
#include <filter/url.h>
#include <filter/google.h>
#include <filter/shell.h>
#include <locale/translate.h>
#include <menu/logic.h>
#include <database/logs.h>
std::string system_googletranslate_url ()
{
return "system/googletranslate";
}
bool system_googletranslate_acl (Webserver_Request& webserver_request)
{
return roles::access_control (webserver_request, roles::manager);
}
std::string system_googletranslate (Webserver_Request& webserver_request)
{
std::string page {};
std::string success {};
std::string error {};
// The header.
Assets_Header header = Assets_Header (translate("Google Translate"), webserver_request);
header.add_bread_crumb (menu_logic_settings_menu (), menu_logic_settings_text ());
page = header.run ();
Assets_View view {};
view.set_variable ("external", assets_external_logic_link_addon ());
// The location of the googletranslate.txt file with required information about setup.
view.set_variable ("config", filter_url_create_root_path ({config::logic::config_folder (), "googletranslate.txt"}));
// Check whether the Google Translate JSON key can be read.
auto [ json_key, json_error ] = filter::google::get_json_key_value_error ();
if (!json_error.empty()) Database_Logs::log(json_error);
error.assign(json_error);
// Check whether gcloud has been installed on the server.
if (error.empty()) {
const bool gcloud_present = filter::shell::is_present (filter::shell::get_executable(filter::shell::Executable::gcloud));
if (!gcloud_present) {
error.assign("The gcloud CLI was not found on the server.");
}
}
// Check whether the service account can be activated.
if (error.empty()) {
auto [ activate_ok, activate_output ] = filter::google::activate_service_account ();
if (!activate_ok) error.assign(activate_output);
Database_Logs::log (activate_output);
}
// Print and store the gcloud access token.
if (error.empty()) {
auto [ access_ok, access_token ] = filter::google::print_store_access_token ();
if (!access_ok) error.assign(access_token);
Database_Logs::log ("Access token: " + access_token);
}
// Do a translation.
std::string english_text { "Jesus the Christ the Messiah" };
std::string greek_text;
if (error.empty()) {
auto [ trans_ok, translation, trans_err ] = filter::google::translate (english_text, "en", "el");
if (!trans_ok) error.assign(trans_err);
if (trans_ok) greek_text = translation;
}
// Handle the OK message.
if (error.empty()) if (json_key.length()) {
std::stringstream ss;
ss << translate("The connection to Google Translate looks good.");
ss << " ";
ss << translate("An example translation was made.");
ss << " - ";
ss << translate ("English") << ": ";
ss << std::quoted(english_text);
ss << " - ";
ss << translate ("Greek") << ": ";
ss << std::quoted(greek_text);
success = ss.str();
}
// Set the feedback.
view.set_variable ("success", success);
view.set_variable ("error", error);
page += view.render ("system", "googletranslate");
page += assets_page::footer ();
return page;
}
// One method to use Google Translate is through the Google Cloud Platform C++ Client Libraries.
// Information: https://github.com/googleapis/google-cloud-cpp
// While building this through $ cmake the build machine started to freeze up.
// It then indicated that there's was no application memory left.
// So this method is not used.
|