summaryrefslogtreecommitdiff
path: root/indra/newview/lltranslate.cpp
blob: e29ea373ce3246be9eb1a7b34179251e8b9f5cc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* @file lltranslate.cpp
* @brief Functions for translating text via Google Translate.
*
 * $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 "lltranslate.h"

#include <curl/curl.h>

#include "llbufferstream.h"
#include "llui.h"
#include "llversioninfo.h"
#include "llviewercontrol.h"

#include "reader.h"

class LLTranslationAPIHandler
{
public:
	virtual void getTranslateURL(
		std::string &url,
		const std::string &from_lang,
		const std::string &to_lang,
		const std::string &text) const = 0;

	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;
};

class LLGoogleV1Handler : public LLTranslationAPIHandler
{
	LOG_CLASS(LLGoogleV1Handler);

public:
	/*virtual*/ void getTranslateURL(
		std::string &url,
		const std::string &from_lang,
		const std::string &to_lang,
		const std::string &text) const
	{
		url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q="
			+ LLURI::escape(text)
			+ "&langpair=" + from_lang + "%7C" + to_lang;
	}

	/*virtual*/ bool 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;

		if (!reader.parse(body, root))
		{
			err_msg = reader.getFormatedErrorMessages();
			return false;
		}

		// This API doesn't return proper status in the HTTP response header,
		// but it is in the body.
		status = root["responseStatus"].asInt();
		if (status != STATUS_OK)
		{
			err_msg = root["responseDetails"].asString();
			return false;
		}

		const Json::Value& response_data = root["responseData"];
		translation = response_data.get("translatedText", "").asString();
		detected_lang = response_data.get("detectedSourceLanguage", "").asString();
		return true;
	}
};

class LLGoogleV2Handler : public LLTranslationAPIHandler
{
	LOG_CLASS(LLGoogleV2Handler);

public:
	/*virtual*/ void 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;
		}
	}

	/*virtual*/ bool 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;

		if (!reader.parse(body, root))
		{
			err_msg = reader.getFormatedErrorMessages();
			return false;
		}

		if (status != STATUS_OK)
		{
			const Json::Value& error = root["error"];
			err_msg = error["message"].asString();
			status = error["code"].asInt();
			return false;
		}

		const Json::Value& response_data = root["data"]["translations"][0U];
		translation = response_data["translatedText"].asString();
		detected_lang = response_data["detectedSourceLanguage"].asString();
		return true;
	}

private:
	static std::string getAPIKey()
	{
		return gSavedSettings.getString("GoogleTranslateAPIv2Key");
	}
};

class LLBingHandler : public LLTranslationAPIHandler
{
	LOG_CLASS(LLBingHandler);

public:
	/*virtual*/ void 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*/ bool parseResponse(
		int& status,
		const std::string& body,
		std::string& translation,
		std::string& detected_lang,
		std::string& err_msg) const
	{
		if (status != STATUS_OK)
		{
			size_t begin = body.find("Message: ");
			size_t end = body.find("</p>", begin);
			err_msg = body.substr(begin, end-begin);
			LLStringUtil::replaceString(err_msg, "&#xD;", ""); // strip CR
			return false;
		}

		// Sample response: <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hola</string>
		size_t begin = body.find(">");
		if (begin == std::string::npos || begin >= (body.size() - 1))
		{
			return false;
		}

		size_t end = body.find("</string>", ++begin);
		if (end == std::string::npos || end < begin)
		{
			return false;
		}

		detected_lang = ""; // unsupported by this API
		translation = body.substr(begin, end-begin);
		LLStringUtil::replaceString(translation, "&#xD;", ""); // strip CR
		return true;
	}

private:
	static std::string 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;
	if (mHandler.parseResponse(status, body, translation, detected_lang, err_msg))
	{
		// Fix up the response
		LLStringUtil::replaceString(translation, "&lt;", "<");
		LLStringUtil::replaceString(translation, "&gt;",">");
		LLStringUtil::replaceString(translation, "&quot;","\"");
		LLStringUtil::replaceString(translation, "&#39;","'");
		LLStringUtil::replaceString(translation, "&amp;","&");
		LLStringUtil::replaceString(translation, "&apos;","'");

		handleResponse(translation, detected_lang);
	}
	else
	{
		llwarns << "Translation request failed: " << err_msg << llendl;
		LL_DEBUGS("Translate") << "HTTP status: " << status << " " << reason << LL_ENDL;
		LL_DEBUGS("Translate") << "Error response body: " << body << LL_ENDL;
		handleFailure(status, err_msg);
	}
}

//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);

	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);
	}

	LL_DEBUGS("Translate") << "Sending translation request: " << url << LL_ENDL;
	LLHTTPClient::get(url, receiver, sHeader, REQUEST_TIMEOUT);
}

//static
std::string LLTranslate::getTranslateLanguage()
{
	std::string language = gSavedSettings.getString("TranslateLanguage");
	if (language.empty() || language == "default")
	{
		language = LLUI::getLanguage();
	}
	language = language.substr(0,2);
	return language;
}

// static
const LLTranslationAPIHandler& LLTranslate::getPreferredHandler()
{
	static LLGoogleV1Handler	google_v1;
	static LLGoogleV2Handler	google_v2;
	static LLBingHandler		bing;

	std::string service = gSavedSettings.getString("TranslationService");
	if (service == "google_v2")
	{
		return google_v2;
	}
	else if (service == "google_v1")
	{
		return google_v1;
	}

	return bing;
}