diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/CMakeLists.txt --- a/indra/newview/CMakeLists.txt Fri Sep 02 13:48:29 2011 -0400 +++ b/indra/newview/CMakeLists.txt Fri Sep 16 00:38:17 2011 +0300 @@ -237,6 +237,7 @@ llfloatertools.cpp llfloatertopobjects.cpp llfloatertos.cpp + llfloatertranslationsettings.cpp llfloateruipreview.cpp llfloaterurlentry.cpp llfloatervoiceeffect.cpp @@ -799,6 +800,7 @@ llfloatertools.h llfloatertopobjects.h llfloatertos.h + llfloatertranslationsettings.h llfloateruipreview.h llfloaterurlentry.h llfloatervoiceeffect.h @@ -1983,12 +1985,19 @@ llmediadataclient.cpp lllogininstance.cpp llremoteparcelrequest.cpp + lltranslate.cpp llviewerhelputil.cpp llversioninfo.cpp llworldmap.cpp llworldmipmap.cpp ) + set_source_files_properties( + lltranslate.cpp + PROPERTIES + LL_TEST_ADDITIONAL_LIBRARIES "${JSONCPP_LIBRARIES}" + ) + ################################################## # DISABLING PRECOMPILED HEADERS USAGE FOR TESTS ################################################## diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/app_settings/settings.xml --- a/indra/newview/app_settings/settings.xml Fri Sep 02 13:48:29 2011 -0400 +++ b/indra/newview/app_settings/settings.xml Fri Sep 16 00:38:17 2011 +0300 @@ -10922,6 +10922,39 @@ 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 + + TutorialURL Comment diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/llfloaterpreference.h --- a/indra/newview/llfloaterpreference.h Fri Sep 02 13:48:29 2011 -0400 +++ b/indra/newview/llfloaterpreference.h Fri Sep 16 00:38:17 2011 +0300 @@ -157,6 +157,7 @@ void onChangeMaturity(); void onClickBlockList(); void onClickProxySettings(); + void onClickTranslationSettings(); void applyUIColor(LLUICtrl* ctrl, const LLSD& param); void getUIColor(LLUICtrl* ctrl, const LLSD& param); diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/llfloaterpreference.cpp --- a/indra/newview/llfloaterpreference.cpp Fri Sep 02 13:48:29 2011 -0400 +++ b/indra/newview/llfloaterpreference.cpp Fri Sep 16 00:38:17 2011 +0300 @@ -345,6 +345,7 @@ mCommitCallbackRegistrar.add("Pref.MaturitySettings", boost::bind(&LLFloaterPreference::onChangeMaturity, this)); mCommitCallbackRegistrar.add("Pref.BlockList", boost::bind(&LLFloaterPreference::onClickBlockList, this)); mCommitCallbackRegistrar.add("Pref.Proxy", boost::bind(&LLFloaterPreference::onClickProxySettings, this)); + mCommitCallbackRegistrar.add("Pref.TranslationSettings", boost::bind(&LLFloaterPreference::onClickTranslationSettings, this)); sSkin = gSavedSettings.getString("SkinCurrent"); @@ -602,6 +603,9 @@ } // hide joystick pref floater LLFloaterReg::hideInstance("pref_joystick"); + + // hide translation settings floater + LLFloaterReg::hideInstance("prefs_translation"); // cancel hardware menu LLFloaterHardwareSettings* hardware_settings = LLFloaterReg::getTypedInstance("prefs_hardware_settings"); @@ -1553,6 +1557,11 @@ LLFloaterReg::showInstance("prefs_proxy"); } +void LLFloaterPreference::onClickTranslationSettings() +{ + LLFloaterReg::showInstance("prefs_translation"); +} + void LLFloaterPreference::updateDoubleClickControls() { // check is one of double-click actions settings enabled diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/llfloatertranslationsettings.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/indra/newview/llfloatertranslationsettings.h Fri Sep 16 00:38:17 2011 +0300 @@ -0,0 +1,76 @@ +/** + * @file llfloatertranslationsettings.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_LLFLOATERTRANSLATIONSETTINGS_H +#define LL_LLFLOATERTRANSLATIONSETTINGS_H + +#include "llfloater.h" + +class LLButton; +class LLCheckBoxCtrl; +class LLComboBox; +class LLLineEditor; +class LLRadioGroup; + +class LLFloaterTranslationSettings : public LLFloater +{ +public: + LLFloaterTranslationSettings(const LLSD& key); + /*virtual*/ BOOL postBuild(); + /*virtual*/ void onOpen(const LLSD& key); + + 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(); + void onGoogleKeyEdited(); + void onBtnBingVerify(); + void onBtnGoogleVerify(); + void onBtnOK(); + + LLCheckBoxCtrl* mMachineTranslationCB; + LLComboBox* mLanguageCombo; + LLLineEditor* mBingAPIKeyEditor; + LLLineEditor* mGoogleAPIKeyEditor; + LLRadioGroup* mTranslationServiceRadioGroup; + LLButton* mBingVerifyBtn; + LLButton* mGoogleVerifyBtn; + LLButton* mOKBtn; + + bool mBingKeyVerified; + bool mGoogleKeyVerified; +}; + +#endif // LL_LLFLOATERTRANSLATIONSETTINGS_H diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/llfloatertranslationsettings.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/indra/newview/llfloatertranslationsettings.cpp Fri Sep 16 00:38:17 2011 +0300 @@ -0,0 +1,296 @@ +/** + * @file llfloatertranslationsettings.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 "llfloatertranslationsettings.h" + +// Viewer includes +#include "lltranslate.h" +#include "llviewercontrol.h" // for gSavedSettings + +// Linden library includes +#include "llbutton.h" +#include "llcheckboxctrl.h" +#include "llcombobox.h" +#include "llfloaterreg.h" +#include "lllineeditor.h" +#include "llnotificationsutil.h" +#include "llradiogroup.h" + +class EnteredKeyVerifier : public LLTranslate::KeyVerificationReceiver +{ +public: + EnteredKeyVerifier(LLTranslate::EService service, bool alert) + : LLTranslate::KeyVerificationReceiver(service) + , mAlert(alert) + { + } + +private: + /*virtual*/ void setVerificationStatus(bool ok) + { + LLFloaterTranslationSettings* floater = + LLFloaterReg::getTypedInstance("prefs_translation"); + + if (!floater) + { + llwarns << "Cannot find translation settings floater" << llendl; + return; + } + + switch (getService()) + { + case LLTranslate::SERVICE_BING: + floater->setBingVerified(ok, mAlert); + break; + case LLTranslate::SERVICE_GOOGLE: + floater->setGoogleVerified(ok, mAlert); + break; + } + } + + bool mAlert; +}; + +LLFloaterTranslationSettings::LLFloaterTranslationSettings(const LLSD& key) +: LLFloater(key) +, mMachineTranslationCB(NULL) +, mLanguageCombo(NULL) +, mTranslationServiceRadioGroup(NULL) +, mBingAPIKeyEditor(NULL) +, mGoogleAPIKeyEditor(NULL) +, mBingVerifyBtn(NULL) +, mGoogleVerifyBtn(NULL) +, mOKBtn(NULL) +, mBingKeyVerified(false) +, mGoogleKeyVerified(false) +{ +} + +// virtual +BOOL LLFloaterTranslationSettings::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"); + mOKBtn = getChild("ok_btn"); + + mMachineTranslationCB->setCommitCallback(boost::bind(&LLFloaterTranslationSettings::updateControlsEnabledState, this)); + mTranslationServiceRadioGroup->setCommitCallback(boost::bind(&LLFloaterTranslationSettings::updateControlsEnabledState, this)); + mOKBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnOK, this)); + getChild("cancel_btn")->setClickedCallback(boost::bind(&LLFloater::closeFloater, this, false)); + mBingVerifyBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnBingVerify, this)); + mGoogleVerifyBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnGoogleVerify, this)); + + mBingAPIKeyEditor->setFocusReceivedCallback(boost::bind(&LLFloaterTranslationSettings::onEditorFocused, this, _1)); + mBingAPIKeyEditor->setKeystrokeCallback(boost::bind(&LLFloaterTranslationSettings::onBingKeyEdited, this), NULL); + mGoogleAPIKeyEditor->setFocusReceivedCallback(boost::bind(&LLFloaterTranslationSettings::onEditorFocused, this, _1)); + mGoogleAPIKeyEditor->setKeystrokeCallback(boost::bind(&LLFloaterTranslationSettings::onGoogleKeyEdited, this), NULL); + + center(); + return TRUE; +} + +// virtual +void LLFloaterTranslationSettings::onOpen(const LLSD& key) +{ + 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(); +} + +void LLFloaterTranslationSettings::setBingVerified(bool ok, bool alert) +{ + if (alert) + { + showAlert(ok ? "bing_api_key_verified" : "bing_api_key_not_verified"); + } + + mBingKeyVerified = ok; + updateControlsEnabledState(); +} + +void LLFloaterTranslationSettings::setGoogleVerified(bool ok, bool alert) +{ + if (alert) + { + showAlert(ok ? "google_api_key_verified" : "google_api_key_not_verified"); + } + + mGoogleKeyVerified = ok; + updateControlsEnabledState(); +} + +std::string LLFloaterTranslationSettings::getSelectedService() const +{ + return mTranslationServiceRadioGroup->getSelectedValue().asString(); +} + +std::string LLFloaterTranslationSettings::getEnteredBingKey() const +{ + return mBingAPIKeyEditor->getTentative() ? LLStringUtil::null : mBingAPIKeyEditor->getText(); +} + +std::string LLFloaterTranslationSettings::getEnteredGoogleKey() const +{ + return mGoogleAPIKeyEditor->getTentative() ? LLStringUtil::null : mGoogleAPIKeyEditor->getText(); +} + +void LLFloaterTranslationSettings::showAlert(const std::string& msg_name) const +{ + LLSD args; + args["MESSAGE"] = getString(msg_name); + LLNotificationsUtil::add("GenericAlert", args); +} + +void LLFloaterTranslationSettings::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")->setEnabled(on); + mBingAPIKeyEditor->setEnabled(on); + + getChild("google_api_key_label")->setEnabled(on); + mGoogleAPIKeyEditor->setEnabled(on); + + mBingAPIKeyEditor->setEnabled(on && bing_selected); + mGoogleAPIKeyEditor->setEnabled(on && google_selected); + + mBingVerifyBtn->setEnabled(on && bing_selected && + !mBingKeyVerified && !getEnteredBingKey().empty()); + mGoogleVerifyBtn->setEnabled(on && google_selected && + !mGoogleKeyVerified && !getEnteredGoogleKey().empty()); + + mOKBtn->setEnabled( + !on || ( + (bing_selected && mBingKeyVerified) || + (google_selected && mGoogleKeyVerified) + )); +} + +void LLFloaterTranslationSettings::verifyKey(int service, const std::string& key, bool alert) +{ + LLTranslate::KeyVerificationReceiverPtr receiver = + new EnteredKeyVerifier((LLTranslate::EService) service, alert); + LLTranslate::verifyKey(receiver, key); +} + +void LLFloaterTranslationSettings::onEditorFocused(LLFocusableElement* control) +{ + LLLineEditor* editor = dynamic_cast(control); + if (editor) + { + if (editor->getTentative()) + { + editor->setText(LLStringUtil::null); + editor->setTentative(FALSE); + } + } +} + +void LLFloaterTranslationSettings::onBingKeyEdited() +{ + if (mBingAPIKeyEditor->isDirty()) + { + setBingVerified(false, false); + } +} + +void LLFloaterTranslationSettings::onGoogleKeyEdited() +{ + if (mGoogleAPIKeyEditor->isDirty()) + { + setGoogleVerified(false, false); + } +} + +void LLFloaterTranslationSettings::onBtnBingVerify() +{ + std::string key = getEnteredBingKey(); + if (!key.empty()) + { + verifyKey(LLTranslate::SERVICE_BING, key); + } +} + +void LLFloaterTranslationSettings::onBtnGoogleVerify() +{ + std::string key = getEnteredGoogleKey(); + if (!key.empty()) + { + verifyKey(LLTranslate::SERVICE_GOOGLE, key); + } +} + +void LLFloaterTranslationSettings::onBtnOK() +{ + gSavedSettings.setBOOL("TranslateChat", mMachineTranslationCB->getValue().asBoolean()); + gSavedSettings.setString("TranslateLanguage", mLanguageCombo->getSelectedValue().asString()); + gSavedSettings.setString("TranslationService", getSelectedService()); + gSavedSettings.setString("BingTranslateAPIKey", getEnteredBingKey()); + gSavedSettings.setString("GoogleTranslateAPIKey", getEnteredGoogleKey()); + closeFloater(false); +} diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/lltranslate.h --- a/indra/newview/lltranslate.h Fri Sep 02 13:48:29 2011 -0400 +++ b/indra/newview/lltranslate.h Fri Sep 16 00:38:17 2011 +0300 @@ -30,89 +30,257 @@ #include "llhttpclient.h" #include "llbufferstream.h" +namespace Json +{ + class Value; +} + +/** + * 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; + + /** + * 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; + + /** + * 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 ~LLTranslationAPIHandler() {} + +protected: + static const int STATUS_OK = 200; +}; + +/// Google Translate v2 API handler. +class LLGoogleTranslationHandler : public LLTranslationAPIHandler +{ + LOG_CLASS(LLGoogleTranslationHandler); + +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; + +private: + 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(); +}; + +/// Microsoft Translator v2 API handler. +class LLBingTranslationHandler : public LLTranslationAPIHandler +{ + LOG_CLASS(LLBingTranslationHandler); + +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; +private: + static std::string getAPIKey(); +}; + +/** + * 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::Responder { - protected: - TranslationReceiver(const std::string &from_lang, const std::string &to_lang) - : m_fromLang(from_lang), - m_toLang(to_lang) - { - } + public: - virtual void handleResponse(const std::string &translation, const std::string &recognized_lang) {}; - virtual void handleFailure() {}; - - public: - ~TranslationReceiver() - { - } - - virtual void completedRaw( U32 status, - const std::string& reason, - const LLChannelDescriptors& channels, - const LLIOPipe::buffer_ptr_t& buffer) - { - if (200 <= status && status < 300) - { - LLBufferStream istr(channels, buffer.get()); - std::stringstream strstrm; - strstrm << istr.rdbuf(); - - const std::string result = strstrm.str(); - std::string translation; - std::string detected_language; - - if (!parseGoogleTranslate(result, translation, detected_language)) - { - handleFailure(); - return; - } - - // 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_language); - } - else - { - LL_WARNS("Translate") << "HTTP request for Google Translate failed with status " << status << ", reason: " << reason << LL_ENDL; - handleFailure(); - } - } + /** + * 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( + U32 http_status, + const std::string& reason, + const LLChannelDescriptors& channels, + const LLIOPipe::buffer_ptr_t& buffer); protected: - const std::string m_toLang; - const std::string m_fromLang; + 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); + + /// 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; }; - static void translateMessage(LLHTTPClient::ResponderPtr &result, const std::string &from_lang, const std::string &to_lang, const std::string &mesg); - static float m_GoogleTimeout; + /** + * Subclasses are supposed to handle API key verification result. + */ + class KeyVerificationReceiver: public LLHTTPClient::Responder + { + public: + EService getService() const; + + protected: + /** + * 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( + U32 http_status, + const std::string& reason, + 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); static std::string getTranslateLanguage(); private: - static void getTranslateUrl(std::string &translate_url, const std::string &from_lang, const std::string &to_lang, const std::string &text); - static bool parseGoogleTranslate(const std::string& body, std::string &translation, std::string &detected_language); - - static LLSD 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; - - static const char* m_GoogleData; - static const char* m_GoogleTranslation; - static const char* m_GoogleLanguage; + static const LLTranslationAPIHandler& getPreferredHandler(); + static const LLTranslationAPIHandler& getHandler(EService service); + static void sendRequest(const std::string& url, LLHTTPClient::ResponderPtr responder); }; #endif diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/lltranslate.cpp --- a/indra/newview/lltranslate.cpp Fri Sep 02 13:48:29 2011 -0400 +++ b/indra/newview/lltranslate.cpp Fri Sep 16 00:38:17 2011 +0300 @@ -31,80 +31,292 @@ #include #include "llbufferstream.h" +#include "lltrans.h" #include "llui.h" #include "llversioninfo.h" #include "llviewercontrol.h" #include "reader.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 = 5; +// virtual +void LLGoogleTranslationHandler::getTranslateURL( + std::string &url, + const std::string &from_lang, + const std::string &to_lang, + const std::string &text) const +{ + url = std::string("https://www.googleapis.com/language/translate/v2?key=") + + getAPIKey() + "&q=" + LLURI::escape(text) + "&target=" + to_lang; + if (!from_lang.empty()) + { + url += "&source=" + from_lang; + } +} -LLSD 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"; +// 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"; +} -// 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"; +// 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; -//static -void LLTranslate::translateMessage(LLHTTPClient::ResponderPtr &result, const std::string &from_lang, const std::string &to_lang, const std::string &mesg) -{ - std::string url; - getTranslateUrl(url, from_lang, to_lang, mesg); - - std::string user_agent = llformat("%s %d.%d.%d (%d)", - LLVersionInfo::getChannel().c_str(), - LLVersionInfo::getMajor(), - LLVersionInfo::getMinor(), - LLVersionInfo::getPatch(), - LLVersionInfo::getBuild()); - - if (!m_Header.size()) + if (!reader.parse(body, root)) { - m_Header.insert(m_AcceptHeader, LLSD(m_AcceptType)); - m_Header.insert(m_AgentHeader, LLSD(user_agent)); + err_msg = reader.getFormatedErrorMessages(); + return false; } - LLHTTPClient::get(url, result, m_Header, m_GoogleTimeout); + 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); +} + +// 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=" + to_lang; + if (!from_lang.empty()) + { + url += "&from=" + 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; +} + +// static +std::string LLBingTranslationHandler::getAPIKey() +{ + return gSavedSettings.getString("BingTranslateAPIKey"); +} + +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( + U32 http_status, + const std::string& reason, + 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 = http_status; + LL_DEBUGS("Translate") << "HTTP status: " << status << " " << reason << 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( + U32 http_status, + const std::string& reason, + const LLChannelDescriptors& channels, + const LLIOPipe::buffer_ptr_t& buffer) +{ + bool ok = (http_status == 200); + setVerificationStatus(ok); } //static -void LLTranslate::getTranslateUrl(std::string &translate_url, const std::string &from_lang, const std::string &to_lang, const std::string &mesg) +void LLTranslate::translateMessage( + TranslationReceiverPtr &receiver, + const std::string &from_lang, + const std::string &to_lang, + const std::string &mesg) { - char * curl_str = curl_escape(mesg.c_str(), mesg.size()); - std::string const escaped_mesg(curl_str); - curl_free(curl_str); + std::string url; + receiver->mHandler.getTranslateURL(url, from_lang, to_lang, mesg); - translate_url = m_GoogleURL - + escaped_mesg + m_GoogleLangSpec - + from_lang // 'from' language; empty string for auto - + "%7C" // | - + to_lang; // 'to' language + LL_DEBUGS("Translate") << "Sending translation request: " << url << LL_ENDL; + sendRequest(url, receiver); } -//static -bool LLTranslate::parseGoogleTranslate(const std::string& body, std::string &translation, std::string &detected_language) +// static +void LLTranslate::verifyKey( + KeyVerificationReceiverPtr& receiver, + const std::string& key) { - Json::Value root; - Json::Reader reader; - - bool success = reader.parse(body, root); - if (!success) - { - LL_WARNS("Translate") << "Non valid response from Google Translate API: '" << reader.getFormatedErrorMessages() << "'" << LL_ENDL; - return false; - } - - translation = root[m_GoogleData].get(m_GoogleTranslation, "").asString(); - detected_language = root[m_GoogleData].get(m_GoogleLanguage, "").asString(); - return true; + 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 @@ -119,3 +331,52 @@ return language; } +// 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 const float REQUEST_TIMEOUT = 5; + static LLSD sHeader; + + if (!sHeader.size()) + { + std::string user_agent = llformat("%s %d.%d.%d (%d)", + LLVersionInfo::getChannel().c_str(), + LLVersionInfo::getMajor(), + LLVersionInfo::getMinor(), + LLVersionInfo::getPatch(), + LLVersionInfo::getBuild()); + + sHeader.insert("Accept", "text/plain"); + sHeader.insert("User-Agent", user_agent); + } + + LLHTTPClient::get(url, responder, sHeader, REQUEST_TIMEOUT); +} diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/llviewerfloaterreg.cpp --- a/indra/newview/llviewerfloaterreg.cpp Fri Sep 02 13:48:29 2011 -0400 +++ b/indra/newview/llviewerfloaterreg.cpp Fri Sep 16 00:38:17 2011 +0300 @@ -102,6 +102,7 @@ #include "llfloatertools.h" #include "llfloatertos.h" #include "llfloatertopobjects.h" +#include "llfloatertranslationsettings.h" #include "llfloateruipreview.h" #include "llfloatervoiceeffect.h" #include "llfloaterwhitelistentry.h" @@ -234,6 +235,7 @@ LLFloaterReg::add("preferences", "floater_preferences.xml", (LLFloaterBuildFunc)&LLFloaterReg::build); LLFloaterReg::add("prefs_proxy", "floater_preferences_proxy.xml", (LLFloaterBuildFunc)&LLFloaterReg::build); LLFloaterReg::add("prefs_hardware_settings", "floater_hardware_settings.xml", (LLFloaterBuildFunc)&LLFloaterReg::build); + LLFloaterReg::add("prefs_translation", "floater_translation_settings.xml", (LLFloaterBuildFunc)&LLFloaterReg::build); LLFloaterReg::add("perm_prefs", "floater_perm_prefs.xml", (LLFloaterBuildFunc)&LLFloaterReg::build); LLFloaterReg::add("pref_joystick", "floater_joystick.xml", (LLFloaterBuildFunc)&LLFloaterReg::build); LLFloaterReg::add("preview_anim", "floater_preview_animation.xml", (LLFloaterBuildFunc)&LLFloaterReg::build, "preview"); diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/llviewermessage.cpp --- a/indra/newview/llviewermessage.cpp Fri Sep 02 13:48:29 2011 -0400 +++ b/indra/newview/llviewermessage.cpp Fri Sep 16 00:38:17 2011 +0300 @@ -3138,7 +3138,7 @@ { // filter out non-interesting responeses if ( !translation.empty() - && (m_toLang != detected_language) + && (mToLang != detected_language) && (LLStringUtil::compareInsensitive(translation, m_origMesg) != 0) ) { m_chat.mText += " (" + translation + ")"; @@ -3147,10 +3147,11 @@ LLNotificationsUI::LLNotificationManager::instance().onChat(m_chat, m_toastArgs); } - void handleFailure() - { - LLTranslate::TranslationReceiver::handleFailure(); - m_chat.mText += " (?)"; + void handleFailure(int status, const std::string& err_msg) + { + 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 + ")"; LLNotificationsUI::LLNotificationManager::instance().onChat(m_chat, m_toastArgs); } @@ -3388,7 +3389,7 @@ const std::string from_lang = ""; // leave empty to trigger autodetect const std::string to_lang = LLTranslate::getTranslateLanguage(); - LLHTTPClient::ResponderPtr result = ChatTranslationReceiver::build(from_lang, to_lang, mesg, chat, args); + LLTranslate::TranslationReceiverPtr result = ChatTranslationReceiver::build(from_lang, to_lang, mesg, chat, args); LLTranslate::translateMessage(result, from_lang, to_lang, mesg); } else diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/skins/default/xui/en/floater_translation_settings.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/indra/newview/skins/default/xui/en/floater_translation_settings.xml Fri Sep 16 00:38:17 2011 +0300 @@ -0,0 +1,244 @@ + + + + Bing appID not verified. Please try again. + Google API key not verified. Please try again. + + Bing appID verified. + Google API key verified. + + + + Translate chat into: + + + + + + + + + + + + + + + + + + + + + + + + Choose translation service to use: + + + + + + + + + Bing [http://www.bing.com/developers/createapp.aspx AppID]: + + + \ No newline at end of file diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/skins/default/xui/en/strings.xml --- a/indra/newview/skins/default/xui/en/strings.xml Fri Sep 02 13:48:29 2011 -0400 +++ b/indra/newview/skins/default/xui/en/strings.xml Fri Sep 16 00:38:17 2011 +0300 @@ -3502,6 +3502,10 @@ Error parsing the external editor command. External editor failed to run. + + Translation failed: [REASON] + Error parsing translation response. + Esc Space diff -r 8da01486a36a -r 588d2bf7b6ad indra/newview/tests/lltranslate_test.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/indra/newview/tests/lltranslate_test.cpp Fri Sep 16 00:38:17 2011 +0300 @@ -0,0 +1,345 @@ +/** + * @file lltranslate_test.cpp + * + * $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 "linden_common.h" + +#include "../test/lltut.h" +#include "../lltranslate.h" +#include "../llversioninfo.h" +#include "../llviewercontrol.h" + +#include "llbufferstream.h" +#include "lltrans.h" +#include "llui.h" + +static const std::string GOOGLE_VALID_RESPONSE1 = +"{\ + \"data\": {\ + \"translations\": [\ + {\ + \"translatedText\": \"привет\",\ + \"detectedSourceLanguage\": \"es\"\ + }\ + ]\ + }\ +}"; + +static const std::string GOOGLE_VALID_RESPONSE2 = +"{\ + \"data\": {\ + \"translations\": [\ + {\ + \"translatedText\": \"привет\"\ + }\ + ]\ + }\ +}\ +"; + +static const std::string GOOGLE_VALID_RESPONSE3 = +"{\ + \"error\": {\ + \"errors\": [\ + {\ + \"domain\": \"global\",\ + \"reason\": \"invalid\",\ + \"message\": \"Invalid Value\"\ + }\ + ],\ + \"code\": 400,\ + \"message\": \"Invalid Value\"\ + }\ +}"; + +static const std::string BING_VALID_RESPONSE1 = +"Привет"; + +static const std::string BING_VALID_RESPONSE2 = +"

Argument Exception

Method: Translate()

Parameter:

\ +

Message: 'from' must be a valid language

\ +

message id=3743.V2_Rest.Translate.58E8454F

"; + +static const std::string BING_VALID_RESPONSE3 = +"

Argument Exception

Method: Translate()

\ +

Parameter: appId

Message: Invalid appId \nParameter name: appId

\ +

message id=3737.V2_Rest.Translate.56016759

"; + +namespace tut +{ + class translate_test + { + protected: + void test_translation( + LLTranslationAPIHandler& handler, + int status, const std::string& resp, + const std::string& exp_trans, const std::string& exp_lang, const std::string& exp_err) + { + std::string translation, detected_lang, err_msg; + bool rc = handler.parseResponse(status, resp, translation, detected_lang, err_msg); + ensure_equals("rc", rc, (status == 200)); + ensure_equals("err_msg", err_msg, exp_err); + ensure_equals("translation", translation, exp_trans); + ensure_equals("detected_lang", detected_lang, exp_lang); + } + + LLGoogleTranslationHandler mGoogle; + LLBingTranslationHandler mBing; + }; + + typedef test_group translate_test_group_t; + typedef translate_test_group_t::object translate_test_object_t; + tut::translate_test_group_t tut_translate("LLTranslate"); + + template<> template<> + void translate_test_object_t::test<1>() + { + test_translation(mGoogle, 200, GOOGLE_VALID_RESPONSE1, "привет", "es", ""); + } + + template<> template<> + void translate_test_object_t::test<2>() + { + test_translation(mGoogle, 200, GOOGLE_VALID_RESPONSE2, "привет", "", ""); + } + + template<> template<> + void translate_test_object_t::test<3>() + { + test_translation(mGoogle, 400, GOOGLE_VALID_RESPONSE3, "", "", "Invalid Value"); + } + + template<> template<> + void translate_test_object_t::test<4>() + { + test_translation(mGoogle, 400, + "", + "", "", "* Line 1, Column 1\n Syntax error: value, object or array expected.\n"); + } + + template<> template<> + void translate_test_object_t::test<5>() + { + test_translation(mGoogle, 400, + "[]", + "", "", ""); + } + + template<> template<> + void translate_test_object_t::test<6>() + { + test_translation(mGoogle, 400, + "{\"oops\": \"invalid\"}", + "", "", ""); + } + + template<> template<> + void translate_test_object_t::test<7>() + { + test_translation(mGoogle, 400, + "{\"data\": {}}", + "", "", ""); + } + + template<> template<> + void translate_test_object_t::test<8>() + { + test_translation(mGoogle, 400, + "{\"data\": { \"translations\": [ {} ] }}", + "", "", ""); + } + + template<> template<> + void translate_test_object_t::test<9>() + { + test_translation(mGoogle, 400, + "{\"data\": { \"translations\": [ { \"translatedTextZZZ\": \"привет\", \"detectedSourceLanguageZZZ\": \"es\" } ] }}", + "", "", ""); + } + + template<> template<> + void translate_test_object_t::test<10>() + { + test_translation(mBing, 200, BING_VALID_RESPONSE1, "Привет", "", ""); + } + + template<> template<> + void translate_test_object_t::test<11>() + { + test_translation(mBing, 400, BING_VALID_RESPONSE2, "", "", "'from' must be a valid language"); + } + + template<> template<> + void translate_test_object_t::test<12>() + { + test_translation(mBing, 400, BING_VALID_RESPONSE3, "", "", "Invalid appId\nParameter name: appId"); + } + + template<> template<> + void translate_test_object_t::test<13>() + { + test_translation(mBing, 200, + "Привет", + "Привет", "", ""); + } + + template<> template<> + void translate_test_object_t::test<14>() + { + test_translation(mBing, 200, + "Привет", + "Привет", "", ""); + } + + template<> template<> + void translate_test_object_t::test<15>() + { + test_translation(mBing, 200, + "Привет", + "Привет", "", ""); + } + + template<> template<> + void translate_test_object_t::test<16>() + { + test_translation(mBing, 400, + "Message: some error

", + "", "", "some error"); + } + + template<> template<> + void translate_test_object_t::test<17>() + { + test_translation(mBing, 400, + "Message: some error", + "", "", "some error"); + } + + template<> template<> + void translate_test_object_t::test<18>() + { + test_translation(mBing, 400, + "some error

", + "", "", "some error"); + } + + template<> template<> + void translate_test_object_t::test<19>() + { + test_translation(mBing, 400, + "some error", + "", "", "some error"); + } + + template<> template<> + void translate_test_object_t::test<20>() + { + std::string url; + mBing.getTranslateURL(url, "en", "es", "hi"); + ensure_equals("bing URL", url, + "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=dummy&text=hi&to=es&from=en"); + } + + template<> template<> + void translate_test_object_t::test<21>() + { + std::string url; + mBing.getTranslateURL(url, "", "es", "hi"); + ensure_equals("bing URL", url, + "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=dummy&text=hi&to=es"); + } + + template<> template<> + void translate_test_object_t::test<22>() + { + std::string url; + mGoogle.getTranslateURL(url, "en", "es", "hi"); + ensure_equals("google URL", url, + "https://www.googleapis.com/language/translate/v2?key=dummy&q=hi&target=es&source=en"); + } + + template<> template<> + void translate_test_object_t::test<23>() + { + std::string url; + mGoogle.getTranslateURL(url, "", "es", "hi"); + ensure_equals("google URL", url, + "https://www.googleapis.com/language/translate/v2?key=dummy&q=hi&target=es"); + } +} + +//== Misc stubs =============================================================== +LLControlGroup gSavedSettings("test"); + +std::string LLUI::getLanguage() { return "en"; } +std::string LLTrans::getString(const std::string &xml_desc, const LLStringUtil::format_map_t& args) { return "dummy"; } + +LLControlGroup::LLControlGroup(const std::string& name) : LLInstanceTracker(name) {} +std::string LLControlGroup::getString(const std::string& name) { return "dummy"; } +LLControlGroup::~LLControlGroup() {} + +namespace boost { + void intrusive_ptr_add_ref(LLCurl::Responder*) {} + void intrusive_ptr_release(LLCurl::Responder*) {} +} + +LLCurl::Responder::Responder() {} +void LLCurl::Responder::completedHeader(U32, std::string const&, LLSD const&) {} +void LLCurl::Responder::completedRaw(U32, const std::string&, const LLChannelDescriptors&, const LLIOPipe::buffer_ptr_t& buffer) {} +void LLCurl::Responder::completed(U32, std::string const&, LLSD const&) {} +void LLCurl::Responder::error(U32, std::string const&) {} +void LLCurl::Responder::errorWithContent(U32, std::string const&, LLSD const&) {} +void LLCurl::Responder::result(LLSD const&) {} +LLCurl::Responder::~Responder() {} + +void LLHTTPClient::get(const std::string&, const LLSD&, ResponderPtr, const LLSD&, const F32) {} +void LLHTTPClient::get(const std::string&, boost::intrusive_ptr, const LLSD&, const F32) {} + +LLBufferStream::LLBufferStream(const LLChannelDescriptors& channels, LLBufferArray* buffer) +: std::iostream(&mStreamBuf), mStreamBuf(channels, buffer) {} +LLBufferStream::~LLBufferStream() {} + +LLBufferStreamBuf::LLBufferStreamBuf(const LLChannelDescriptors&, LLBufferArray*) {} +#if( LL_WINDOWS || __GNUC__ > 2) +LLBufferStreamBuf::pos_type LLBufferStreamBuf::seekoff( + off_type off, + std::ios::seekdir way, + std::ios::openmode which) +#else +streampos LLBufferStreamBuf::seekoff( + streamoff off, + std::ios::seekdir way, + std::ios::openmode which) +#endif +{ return 0; } +int LLBufferStreamBuf::sync() {return 0;} +int LLBufferStreamBuf::underflow() {return 0;} +int LLBufferStreamBuf::overflow(int) {return 0;} +LLBufferStreamBuf::~LLBufferStreamBuf() {} + +S32 LLVersionInfo::getBuild() { return 0; } +const std::string& LLVersionInfo::getChannel() {static std::string dummy; return dummy;} +S32 LLVersionInfo::getMajor() { return 0; } +S32 LLVersionInfo::getMinor() { return 0; } +S32 LLVersionInfo::getPatch() { return 0; }