aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/newview/lltranslate.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'linden/indra/newview/lltranslate.cpp')
-rw-r--r--linden/indra/newview/lltranslate.cpp136
1 files changed, 136 insertions, 0 deletions
diff --git a/linden/indra/newview/lltranslate.cpp b/linden/indra/newview/lltranslate.cpp
new file mode 100644
index 0000000..625e543
--- /dev/null
+++ b/linden/indra/newview/lltranslate.cpp
@@ -0,0 +1,136 @@
1/**
2* @file lltranslate.cpp
3* @brief Functions for translating text via Google Translate.
4*
5* $LicenseInfo:firstyear=2009&license=viewergpl$
6*
7* Copyright (c) 2009, Linden Research, Inc.
8*
9* Second Life Viewer Source Code
10* The source code in this file ("Source Code") is provided by Linden Lab
11* to you under the terms of the GNU General Public License, version 2.0
12* ("GPL"), unless you have obtained a separate licensing agreement
13* ("Other License"), formally executed by you and Linden Lab. Terms of
14* the GPL can be found in doc/GPL-license.txt in this distribution, or
15* online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
16*
17* There are special exceptions to the terms and conditions of the GPL as
18* it is applied to this Source Code. View the full text of the exception
19* in the file doc/FLOSS-exception.txt in this software distribution, or
20* online at
21* http://secondlifegrid.net/programs/open_source/licensing/flossexception
22*
23* By copying, modifying or distributing this software, you acknowledge
24* that you have read and understood your obligations described above,
25* and agree to abide by those obligations.
26*
27* ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
28* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
29* COMPLETENESS OR PERFORMANCE.
30* $/LicenseInfo$
31*/
32
33#include "llviewerprecompiledheaders.h"
34
35#include "llbufferstream.h"
36#include "lltranslate.h"
37#include "llui.h"
38#include "llversionviewer.h"
39#include "llviewercontrol.h"
40
41// These two are concatenated with the language specifiers to form a complete Google Translate URL
42const char* LLTranslate::m_GoogleURL = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=";
43const char* LLTranslate::m_GoogleLangSpec = "&langpair=";
44float LLTranslate::m_GoogleTimeout = 10;
45
46LLSD LLTranslate::m_Header;
47// These constants are for the GET header.
48const char* LLTranslate::m_AcceptHeader = "Accept";
49const char* LLTranslate::m_AcceptType = "text/plain";
50const char* LLTranslate::m_AgentHeader = "User-Agent";
51
52// These constants are in the JSON returned from Google
53const char* LLTranslate::m_GoogleData = "responseData";
54const char* LLTranslate::m_GoogleTranslation = "translatedText";
55const char* LLTranslate::m_GoogleLanguage = "detectedSourceLanguage";
56
57//static
58void LLTranslate::translateMessage(LLHTTPClient::ResponderPtr &result, const std::string &fromLang, const std::string &toLang, const std::string &mesg)
59{
60 std::string url;
61 getTranslateUrl(url, fromLang, toLang, mesg);
62
63 std::string user_agent = llformat("%s %d.%d.%d (%d)",
64 gSavedSettings.getString("VersionChannelName").c_str(),
65 LL_VERSION_MAJOR,
66 LL_VERSION_MINOR,
67 LL_VERSION_PATCH,
68 LL_VERSION_BUILD );
69
70 if (!m_Header.size())
71 {
72 m_Header.insert(m_AcceptHeader, LLSD(m_AcceptType));
73 m_Header.insert(m_AgentHeader, LLSD(user_agent));
74 }
75
76 LLHTTPClient::get(url, result, m_Header, m_GoogleTimeout);
77}
78
79//static
80void LLTranslate::getTranslateUrl(std::string &translateUrl, const std::string &fromLang, const std::string &toLang, const std::string &mesg)
81{
82 std::string escaped_mesg = curl_escape(mesg.c_str(), mesg.size());
83
84 translateUrl = m_GoogleURL
85 + escaped_mesg + m_GoogleLangSpec
86 + fromLang // 'from' language; empty string for auto
87 + "%7C" // |
88 + toLang; // 'to' language
89}
90
91//static
92void LLTranslate::stringReplaceAll(std::string& context, const std::string& from, const std::string& to)
93{
94 size_t lookHere = 0;
95 size_t foundHere;
96
97 while((foundHere = context.find(from, lookHere))
98 != std::string::npos) {
99 context.replace(foundHere, from.size(), to);
100 lookHere = foundHere + to.size();
101 }
102}
103
104//static
105BOOL LLTranslate::parseGoogleTranslate(const std::string result, std::string &translation, std::string &detectedLanguage)
106{
107 Json::Value root;
108 Json::Reader reader;
109 BOOL parsingSuccessful = reader.parse(result, root );
110 if ( !parsingSuccessful )
111 {
112 LL_WARNS("JSON") << reader.getFormatedErrorMessages() << LL_ENDL;
113 return FALSE;
114 }
115
116 translation = root[m_GoogleData].get(m_GoogleTranslation, "").asString();
117 detectedLanguage = root[m_GoogleData].get(m_GoogleLanguage, "").asString();
118 return TRUE;
119}
120
121//static
122std::string LLTranslate::getTranslateLanguage()
123{
124 std::string language = "en";
125 if (LLUI::sConfigGroup)
126 {
127 language = LLUI::sConfigGroup->getString("TranslateLanguage");
128 if (language.empty() || language == "default")
129 {
130 language = LLUI::getLanguage();
131 }
132 }
133 language = language.substr(0,2);
134 return language;
135}
136