diff --git a/indra/llmessage/aihttptimeoutpolicy.cpp b/indra/llmessage/aihttptimeoutpolicy.cpp index 4c71111f5..409ff9215 100644 --- a/indra/llmessage/aihttptimeoutpolicy.cpp +++ b/indra/llmessage/aihttptimeoutpolicy.cpp @@ -955,7 +955,6 @@ P(responderIgnore); P(setDisplayNameResponder); P2(simulatorFeaturesReceived, transfer_22s_connect_10s); P2(startGroupVoteResponder, transfer_300s); -P(translationReceiver); P(uploadModelPremissionsResponder); P(verifiedDestinationResponder); P(viewerChatterBoxInvitationAcceptResponder); diff --git a/indra/newview/CMakeLists.txt b/indra/newview/CMakeLists.txt index 0ff76cb36..25df1702e 100644 --- a/indra/newview/CMakeLists.txt +++ b/indra/newview/CMakeLists.txt @@ -403,6 +403,7 @@ set(viewer_SOURCE_FILES llpanelprimmediacontrols.cpp llpanelprofile.cpp llpanelskins.cpp + llpaneltranslationsettings.cpp llpanelvoicedevicesettings.cpp llpanelvoiceeffect.cpp llpanelvolume.cpp @@ -482,6 +483,7 @@ set(viewer_SOURCE_FILES lltoolselectrect.cpp lltoolview.cpp lltracker.cpp + lltranslate.cpp lluploaddialog.cpp lluploadfloaterobservers.cpp llurl.cpp @@ -934,6 +936,7 @@ set(viewer_HEADER_FILES llpanelprimmediacontrols.h llpanelprofile.h llpanelskins.h + llpaneltranslationsettings.h llpanelvoicedevicesettings.h llpanelvoiceeffect.h llpanelvolume.h diff --git a/indra/newview/app_settings/settings.xml b/indra/newview/app_settings/settings.xml index 81e21494c..55d20bc41 100644 --- a/indra/newview/app_settings/settings.xml +++ b/indra/newview/app_settings/settings.xml @@ -16872,6 +16872,61 @@ This should be as low as possible, but too low may break functionality Value 1 + TranslateLanguage + + Comment + Translate Language specifier + Persist + 1 + Type + String + Value + default + + TranslateChat + + Comment + Translate incoming chat messages + Persist + 1 + Type + Boolean + Value + 0 + + TranslationService + + Comment + Translation API to use. (google|bing) + Persist + 1 + Type + String + Value + bing + + GoogleTranslateAPIKey + + Comment + Google Translate API key + Persist + 1 + Type + String + Value + + + BingTranslateAPIKey + + Comment + Bing AppID to use with the Microsoft Translator API + Persist + 1 + Type + String + Value + + InterpolationTime Comment diff --git a/indra/newview/ascentprefschat.cpp b/indra/newview/ascentprefschat.cpp index c604a3260..6174f0aea 100644 --- a/indra/newview/ascentprefschat.cpp +++ b/indra/newview/ascentprefschat.cpp @@ -41,6 +41,7 @@ #include "llradiogroup.h" #include "lluictrlfactory.h" #include "llviewercontrol.h" +#include "llpaneltranslationsettings.h" #include "NACLantispam.h" #include "lgghunspell_wrapper.h" #include "lltrans.h" @@ -48,9 +49,12 @@ #include "llstartup.h" +static void* createTranslationPanel(void*) { return LLPanelTranslationSettings::getInstance(); } + LLPrefsAscentChat::LLPrefsAscentChat() { - LLUICtrlFactory::getInstance()->buildPanel(this, "panel_preferences_ascent_chat.xml"); + mFactoryMap["Translation Panel"] = LLCallbackMap(createTranslationPanel, NULL); + LLUICtrlFactory::getInstance()->buildPanel(this, "panel_preferences_ascent_chat.xml", &getFactoryMap()); getChild("SpellBase")->setCommitCallback(boost::bind(&LLPrefsAscentChat::onSpellBaseComboBoxCommit, this, _2)); getChild("EmSpell_EditCustom")->setCommitCallback(boost::bind(&LLPrefsAscentChat::onSpellEditCustom, this)); @@ -557,5 +561,6 @@ void LLPrefsAscentChat::apply() { refreshValues(); refresh(); + LLPanelTranslationSettings::getInstance()->apply(); } diff --git a/indra/newview/llfloaterchat.cpp b/indra/newview/llfloaterchat.cpp index 449fe6028..8c8159927 100644 --- a/indra/newview/llfloaterchat.cpp +++ b/indra/newview/llfloaterchat.cpp @@ -60,6 +60,7 @@ #include "llparticipantlist.h" #include "llspeakers.h" #include "llstylemap.h" +#include "lltranslate.h" #include "lluictrlfactory.h" #include "llviewermessage.h" #include "llviewertexteditor.h" @@ -96,6 +97,7 @@ LLFloaterChat::LLFloaterChat(const LLSD& seed) getChild("show mutes")->setCommitCallback(boost::bind(&LLFloaterChat::onClickToggleShowMute, this, _2, getChild("Chat History Editor"), history_editor_with_mute)); history_editor_with_mute->setVisible(false); getChild("chat_history_open")->setCommitCallback(boost::bind(show_log_browser, "chat", "chat")); + getChildView("translate chat")->setEnabled(LLTranslate::isTranslationConfigured()); } LLFloaterChat::~LLFloaterChat() @@ -344,6 +346,11 @@ void LLFloaterChat::onClickToggleShowMute(bool show_mute, LLTextEditor* history_ (show_mute ? history_editor_with_mute : history_editor)->setCursorAndScrollToEnd(); } +void LLFloaterChat::showTranslationCheckbox(bool show) +{ + getChildView("translate chat")->setEnabled(show); +} + // Put a line of chat in all the right places void LLFloaterChat::addChat(const LLChat& chat, BOOL from_instant_message, diff --git a/indra/newview/llfloaterchat.h b/indra/newview/llfloaterchat.h index e1c55e779..7ff153c11 100644 --- a/indra/newview/llfloaterchat.h +++ b/indra/newview/llfloaterchat.h @@ -72,6 +72,7 @@ public: void onClickToggleShowMute(bool show_mute, class LLTextEditor* history_editor, LLTextEditor* history_editor_with_mute); static void chatFromLogFile(LLLogChat::ELogLineType type, std::string line, void* userdata); + void showTranslationCheckbox(bool show); static void loadHistory(); static void* createSpeakersPanel(void* data); static void* createChatPanel(void* data); diff --git a/indra/newview/llpaneltranslationsettings.cpp b/indra/newview/llpaneltranslationsettings.cpp new file mode 100644 index 000000000..5fd8a220a --- /dev/null +++ b/indra/newview/llpaneltranslationsettings.cpp @@ -0,0 +1,320 @@ +/** + * @file llpaneltranslationsettings.cpp + * @brief Machine translation settings for chat + * + * $LicenseInfo:firstyear=2011&license=viewerlgpl$ + * Second Life Viewer Source Code + * Copyright (C) 2011, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA + * $/LicenseInfo$ + */ + +#include "llviewerprecompiledheaders.h" + +#include "llpaneltranslationsettings.h" + +// Viewer includes +#include "llfloaterchat.h" +#include "lltranslate.h" +#include "llviewercontrol.h" // for gSavedSettings + +// Linden library includes +#include "llbutton.h" +#include "llcheckboxctrl.h" +#include "llcombobox.h" +#include "lllineeditor.h" +#include "llnotificationsutil.h" +#include "llradiogroup.h" +#include "lltexteditor.h" +#include "lluictrlfactory.h" + +class EnteredKeyVerifier : public LLTranslate::KeyVerificationReceiver +{ +public: + EnteredKeyVerifier(LLTranslate::EService service, bool alert) + : LLTranslate::KeyVerificationReceiver(service) + , mAlert(alert) + { + } + +private: + /*virtual*/ void setVerificationStatus(bool ok) + { + LLPanelTranslationSettings* panel = LLPanelTranslationSettings::getInstance(); + if (!panel) + { + llwarns << "The translation settings panel has disappeared!" << llendl; + return; + } + + switch (getService()) + { + case LLTranslate::SERVICE_BING: + panel->setBingVerified(ok, mAlert); + break; + case LLTranslate::SERVICE_GOOGLE: + panel->setGoogleVerified(ok, mAlert); + break; + } + } + /*virtual*/ const char* getName() const { return "EnteredKeyVerifier"; } + + bool mAlert; +}; + +LLPanelTranslationSettings::LLPanelTranslationSettings() +: mMachineTranslationCB(NULL) +, mLanguageCombo(NULL) +, mTranslationServiceRadioGroup(NULL) +, mBingAPIKeyEditor(NULL) +, mGoogleAPIKeyEditor(NULL) +, mBingVerifyBtn(NULL) +, mGoogleVerifyBtn(NULL) +, mBingKeyVerified(false) +, mGoogleKeyVerified(false) +{ + LLUICtrlFactory::getInstance()->buildPanel(this, "panel_translation_settings.xml"); + + mMachineTranslationCB->setValue(gSavedSettings.getBOOL("TranslateChat")); + mLanguageCombo->setSelectedByValue(gSavedSettings.getString("TranslateLanguage"), TRUE); + mTranslationServiceRadioGroup->setSelectedByValue(gSavedSettings.getString("TranslationService"), TRUE); + + std::string bing_key = gSavedSettings.getString("BingTranslateAPIKey"); + if (!bing_key.empty()) + { + mBingAPIKeyEditor->setText(bing_key); + mBingAPIKeyEditor->setTentative(FALSE); + verifyKey(LLTranslate::SERVICE_BING, bing_key, false); + } + else + { + mBingAPIKeyEditor->setTentative(TRUE); + mBingKeyVerified = FALSE; + } + + std::string google_key = gSavedSettings.getString("GoogleTranslateAPIKey"); + if (!google_key.empty()) + { + mGoogleAPIKeyEditor->setText(google_key); + mGoogleAPIKeyEditor->setTentative(FALSE); + verifyKey(LLTranslate::SERVICE_GOOGLE, google_key, false); + } + else + { + mGoogleAPIKeyEditor->setTentative(TRUE); + mGoogleKeyVerified = FALSE; + } + + updateControlsEnabledState(); +} + +// virtual +BOOL LLPanelTranslationSettings::postBuild() +{ + mMachineTranslationCB = getChild("translate_chat_checkbox"); + mLanguageCombo = getChild("translate_language_combo"); + mTranslationServiceRadioGroup = getChild("translation_service_rg"); + mBingAPIKeyEditor = getChild("bing_api_key"); + mGoogleAPIKeyEditor = getChild("google_api_key"); + mBingVerifyBtn = getChild("verify_bing_api_key_btn"); + mGoogleVerifyBtn = getChild("verify_google_api_key_btn"); + + mMachineTranslationCB->setCommitCallback(boost::bind(&LLPanelTranslationSettings::updateControlsEnabledState, this)); + mTranslationServiceRadioGroup->setCommitCallback(boost::bind(&LLPanelTranslationSettings::updateControlsEnabledState, this)); + mBingVerifyBtn->setClickedCallback(boost::bind(&LLPanelTranslationSettings::onBtnBingVerify, this)); + mGoogleVerifyBtn->setClickedCallback(boost::bind(&LLPanelTranslationSettings::onBtnGoogleVerify, this)); + + mBingAPIKeyEditor->setFocusReceivedCallback(boost::bind(&LLPanelTranslationSettings::onEditorFocused, this, _1)); + mBingAPIKeyEditor->setKeystrokeCallback(boost::bind(&LLPanelTranslationSettings::onBingKeyEdited, this, _1)); + mGoogleAPIKeyEditor->setFocusReceivedCallback(boost::bind(&LLPanelTranslationSettings::onEditorFocused, this, _1)); + mGoogleAPIKeyEditor->setKeystrokeCallback(boost::bind(&LLPanelTranslationSettings::onGoogleKeyEdited, this, _1)); + + // Now set the links, because v3 has special xml linking and we do not. + LLTextEditor* bing_api_key = getChild("bing_api_key_label"); + LLTextEditor* google_api_key = getChild("google_api_key_label"); + LLTextEditor* google_links = getChild("google_links_text"); + google_api_key->setParseHTML(true); + bing_api_key->setParseHTML(true); + google_links->setParseHTML(true); + + LLStyleSP link_style(new LLStyle); + link_style->setColor(gSavedSettings.getColor4("HTMLLinkColor")); + std::string link_text; + std::string link_url; + + link_text = " AppId:"; + link_url = "https://datamarket.azure.com/dataset/1899a118-d202-492c-aa16-ba21c33c06cb"; //Microsoft changed their policy, no longer link to "http://www.bing.com/developers/createapp.aspx"; + link_style->setLinkHREF(link_url); + bing_api_key->appendStyledText(link_text, false, false, link_style); + + link_text = " API key:"; + link_url = "https://developers.google.com/translate/v2/getting_started#auth"; + link_style->setLinkHREF(link_url); + google_api_key->appendStyledText(link_text, false, false, link_style); + + link_text = getString("Pricing"); + link_url = "https://developers.google.com/translate/v2/pricing"; + link_style->setLinkHREF(link_url); + google_links->appendStyledText(link_text, false, false, link_style); + google_links->appendColoredText(std::string(" | "), false, false, gColors.getColor("TextFgReadOnlyColor")); + link_text = getString("Stats"); + link_url = "https://code.google.com/apis/console"; + link_style->setLinkHREF(link_url); + google_links->appendStyledText(link_text, false, false, link_style); + + //center(); + return TRUE; +} + +void LLPanelTranslationSettings::setBingVerified(bool ok, bool alert) +{ + if (alert) + { + showAlert(ok ? "bing_api_key_verified" : "bing_api_key_not_verified"); + } + + mBingKeyVerified = ok; + updateControlsEnabledState(); +} + +void LLPanelTranslationSettings::setGoogleVerified(bool ok, bool alert) +{ + if (alert) + { + showAlert(ok ? "google_api_key_verified" : "google_api_key_not_verified"); + } + + mGoogleKeyVerified = ok; + updateControlsEnabledState(); +} + +std::string LLPanelTranslationSettings::getSelectedService() const +{ + return mTranslationServiceRadioGroup->getSelectedValue().asString(); +} + +std::string LLPanelTranslationSettings::getEnteredBingKey() const +{ + return mBingAPIKeyEditor->getTentative() ? LLStringUtil::null : mBingAPIKeyEditor->getText(); +} + +std::string LLPanelTranslationSettings::getEnteredGoogleKey() const +{ + return mGoogleAPIKeyEditor->getTentative() ? LLStringUtil::null : mGoogleAPIKeyEditor->getText(); +} + +void LLPanelTranslationSettings::showAlert(const std::string& msg_name) const +{ + LLSD args; + args["MESSAGE"] = getString(msg_name); + LLNotificationsUtil::add("GenericAlert", args); +} + +void LLPanelTranslationSettings::updateControlsEnabledState() +{ + // Enable/disable controls based on the checkbox value. + bool on = mMachineTranslationCB->getValue().asBoolean(); + std::string service = getSelectedService(); + bool bing_selected = service == "bing"; + bool google_selected = service == "google"; + + mTranslationServiceRadioGroup->setEnabled(on); + mLanguageCombo->setEnabled(on); + + getChild("bing_api_key_label")->setVisible(bing_selected); + mBingAPIKeyEditor->setVisible(bing_selected); + mBingVerifyBtn->setVisible(bing_selected); + + getChild("google_api_key_label")->setVisible(google_selected); + getChild("google_links_text")->setVisible(google_selected); + mGoogleAPIKeyEditor->setVisible(google_selected); + mGoogleVerifyBtn->setVisible(google_selected); + + mBingAPIKeyEditor->setEnabled(on); + mGoogleAPIKeyEditor->setEnabled(on); + + mBingVerifyBtn->setEnabled(on && + !mBingKeyVerified && !getEnteredBingKey().empty()); + mGoogleVerifyBtn->setEnabled(on && + !mGoogleKeyVerified && !getEnteredGoogleKey().empty()); +} + +void LLPanelTranslationSettings::verifyKey(int service, const std::string& key, bool alert) +{ + LLTranslate::KeyVerificationReceiverPtr receiver = + new EnteredKeyVerifier((LLTranslate::EService) service, alert); + LLTranslate::verifyKey(receiver, key); +} + +void LLPanelTranslationSettings::onEditorFocused(LLFocusableElement* control) +{ + LLLineEditor* editor = dynamic_cast(control); + if (editor && editor->hasTabStop()) // if enabled. getEnabled() doesn't work + { + if (editor->getTentative()) + { + editor->setText(LLStringUtil::null); + editor->setTentative(FALSE); + } + } +} + +void LLPanelTranslationSettings::onBingKeyEdited(LLLineEditor* caller) +{ + if (caller->isDirty()) + { + setBingVerified(false, false); + } +} + +void LLPanelTranslationSettings::onGoogleKeyEdited(LLLineEditor* caller) +{ + if (caller->isDirty()) + { + setGoogleVerified(false, false); + } +} + +void LLPanelTranslationSettings::onBtnBingVerify() +{ + std::string key = getEnteredBingKey(); + if (!key.empty()) + { + verifyKey(LLTranslate::SERVICE_BING, key); + } +} + +void LLPanelTranslationSettings::onBtnGoogleVerify() +{ + std::string key = getEnteredGoogleKey(); + if (!key.empty()) + { + verifyKey(LLTranslate::SERVICE_GOOGLE, key); + } +} + +void LLPanelTranslationSettings::apply() +{ + gSavedSettings.setBOOL("TranslateChat", mMachineTranslationCB->getValue().asBoolean()); + gSavedSettings.setString("TranslateLanguage", mLanguageCombo->getSelectedValue().asString()); + gSavedSettings.setString("TranslationService", getSelectedService()); + gSavedSettings.setString("BingTranslateAPIKey", getEnteredBingKey()); + gSavedSettings.setString("GoogleTranslateAPIKey", getEnteredGoogleKey()); + LLFloaterChat::getInstance()->showTranslationCheckbox(LLTranslate::isTranslationConfigured()); +} + diff --git a/indra/newview/llpaneltranslationsettings.h b/indra/newview/llpaneltranslationsettings.h new file mode 100644 index 000000000..59c3da31b --- /dev/null +++ b/indra/newview/llpaneltranslationsettings.h @@ -0,0 +1,75 @@ +/** + * @file llpaneltranslationsettings.h + * @brief Machine translation settings for chat + * + * $LicenseInfo:firstyear=2011&license=viewerlgpl$ + * Second Life Viewer Source Code + * Copyright (C) 2011, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA + * $/LicenseInfo$ + */ + +#ifndef LL_LLPANELTRANSLATIONSETTINGS_H +#define LL_LLPANELTRANSLATIONSETTINGS_H + +#include "llpanel.h" + +class LLButton; +class LLCheckBoxCtrl; +class LLComboBox; +class LLLineEditor; +class LLRadioGroup; + +class LLPanelTranslationSettings : public LLPanel, public LLSingleton +{ +public: + LLPanelTranslationSettings(); + /*virtual*/ BOOL postBuild(); + + void apply(); + + void setBingVerified(bool ok, bool alert); + void setGoogleVerified(bool ok, bool alert); + +private: + std::string getSelectedService() const; + std::string getEnteredBingKey() const; + std::string getEnteredGoogleKey() const; + void showAlert(const std::string& msg_name) const; + void updateControlsEnabledState(); + void verifyKey(int service, const std::string& key, bool alert = true); + + void onEditorFocused(LLFocusableElement* control); + void onBingKeyEdited(LLLineEditor* caller); + void onGoogleKeyEdited(LLLineEditor* caller); + void onBtnBingVerify(); + void onBtnGoogleVerify(); + + LLCheckBoxCtrl* mMachineTranslationCB; + LLComboBox* mLanguageCombo; + LLLineEditor* mBingAPIKeyEditor; + LLLineEditor* mGoogleAPIKeyEditor; + LLRadioGroup* mTranslationServiceRadioGroup; + LLButton* mBingVerifyBtn; + LLButton* mGoogleVerifyBtn; + + bool mBingKeyVerified; + bool mGoogleKeyVerified; +}; + +#endif // LL_LLPANELTRANSLATIONSETTINGS_H diff --git a/indra/newview/lltranslate.cpp b/indra/newview/lltranslate.cpp index 833926afe..8cbabce02 100644 --- a/indra/newview/lltranslate.cpp +++ b/indra/newview/lltranslate.cpp @@ -2,136 +2,402 @@ * @file lltranslate.cpp * @brief Functions for translating text via Google Translate. * -* $LicenseInfo:firstyear=2009&license=viewergpl$ -* -* Copyright (c) 2009, Linden Research, Inc. -* -* Second Life Viewer Source Code -* The source code in this file ("Source Code") is provided by Linden Lab -* to you under the terms of the GNU General Public License, version 2.0 -* ("GPL"), unless you have obtained a separate licensing agreement -* ("Other License"), formally executed by you and Linden Lab. Terms of -* the GPL can be found in doc/GPL-license.txt in this distribution, or -* online at http://secondlifegrid.net/programs/open_source/licensing/gplv2 -* -* There are special exceptions to the terms and conditions of the GPL as -* it is applied to this Source Code. View the full text of the exception -* in the file doc/FLOSS-exception.txt in this software distribution, or -* online at -* http://secondlifegrid.net/programs/open_source/licensing/flossexception -* -* By copying, modifying or distributing this software, you acknowledge -* that you have read and understood your obligations described above, -* and agree to abide by those obligations. -* -* ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO -* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, -* COMPLETENESS OR PERFORMANCE. -* $/LicenseInfo$ -*/ + * $LicenseInfo:firstyear=2009&license=viewerlgpl$ + * Second Life Viewer Source Code + * Copyright (C) 2010, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA + * $/LicenseInfo$ + */ #include "llviewerprecompiledheaders.h" -#include "llbufferstream.h" #include "lltranslate.h" + +#include + +#include "llbufferstream.h" +#include "lltrans.h" #include "llui.h" -#include "sgversion.h" +#include "llviewercontrol.h" #include "llweb.h" -// -#include "llviewercontrol.h" -// +#include "sgversion.h" -// These two are concatenated with the language specifiers to form a complete Google Translate URL -const char* LLTranslate::m_GoogleURL = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q="; -const char* LLTranslate::m_GoogleLangSpec = "&langpair="; -float LLTranslate::m_GoogleTimeout = 10; +#include "json/reader.h" -AIHTTPHeaders LLTranslate::m_Header; -// These constants are for the GET header. -const char* LLTranslate::m_AcceptHeader = "Accept"; -const char* LLTranslate::m_AcceptType = "text/plain"; -const char* LLTranslate::m_AgentHeader = "User-Agent"; - -// These constants are in the JSON returned from Google -const char* LLTranslate::m_GoogleData = "responseData"; -const char* LLTranslate::m_GoogleTranslation = "translatedText"; -const char* LLTranslate::m_GoogleLanguage = "detectedSourceLanguage"; - -//static -void LLTranslate::translateMessage(LLHTTPClient::ResponderPtr &result, const std::string &fromLang, const std::string &toLang, const std::string &mesg) +// virtual +void LLGoogleTranslationHandler::getTranslateURL( + std::string &url, + const std::string &from_lang, + const std::string &to_lang, + const std::string &text) const { - std::string url; - getTranslateUrl(url, fromLang, toLang, mesg); - -// - std::string user_agent = gCurrentVersion; -// - - if (m_Header.empty()) + url = std::string("https://www.googleapis.com/language/translate/v2?key=") + + getAPIKey() + "&q=" + LLURI::escape(text) + "&target=" + to_lang; + if (!from_lang.empty()) { - m_Header.addHeader(m_AcceptHeader, m_AcceptType); - m_Header.addHeader(m_AgentHeader, user_agent); - } - - LLHTTPClient::get(url, result, m_Header, m_GoogleTimeout); -} - -//static -void LLTranslate::getTranslateUrl(std::string &translateUrl, const std::string &fromLang, const std::string &toLang, const std::string &mesg) -{ - std::string escaped_mesg = LLWeb::curlEscape(mesg); - - translateUrl = m_GoogleURL - + escaped_mesg + m_GoogleLangSpec - + fromLang // 'from' language; empty string for auto - + "%7C" // | - + toLang; // 'to' language -} - -//static -void LLTranslate::stringReplaceAll(std::string& context, const std::string& from, const std::string& to) -{ - size_t lookHere = 0; - size_t foundHere; - - while((foundHere = context.find(from, lookHere)) - != std::string::npos) { - context.replace(foundHere, from.size(), to); - lookHere = foundHere + to.size(); + url += "&source=" + from_lang; } } -//static -BOOL LLTranslate::parseGoogleTranslate(const std::string result, std::string &translation, std::string &detectedLanguage) +// virtual +void LLGoogleTranslationHandler::getKeyVerificationURL( + std::string& url, + const std::string& key) const +{ + url = std::string("https://www.googleapis.com/language/translate/v2/languages?key=") + + key + "&target=en"; +} + +// virtual +bool LLGoogleTranslationHandler::parseResponse( + int& status, + const std::string& body, + std::string& translation, + std::string& detected_lang, + std::string& err_msg) const { Json::Value root; Json::Reader reader; - BOOL parsingSuccessful = reader.parse(result, root ); - if ( !parsingSuccessful ) + + if (!reader.parse(body, root)) { - LL_WARNS("JSON") << reader.getFormatedErrorMessages() << LL_ENDL; - return FALSE; + err_msg = reader.getFormatedErrorMessages(); + return false; } - translation = root[m_GoogleData].get(m_GoogleTranslation, "").asString(); - detectedLanguage = root[m_GoogleData].get(m_GoogleLanguage, "").asString(); - return TRUE; + if (!root.isObject()) // empty response? should not happen + { + return false; + } + + if (status != STATUS_OK) + { + // Request failed. Extract error message from the response. + parseErrorResponse(root, status, err_msg); + return false; + } + + // Request succeeded, extract translation from the response. + return parseTranslation(root, translation, detected_lang); +} + +// virtual +bool LLGoogleTranslationHandler::isConfigured() const +{ + return !getAPIKey().empty(); +} + +// static +void LLGoogleTranslationHandler::parseErrorResponse( + const Json::Value& root, + int& status, + std::string& err_msg) +{ + const Json::Value& error = root.get("error", 0); + if (!error.isObject() || !error.isMember("message") || !error.isMember("code")) + { + return; + } + + err_msg = error["message"].asString(); + status = error["code"].asInt(); +} + +// static +bool LLGoogleTranslationHandler::parseTranslation( + const Json::Value& root, + std::string& translation, + std::string& detected_lang) +{ + // JsonCpp is prone to aborting the program on failed assertions, + // so be super-careful and verify the response format. + const Json::Value& data = root.get("data", 0); + if (!data.isObject() || !data.isMember("translations")) + { + return false; + } + + const Json::Value& translations = data["translations"]; + if (!translations.isArray() || translations.size() == 0) + { + return false; + } + + const Json::Value& first = translations[0U]; + if (!first.isObject() || !first.isMember("translatedText")) + { + return false; + } + + translation = first["translatedText"].asString(); + detected_lang = first.get("detectedSourceLanguage", "").asString(); + return true; +} + +// static +std::string LLGoogleTranslationHandler::getAPIKey() +{ + return gSavedSettings.getString("GoogleTranslateAPIKey"); +} + +// virtual +void LLBingTranslationHandler::getTranslateURL( + std::string &url, + const std::string &from_lang, + const std::string &to_lang, + const std::string &text) const +{ + url = std::string("http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=") + + getAPIKey() + "&text=" + LLURI::escape(text) + "&to=" + getAPILanguageCode(to_lang); + if (!from_lang.empty()) + { + url += "&from=" + getAPILanguageCode(from_lang); + } +} + +// virtual +void LLBingTranslationHandler::getKeyVerificationURL( + std::string& url, + const std::string& key) const +{ + url = std::string("http://api.microsofttranslator.com/v2/Http.svc/GetLanguagesForTranslate?appId=") + + key; +} + +// virtual +bool LLBingTranslationHandler::parseResponse( + int& status, + const std::string& body, + std::string& translation, + std::string& detected_lang, + std::string& err_msg) const +{ + if (status != STATUS_OK) + { + static const std::string MSG_BEGIN_MARKER = "Message: "; + size_t begin = body.find(MSG_BEGIN_MARKER); + if (begin != std::string::npos) + { + begin += MSG_BEGIN_MARKER.size(); + } + else + { + begin = 0; + err_msg.clear(); + } + size_t end = body.find("

", begin); + err_msg = body.substr(begin, end-begin); + LLStringUtil::replaceString(err_msg, " ", ""); // strip CR + return false; + } + + // Sample response: Hola + size_t begin = body.find(">"); + if (begin == std::string::npos || begin >= (body.size() - 1)) + { + begin = 0; + } + else + { + ++begin; + } + + size_t end = body.find("", begin); + + detected_lang = ""; // unsupported by this API + translation = body.substr(begin, end-begin); + LLStringUtil::replaceString(translation, " ", ""); // strip CR + return true; +} + +// virtual +bool LLBingTranslationHandler::isConfigured() const +{ + return !getAPIKey().empty(); +} + +// static +std::string LLBingTranslationHandler::getAPIKey() +{ + return gSavedSettings.getString("BingTranslateAPIKey"); +} + +// static +std::string LLBingTranslationHandler::getAPILanguageCode(const std::string& lang) +{ + return lang == "zh" ? "zh-CHT" : lang; // treat Chinese as Traditional Chinese +} + +LLTranslate::TranslationReceiver::TranslationReceiver(const std::string& from_lang, const std::string& to_lang) +: mFromLang(from_lang) +, mToLang(to_lang) +, mHandler(LLTranslate::getPreferredHandler()) +{ +} + +// virtual +void LLTranslate::TranslationReceiver::completedRaw( + const LLChannelDescriptors& channels, + const LLIOPipe::buffer_ptr_t& buffer) +{ + LLBufferStream istr(channels, buffer.get()); + std::stringstream strstrm; + strstrm << istr.rdbuf(); + + const std::string body = strstrm.str(); + std::string translation, detected_lang, err_msg; + int status = getStatus(); + LL_DEBUGS("Translate") << "HTTP status: " << status << " " << getReason() << LL_ENDL; + LL_DEBUGS("Translate") << "Response body: " << body << LL_ENDL; + if (mHandler.parseResponse(status, body, translation, detected_lang, err_msg)) + { + // Fix up the response + LLStringUtil::replaceString(translation, "<", "<"); + LLStringUtil::replaceString(translation, ">",">"); + LLStringUtil::replaceString(translation, ""","\""); + LLStringUtil::replaceString(translation, "'","'"); + LLStringUtil::replaceString(translation, "&","&"); + LLStringUtil::replaceString(translation, "'","'"); + + handleResponse(translation, detected_lang); + } + else + { + if (err_msg.empty()) + { + err_msg = LLTrans::getString("TranslationResponseParseError"); + } + + llwarns << "Translation request failed: " << err_msg << llendl; + handleFailure(status, err_msg); + } +} + +LLTranslate::KeyVerificationReceiver::KeyVerificationReceiver(EService service) +: mService(service) +{ +} + +LLTranslate::EService LLTranslate::KeyVerificationReceiver::getService() const +{ + return mService; +} + +// virtual +void LLTranslate::KeyVerificationReceiver::completedRaw( + const LLChannelDescriptors& channels, + const LLIOPipe::buffer_ptr_t& buffer) +{ + bool ok = (getStatus() == 200); + setVerificationStatus(ok); +} + +//static +void LLTranslate::translateMessage( + TranslationReceiverPtr &receiver, + const std::string &from_lang, + const std::string &to_lang, + const std::string &mesg) +{ + std::string url; + receiver->mHandler.getTranslateURL(url, from_lang, to_lang, mesg); + + LL_DEBUGS("Translate") << "Sending translation request: " << url << LL_ENDL; + sendRequest(url, receiver); +} + +// static +void LLTranslate::verifyKey( + KeyVerificationReceiverPtr& receiver, + const std::string& key) +{ + std::string url; + const LLTranslationAPIHandler& handler = getHandler(receiver->getService()); + handler.getKeyVerificationURL(url, key); + + LL_DEBUGS("Translate") << "Sending key verification request: " << url << LL_ENDL; + sendRequest(url, receiver); } //static std::string LLTranslate::getTranslateLanguage() { - std::string language = "en"; - if (LLUI::sConfigGroup) + std::string language = gSavedSettings.getString("TranslateLanguage"); + if (language.empty() || language == "default") { - language = LLUI::sConfigGroup->getString("TranslateLanguage"); - if (language.empty() || language == "default") - { - language = LLUI::getLanguage(); - } + language = LLUI::getLanguage(); } language = language.substr(0,2); return language; } +// static +bool LLTranslate::isTranslationConfigured() +{ + return getPreferredHandler().isConfigured(); +} + +// static +const LLTranslationAPIHandler& LLTranslate::getPreferredHandler() +{ + EService service = SERVICE_BING; + + std::string service_str = gSavedSettings.getString("TranslationService"); + if (service_str == "google") + { + service = SERVICE_GOOGLE; + } + + return getHandler(service); +} + +// static +const LLTranslationAPIHandler& LLTranslate::getHandler(EService service) +{ + static LLGoogleTranslationHandler google; + static LLBingTranslationHandler bing; + + if (service == SERVICE_GOOGLE) + { + return google; + } + + return bing; +} + +// static +void LLTranslate::sendRequest(const std::string& url, LLHTTPClient::ResponderPtr responder) +{ + static LLSD sHeader; + + if (!sHeader.size()) + { + std::string user_agent = llformat("%s %d.%d.%d (%d)", + gVersionChannel, + gVersionMajor, + gVersionMinor, + gVersionPatch, + gVersionBuild); + + sHeader.insert("Accept", "text/plain"); + sHeader.insert("User-Agent", user_agent); + } + + LLHTTPClient::get(url, sHeader, responder); +} diff --git a/indra/newview/lltranslate.h b/indra/newview/lltranslate.h index f5638b6e2..2deb1952d 100644 --- a/indra/newview/lltranslate.h +++ b/indra/newview/lltranslate.h @@ -2,127 +2,301 @@ * @file lltranslate.h * @brief Human language translation class and JSON response receiver. * -* $LicenseInfo:firstyear=2009&license=viewergpl$ -* -* Copyright (c) 2009, Linden Research, Inc. -* -* Second Life Viewer Source Code -* The source code in this file ("Source Code") is provided by Linden Lab -* to you under the terms of the GNU General Public License, version 2.0 -* ("GPL"), unless you have obtained a separate licensing agreement -* ("Other License"), formally executed by you and Linden Lab. Terms of -* the GPL can be found in doc/GPL-license.txt in this distribution, or -* online at http://secondlifegrid.net/programs/open_source/licensing/gplv2 -* -* There are special exceptions to the terms and conditions of the GPL as -* it is applied to this Source Code. View the full text of the exception -* in the file doc/FLOSS-exception.txt in this software distribution, or -* online at -* http://secondlifegrid.net/programs/open_source/licensing/flossexception -* -* By copying, modifying or distributing this software, you acknowledge -* that you have read and understood your obligations described above, -* and agree to abide by those obligations. -* -* ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO -* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, -* COMPLETENESS OR PERFORMANCE. -* $/LicenseInfo$ -*/ + * $LicenseInfo:firstyear=2009&license=viewerlgpl$ + * Second Life Viewer Source Code + * Copyright (C) 2010, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA + * $/LicenseInfo$ + */ #ifndef LL_LLTRANSLATE_H #define LL_LLTRANSLATE_H #include "llhttpclient.h" #include "llbufferstream.h" -#include "json/reader.h" +#include "lliopipe.h" -class AIHTTPTimeoutPolicy; -extern AIHTTPTimeoutPolicy translationReceiver_timeout; - -class LLTranslate +namespace Json { -public : - class TranslationReceiver : public LLHTTPClient::ResponderWithResult - { - protected: - TranslationReceiver(const std::string &fromLang, const std::string &toLang) - : m_fromLang(fromLang), - m_toLang(toLang) - { - } + class Value; +} - virtual void handleResponse(const std::string &translation, const std::string &recognizedLang) {} - virtual void handleFailure() {}; +/** + * Handler of an HTTP machine translation service. + * + * Derived classes know the service URL + * and how to parse the translation result. + */ +class LLTranslationAPIHandler +{ +public: + /** + * Get URL for translation of the given string. + * + * Sending HTTP GET request to the URL will initiate translation. + * + * @param[out] url Place holder for the result. + * @param from_lang Source language. Leave empty for auto-detection. + * @param to_lang Target language. + * @param text Text to translate. + */ + virtual void getTranslateURL( + std::string &url, + const std::string &from_lang, + const std::string &to_lang, + const std::string &text) const = 0; - public: - ~TranslationReceiver() - { - } + /** + * Get URL to verify the given API key. + * + * Sending request to the URL verifies the key. + * Positive HTTP response (code 200) means that the key is valid. + * + * @param[out] url Place holder for the URL. + * @param[in] key Key to verify. + */ + virtual void getKeyVerificationURL( + std::string &url, + const std::string &key) const = 0; - /*virtual*/ void httpFailure(void) - { - LL_WARNS("Translate") << "URL Request error: " << reason << LL_ENDL; - handleFailure(); - } + /** + * Parse translation response. + * + * @param[in,out] status HTTP status. May be modified while parsing. + * @param body Response text. + * @param[out] translation Translated text. + * @param[out] detected_lang Detected source language. May be empty. + * @param[out] err_msg Error message (in case of error). + */ + virtual bool parseResponse( + int& status, + const std::string& body, + std::string& translation, + std::string& detected_lang, + std::string& err_msg) const = 0; - /*virtual*/ void completedRaw( - LLChannelDescriptors const& channels, - LLIOPipe::buffer_ptr_t const& buffer) - { - LLBufferStream istr(channels, buffer.get()); + /** + * @return if the handler is configured to function properly + */ + virtual bool isConfigured() const = 0; - std::stringstream strstrm; - strstrm << istr.rdbuf(); - const std::string result = strstrm.str(); + virtual ~LLTranslationAPIHandler() {} - std::string translation; - std::string detectedLanguage; +protected: + static const int STATUS_OK = 200; +}; - if (!parseGoogleTranslate(result, translation, detectedLanguage)) - { - handleFailure(); - return; - } +/// Google Translate v2 API handler. +class LLGoogleTranslationHandler : public LLTranslationAPIHandler +{ + LOG_CLASS(LLGoogleTranslationHandler); - // Fix up the response - stringReplaceAll( translation, "<","<"); - stringReplaceAll( translation, ">",">"); - stringReplaceAll( translation, ""","\""); - stringReplaceAll( translation, "'","'"); - stringReplaceAll( translation, "&","&"); - stringReplaceAll( translation, "'","'"); - - handleResponse(translation, detectedLanguage); - } - - /*virtual*/ AIHTTPTimeoutPolicy const& getHTTPTimeoutPolicy(void) const { return translationReceiver_timeout; } - - protected: - const std::string m_toLang; - const std::string m_fromLang; - }; - - static void translateMessage(LLHTTPClient::ResponderPtr &result, const std::string &fromLang, const std::string &toLang, const std::string &mesg); - static float m_GoogleTimeout; - static std::string getTranslateLanguage(); +public: + /*virtual*/ void getTranslateURL( + std::string &url, + const std::string &from_lang, + const std::string &to_lang, + const std::string &text) const; + /*virtual*/ void getKeyVerificationURL( + std::string &url, + const std::string &key) const; + /*virtual*/ bool parseResponse( + int& status, + const std::string& body, + std::string& translation, + std::string& detected_lang, + std::string& err_msg) const; + /*virtual*/ bool isConfigured() const; private: - static void getTranslateUrl(std::string &translateUrl, const std::string &fromLang, const std::string &toLang, const std::string &text); - static void stringReplaceAll(std::string& context, const std::string& from, const std::string& to); - static BOOL parseGoogleTranslate(const std::string result, std::string &translation, std::string &detectedLanguage); + static void parseErrorResponse( + const Json::Value& root, + int& status, + std::string& err_msg); + static bool parseTranslation( + const Json::Value& root, + std::string& translation, + std::string& detected_lang); + static std::string getAPIKey(); +}; - static AIHTTPHeaders m_Header; - static const char* m_GoogleURL; - static const char* m_GoogleLangSpec; - static const char* m_AcceptHeader; - static const char* m_AcceptType; - static const char* m_AgentHeader; - static const char* m_UserAgent; +/// Microsoft Translator v2 API handler. +class LLBingTranslationHandler : public LLTranslationAPIHandler +{ + LOG_CLASS(LLBingTranslationHandler); - static const char* m_GoogleData; - static const char* m_GoogleTranslation; - static const char* m_GoogleLanguage; +public: + /*virtual*/ void getTranslateURL( + std::string &url, + const std::string &from_lang, + const std::string &to_lang, + const std::string &text) const; + /*virtual*/ void getKeyVerificationURL( + std::string &url, + const std::string &key) const; + /*virtual*/ bool parseResponse( + int& status, + const std::string& body, + std::string& translation, + std::string& detected_lang, + std::string& err_msg) const; + /*virtual*/ bool isConfigured() const; +private: + static std::string getAPIKey(); + static std::string getAPILanguageCode(const std::string& lang); +}; + +/** + * Entry point for machine translation services. + * + * Basically, to translate a string, we need to know the URL + * of a translation service, have a valid API for the service + * and be given the target language. + * + * Callers specify the string to translate and the target language, + * LLTranslate takes care of the rest. + * + * API keys for translation are taken from saved settings. + */ +class LLTranslate +{ + LOG_CLASS(LLTranslate); + +public : + + typedef enum e_service { + SERVICE_BING, + SERVICE_GOOGLE, + } EService; + + /** + * Subclasses are supposed to handle translation results (e.g. show them in chat) + */ + class TranslationReceiver: public LLHTTPClient::ResponderWithCompleted + { + public: + /** + * Using mHandler, parse incoming response. + * + * Calls either handleResponse() or handleFailure() + * depending on the HTTP status code and parsing success. + * + * @see handleResponse() + * @see handleFailure() + * @see mHandler + */ + /*virtual*/ void completedRaw( + const LLChannelDescriptors& channels, + const LLIOPipe::buffer_ptr_t& buffer); + + protected: + friend class LLTranslate; + + /// Remember source and target languages for subclasses to be able to filter inappropriate results. + TranslationReceiver(const std::string& from_lang, const std::string& to_lang); + + virtual void result(LLSD const& content) {}; + + /// Override point to handle successful translation. + virtual void handleResponse(const std::string &translation, const std::string &recognized_lang) = 0; + + /// Override point to handle unsuccessful translation. + virtual void handleFailure(int status, const std::string& err_msg) = 0; + + std::string mFromLang; + std::string mToLang; + const LLTranslationAPIHandler& mHandler; + }; + + /** + * Subclasses are supposed to handle API key verification result. + */ + class KeyVerificationReceiver: public LLHTTPClient::ResponderWithCompleted + { + public: + EService getService() const; + + protected: + virtual void result(LLSD const& content) {}; + /** + * Save the translation service the key belongs to. + * + * Subclasses need to know it. + * + * @see getService() + */ + KeyVerificationReceiver(EService service); + + /** + * Parse verification response. + * + * Calls setVerificationStatus() with the verification status, + * which is true if HTTP status code is 200. + * + * @see setVerificationStatus() + */ + /*virtual*/ void completedRaw( + const LLChannelDescriptors& channels, + const LLIOPipe::buffer_ptr_t& buffer); + + /** + * Override point for subclasses to handle key verification status. + */ + virtual void setVerificationStatus(bool ok) = 0; + + EService mService; + }; + + typedef boost::intrusive_ptr TranslationReceiverPtr; + typedef boost::intrusive_ptr KeyVerificationReceiverPtr; + + /** + * Translate given text. + * + * @param receiver Object to pass translation result to. + * @param from_lang Source language. Leave empty for auto-detection. + * @param to_lang Target language. + * @param mesg Text to translate. + */ + static void translateMessage(TranslationReceiverPtr &receiver, const std::string &from_lang, const std::string &to_lang, const std::string &mesg); + + /** + * Verify given API key of a translation service. + * + * @param receiver Object to pass verification result to. + * @param key Key to verify. + */ + static void verifyKey(KeyVerificationReceiverPtr& receiver, const std::string& key); + + /** + * @return translation target language + */ + static std::string getTranslateLanguage(); + + /** + * @return true if translation is configured properly. + */ + static bool isTranslationConfigured(); + +private: + static const LLTranslationAPIHandler& getPreferredHandler(); + static const LLTranslationAPIHandler& getHandler(EService service); + static void sendRequest(const std::string& url, LLHTTPClient::ResponderPtr responder); }; #endif diff --git a/indra/newview/llviewermessage.cpp b/indra/newview/llviewermessage.cpp index 2779f1404..e27aec30e 100644 --- a/indra/newview/llviewermessage.cpp +++ b/indra/newview/llviewermessage.cpp @@ -91,6 +91,7 @@ #include "llimview.h" #include "llspeakers.h" #include "lltrans.h" +#include "lltranslate.h" #include "llviewerfoldertype.h" #include "llviewergenericmessage.h" #include "llviewermenu.h" @@ -3612,52 +3613,55 @@ void process_decline_callingcard(LLMessageSystem* msg, void**) LLNotificationsUtil::add("CallingCardDeclined"); } -#if 0 // Google translate doesn't work anymore class ChatTranslationReceiver : public LLTranslate::TranslationReceiver { public : - ChatTranslationReceiver(const std::string &fromLang, const std::string &toLang, LLChat *chat, - const BOOL history) - : LLTranslate::TranslationReceiver(fromLang, toLang), + ChatTranslationReceiver(const std::string &from_lang, const std::string &to_lang, const std::string &mesg, + const LLChat &chat, const BOOL history) + : LLTranslate::TranslationReceiver(from_lang, to_lang), m_chat(chat), - m_history(history) + m_history(history), + m_origMesg(mesg) { } - static boost::intrusive_ptr build(const std::string &fromLang, const std::string &toLang, LLChat *chat, const BOOL history) + static boost::intrusive_ptr build(const std::string &from_lang, const std::string &to_lang, const std::string &mesg, const LLChat &chat, BOOL history) { - return boost::intrusive_ptr(new ChatTranslationReceiver(fromLang, toLang, chat, history)); + return boost::intrusive_ptr(new ChatTranslationReceiver(from_lang, to_lang, mesg, chat, history)); } protected: - void handleResponse(const std::string &translation, const std::string &detectedLanguage) - { - if (m_toLang != detectedLanguage) - m_chat->mText += " (" + translation + ")"; + void handleResponse(const std::string &translation, const std::string &detected_language) + { + // filter out non-interesting responeses + if ( !translation.empty() + && (mToLang != detected_language) + && (LLStringUtil::compareInsensitive(translation, m_origMesg) != 0) ) + { + m_chat.mText += " (" + translation + ")"; + } - add_floater_chat(*m_chat, m_history); - - delete m_chat; + add_floater_chat(m_chat, m_history); } - void handleFailure() + void handleFailure(int status, const std::string& err_msg) { - LLTranslate::TranslationReceiver::handleFailure(); + llwarns << "Translation failed for mesg " << m_origMesg << " toLang " << mToLang << " fromLang " << mFromLang << llendl; - m_chat->mText += " (?)"; + std::string msg = LLTrans::getString("TranslationFailed", LLSD().with("[REASON]", err_msg)); + LLStringUtil::replaceString(msg, "\n", " "); // we want one-line error messages + m_chat.mText += " (" + msg + ")"; - add_floater_chat(*m_chat, m_history); - - delete m_chat; + add_floater_chat(m_chat, m_history); } /*virtual*/ char const* getName(void) const { return "ChatTranslationReceiver"; } private: - LLChat *m_chat; - const BOOL m_history; + LLChat m_chat; + std::string m_origMesg; + BOOL m_history; }; -#endif void add_floater_chat(const LLChat &chat, const BOOL history) { @@ -3673,29 +3677,6 @@ void add_floater_chat(const LLChat &chat, const BOOL history) } } -#if 0 // Google translate doesn't work anymore -void check_translate_chat(const std::string &mesg, LLChat &chat, const BOOL history) -{ - const bool translate = LLUI::sConfigGroup->getBOOL("TranslateChat"); - - if (translate && chat.mSourceType != CHAT_SOURCE_SYSTEM) - { - // fromLang hardcoded to "" (autodetection) pending implementation of - // SVC-4879 - const std::string &fromLang = ""; - const std::string &toLang = LLTranslate::getTranslateLanguage(); - LLChat *newChat = new LLChat(chat); - - LLHTTPClient::ResponderPtr result = ChatTranslationReceiver::build(fromLang, toLang, newChat, history); - LLTranslate::translateMessage(result, fromLang, toLang, mesg); - } - else - { - add_floater_chat(chat, history); - } -} -#endif - // defined in llchatbar.cpp, but not declared in any header void send_chat_from_viewer(std::string utf8_out_text, EChatType type, S32 channel); @@ -4263,16 +4244,34 @@ void process_chat_from_simulator(LLMessageSystem *msg, void **user_data) chat.mPosAgent = chatter->getPositionAgent(); } + // truth table: + // LINDEN MUTED BUSY OWNED_BY_YOU TASK DISPLAY STORE IN HISTORY + // F T * * * No No + // F F T F * No Yes + // * F F * * Yes Yes + // * F * T * Yes Yes + // T * * * F Yes Yes + chat.mMuted = is_muted && !is_linden; bool only_history = visible_in_chat_bubble || (!is_linden && !is_owned_by_me && is_do_not_disturb); -#if 0 // Google translate doesn't work anymore if (!chat.mMuted) { - check_translate_chat(mesg, chat, only_history); + LLSD args; + if (chat.mSourceType != CHAT_SOURCE_SYSTEM && gSavedSettings.getBOOL("TranslateChat")) + { + if (ircstyle) + { + mesg.erase(4); + } + const std::string from_lang = ""; // leave empty to trigger autodetect + const std::string to_lang = LLTranslate::getTranslateLanguage(); + + LLTranslate::TranslationReceiverPtr result = ChatTranslationReceiver::build(from_lang, to_lang, mesg, chat, only_history); + LLTranslate::translateMessage(result, from_lang, to_lang, mesg); + return; + } } -#else add_floater_chat(chat, only_history); -#endif } } diff --git a/indra/newview/skins/default/xui/en-us/floater_chat_history.xml b/indra/newview/skins/default/xui/en-us/floater_chat_history.xml index f7b05c724..773a0e41a 100644 --- a/indra/newview/skins/default/xui/en-us/floater_chat_history.xml +++ b/indra/newview/skins/default/xui/en-us/floater_chat_history.xml @@ -16,7 +16,7 @@ Gestures - +