Skip to content

Commit 4e53101

Browse files
committed
fixed #1
1 parent 6134a70 commit 4e53101

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

GoogleTranslateApi/GoogleTranslate.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5-
using System.Threading.Tasks;
6-
using System.Xml;
75
using System.Net;
8-
using System.ComponentModel;
9-
using System.IO;
106

117
namespace GoogleTranslateApi
128
{
@@ -49,17 +45,18 @@ public Block this[int x]
4945
}
5046
}
5147

52-
48+
/// <summary>
49+
/// Creates a Block class
50+
/// </summary>
51+
/// <param name="data">The data string to be parsed</param>
52+
/// <exception cref="ArgumentException"></exception>
5353
public Block(string data)
5454
{
5555
if (!IsValidData(data))
5656
throw new ArgumentException("Invalid data string", "data");
57-
//int elements = data.Count(c => c == '[') + data.Count(c => c == ',')*2;
58-
//Data = new object[elements];
5957
List<object> ldata = new List<object>() { null };
6058
Queue<char> vs = new Queue<char>(data.ToCharArray());
6159

62-
//int datai = 0;
6360
while (vs.Count > 0)
6461
{
6562
char @char = vs.Dequeue();
@@ -77,11 +74,9 @@ public Block(string data)
7774
n--;
7875
}
7976
vs = new Queue<char>(nblock.Substring(end));
80-
//ldata[datai] = new Block(nblock.Substring(0, nblock.LastIndexOf(']')));
8177
ldata[ldata.Count - 1] = new Block(nblock.Substring(0, end - 1));
8278
break;
8379
case ',':
84-
//datai++
8580
ldata.Add(null);
8681
continue;
8782
case ']':
@@ -102,14 +97,12 @@ public Block(string data)
10297
@char = vs.Dequeue();
10398
break;
10499
}
105-
//Data[datai] = String.Concat(Data[datai], @char);
106100
ldata[ldata.Count - 1] = String.Concat(ldata[ldata.Count - 1], @char == '\\' ? vs.Dequeue() : @char);
107101
}
108102
while (vs.Peek() != '"');
109103
vs.Dequeue();
110104
break;
111105
default:
112-
//Data[datai] = String.Concat(Data[datai], @char);
113106
ldata[ldata.Count - 1] = String.Concat(ldata[ldata.Count - 1], @char);
114107
break;
115108
}
@@ -121,8 +114,6 @@ public Block(string data)
121114

122115
private static bool IsValidData(string data)
123116
{
124-
if (data == String.Empty)
125-
return false;
126117
if ((data.Count(c => c == '[') + data.Count(c => c == ']')) % 2 != 0)
127118
return false;
128119
if (data.Count(c => c == '"') % 2 != 0)
@@ -159,15 +150,21 @@ public Translate(Language source, Language target)
159150

160151
private string Download(string text)
161152
{
162-
WebClient web = new WebClient();
163-
web.Encoding = Encoding.UTF8;
153+
WebClient web = new WebClient
154+
{
155+
Encoding = Encoding.UTF8
156+
};
164157
//web.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0");
165158
//web.Headers.Add(HttpRequestHeader.AcceptCharset, "UTF-8");
166159
Uri uri = new Uri(this.Request + Uri.EscapeUriString(text));
167-
//web.DownloadFile(uri, "Tranlate.txt");
168160
return web.DownloadString(uri);
169161
}
170162

163+
/// <summary>
164+
/// Returns the translated text
165+
/// </summary>
166+
/// <param name="text">The text to be translated</param>
167+
/// <returns>A string that contains the translated text</returns>
171168
public string Text(string text)
172169
{
173170
string Dest = string.Empty;
@@ -180,7 +177,6 @@ public string Text(string text)
180177
Block splitData = Datablock[0][0][n];
181178
Dest = String.Concat(Dest, splitData.Data[0]);
182179
}
183-
//string Source = Datablock[0][0][0].Data[1] == null ? string.Empty : Datablock[0][0][0].Data[1].ToString();
184180
return Dest;
185181
}
186182
}
8.5 KB
Binary file not shown.
21.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)