Skip to content

Commit 2b0e471

Browse files
committed
Add project files.
1 parent 8741def commit 2b0e471

39 files changed

+4314
-0
lines changed

NLocalizer.sln

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26403.7
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLocalizer", "NLocalizer\NLocalizer.csproj", "{E55DF780-979D-492C-8536-68BC591BBC5B}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLocalizerTest", "NLocalizerTest\NLocalizerTest.csproj", "{5BC6E09D-50B3-4BB8-A296-D93662247D43}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{E55DF780-979D-492C-8536-68BC591BBC5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{E55DF780-979D-492C-8536-68BC591BBC5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{E55DF780-979D-492C-8536-68BC591BBC5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{E55DF780-979D-492C-8536-68BC591BBC5B}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{5BC6E09D-50B3-4BB8-A296-D93662247D43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{5BC6E09D-50B3-4BB8-A296-D93662247D43}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{5BC6E09D-50B3-4BB8-A296-D93662247D43}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{5BC6E09D-50B3-4BB8-A296-D93662247D43}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
EndGlobal

NLocalizer/Finder.cs

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
/*******************************************************************************
2+
3+
NLocalizer (C) 2010-2012 Chris Prusik (KAP1 Ltd www.kap1.net)
4+
The fast, simple solution for localizing .NET applications, by text files.
5+
Latest version: http://NLocalizer.codeplex.com/
6+
7+
$Id$
8+
9+
This library is free software; you can redistribute it and/or
10+
modify it under the terms of the GNU Lesser General Public
11+
License as published by the Free Software Foundation; either
12+
version 2.1 of the License, or (at your option) any later version.
13+
14+
This library is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
Lesser General Public License for more details.
18+
19+
*******************************************************************************/
20+
using System;
21+
22+
namespace NLocalizer
23+
{
24+
25+
/// <summary>
26+
/// Text finder static class.
27+
/// </summary>
28+
public static class Finder
29+
{
30+
/// <summary>
31+
/// Find searchText in text from begin pos.
32+
/// </summary>
33+
/// <param name="text">Searched text.</param>
34+
/// <param name="searchText">Text to find.</param>
35+
/// <param name="begin">Start cursor positon.</param>
36+
/// <param name="forward">Forward search (true) or reverse (false).</param>
37+
/// <returns></returns>
38+
public static int IndexOf(string text, string searchText, int begin, bool forward)
39+
{
40+
if (forward)
41+
return text.IndexOf(searchText, begin, StringComparison.Ordinal);
42+
while (begin >= 0)
43+
{
44+
if (text.Length >= begin + searchText.Length &&
45+
searchText == text.Substring(begin, searchText.Length))
46+
return begin;
47+
begin--;
48+
}
49+
return -1;
50+
}
51+
52+
/// <summary>
53+
/// Remove all string from text beetween before and after.
54+
/// </summary>
55+
/// <param name="text"></param>
56+
/// <param name="before"></param>
57+
/// <param name="after"></param>
58+
/// <returns></returns>
59+
public static string RemoveBeetweenWithout(string text, string before, string after)
60+
{
61+
int begin = 0;
62+
if (text != "")
63+
{
64+
if (before != "")
65+
{
66+
begin = text.IndexOf(before, begin, StringComparison.Ordinal);
67+
if (begin >= 0)
68+
begin = begin + before.Length;
69+
else
70+
begin = text.Length;
71+
}
72+
}
73+
74+
int end = text.Length;
75+
if (after != "")
76+
{
77+
end = text.IndexOf(after, Math.Min(begin + 1, text.Length - 1), StringComparison.Ordinal);
78+
if (end < 0)
79+
end = text.Length;
80+
}
81+
string result = text.Remove(begin, end - begin);
82+
return result;
83+
}
84+
85+
/// <summary>
86+
/// Remove all string from text beetween before and after.
87+
/// </summary>
88+
/// <param name="text"></param>
89+
/// <param name="before"></param>
90+
/// <param name="after"></param>
91+
/// <returns></returns>
92+
public static string RemoveBeetweenWithAll(string text, string before, string after)
93+
{
94+
string result = text;
95+
int len;
96+
do
97+
{
98+
len = result.Length;
99+
result = RemoveBeetweenWith(result, before, after);
100+
}
101+
while (len > result.Length);
102+
return result;
103+
}
104+
105+
/// <summary>
106+
/// Remove all string from text beetween before and after.
107+
/// </summary>
108+
/// <param name="text"></param>
109+
/// <param name="before"></param>
110+
/// <param name="after"></param>
111+
/// <returns></returns>
112+
public static string RemoveBeetweenWith(string text, string before, string after)
113+
{
114+
int begin = 0;
115+
if (text != "")
116+
{
117+
if (before != "")
118+
{
119+
begin = text.IndexOf(before, begin, StringComparison.Ordinal);
120+
if (begin < 0)
121+
begin = text.Length;
122+
}
123+
}
124+
125+
int end = text.Length;
126+
if (after != "")
127+
{
128+
end = text.IndexOf(after, Math.Min(begin + 1, text.Length - 1), StringComparison.Ordinal);
129+
if (end < 0)
130+
end = text.Length;
131+
}
132+
if (begin != text.Length || end != text.Length)
133+
return text.Remove(begin, end - begin + after.Length);
134+
return text;
135+
}
136+
137+
/// <summary>
138+
/// Find string beetween before and after in text.
139+
/// </summary>
140+
/// <param name="text">Searched text.</param>
141+
/// <param name="before"></param>
142+
/// <param name="after"></param>
143+
/// <param name="forward">Forward search (true) or reverse (false).</param>
144+
/// <returns></returns>
145+
public static string FindBeetween(string text, string before, string after, bool forward)
146+
{
147+
int begin = 0;
148+
return FindBeetween(text, before, after, ref begin, forward);
149+
}
150+
151+
/// <summary>
152+
/// Find string beetween before and after in text.
153+
/// </summary>
154+
/// <param name="text">Searched text.</param>
155+
/// <param name="before"></param>
156+
/// <param name="after"></param>
157+
/// <returns></returns>
158+
public static string FindBeetween(string text, string before, string after)
159+
{
160+
int begin = 0;
161+
return FindBeetween(text, before, after, ref begin, true);
162+
}
163+
164+
/// <summary>
165+
/// Find string beetween before and after in text.
166+
/// </summary>
167+
/// <param name="text">Searched text.</param>
168+
/// <param name="before"></param>
169+
/// <param name="after"></param>
170+
/// <param name="begin">Start cursor positon.</param>
171+
/// <returns></returns>
172+
public static string FindBeetween(string text, string before, string after, ref int begin)
173+
{
174+
return FindBeetween(text, before, after, ref begin, true);
175+
}
176+
177+
/// <summary>
178+
/// Find string beetween before and after in text.
179+
/// </summary>
180+
/// <param name="text">Searched text.</param>
181+
/// <param name="before"></param>
182+
/// <param name="after"></param>
183+
/// <param name="begin">Start cursor positon.</param>
184+
/// <param name="forward">Forward search (true) or reverse (false).</param>
185+
/// <returns></returns>
186+
public static string FindBeetween(string text, string before, string after, ref int begin, bool forward)
187+
{
188+
if (text != "")
189+
{
190+
if (before != "")
191+
{
192+
begin = IndexOf(text, before, Math.Min(begin, text.Length - 1), forward);
193+
if (begin >= 0)
194+
begin = begin + before.Length;
195+
else
196+
begin = text.Length;
197+
}
198+
}
199+
200+
int end = text.Length;
201+
if (after != "")
202+
{
203+
end = IndexOf(text, after, Math.Min(begin, text.Length - 1), forward);
204+
if (end < 0)
205+
end = text.Length;
206+
}
207+
string result = text.Substring(begin, end - begin);
208+
begin += result.Length;
209+
return result;
210+
}
211+
212+
/// <summary>
213+
/// Find string beetween before and after in text.
214+
/// </summary>
215+
/// <param name="text">Searched text.</param>
216+
/// <param name="before"></param>
217+
/// <param name="after"></param>
218+
/// <returns></returns>
219+
public static string FindBeetween(string text, string[] before, string after)
220+
{
221+
int begin = 0;
222+
return FindBeetween(text, before, after, ref begin, true);
223+
}
224+
225+
/// <summary>
226+
/// Find string beetween before and after in text.
227+
/// </summary>
228+
/// <param name="text">Searched text.</param>
229+
/// <param name="before"></param>
230+
/// <param name="after"></param>
231+
/// <param name="forward">Forward search (true) or reverse (false).</param>
232+
/// <returns></returns>
233+
public static string FindBeetween(string text, string[] before, string after, bool forward)
234+
{
235+
int begin = 0;
236+
return FindBeetween(text, before, after, ref begin, forward);
237+
}
238+
239+
/// <summary>
240+
/// Find string beetween before and after in text.
241+
/// </summary>
242+
/// <param name="text">Searched text.</param>
243+
/// <param name="before"></param>
244+
/// <param name="after"></param>
245+
/// <param name="begin">Start cursor positon.</param>
246+
/// <param name="forward">Forward search (true) or reverse (false).</param>
247+
/// <returns></returns>
248+
public static string FindBeetween(string text, string[] before, string after, ref int begin, bool forward)
249+
{
250+
int minPos = text.Length;
251+
string result = "";
252+
for (int i = 0; i < before.Length; i++)
253+
{
254+
int pos = begin;
255+
string tempResult = FindBeetween(text, before[i], after, ref pos, forward);
256+
if (pos < minPos)
257+
{
258+
result = tempResult;
259+
minPos = pos;
260+
}
261+
}
262+
begin = minPos;
263+
return result;
264+
}
265+
}
266+
}

NLocalizer/GoogleTranslator.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*******************************************************************************
2+
3+
NLocalizer (C) 2010-2012 Chris Prusik (KAP1 Ltd www.kap1.net)
4+
The fast, simple solution for localizing .NET applications, by text files.
5+
Latest version: http://NLocalizer.codeplex.com/
6+
7+
$Id$
8+
9+
This library is free software; you can redistribute it and/or
10+
modify it under the terms of the GNU Lesser General Public
11+
License as published by the Free Software Foundation; either
12+
version 2.1 of the License, or (at your option) any later version.
13+
14+
This library is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
Lesser General Public License for more details.
18+
19+
*******************************************************************************/
20+
using System;
21+
using System.Net;
22+
using System.Text;
23+
using System.Globalization;
24+
using System.Xml;
25+
26+
namespace NLocalizer
27+
{
28+
/// <summary>
29+
/// Automatic translation by http://translator.google.com
30+
/// </summary>
31+
public static class GoogleTranslator
32+
{
33+
/// <summary>
34+
/// Translates the text.
35+
/// </summary>
36+
/// <param name="input">The input.</param>
37+
/// <param name="sourceLanguage">Source language or locale.</param>
38+
/// <param name="destinationlanguage">Destination language or locale.</param>
39+
/// <returns></returns>
40+
public static string Translate(string input, string sourceLanguage, string destinationlanguage)
41+
{
42+
string sourcePair = "";
43+
string destinationPair = "";
44+
foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.AllCultures))
45+
{
46+
if (String.Compare(info.Name, sourceLanguage, StringComparison.OrdinalIgnoreCase) == 0 ||
47+
String.Compare(info.NativeName, sourceLanguage, StringComparison.OrdinalIgnoreCase) == 0 ||
48+
String.Compare(info.EnglishName, sourceLanguage, StringComparison.OrdinalIgnoreCase) == 0 ||
49+
String.Compare(info.DisplayName, sourceLanguage, StringComparison.OrdinalIgnoreCase) == 0 ||
50+
String.Compare(info.TwoLetterISOLanguageName, sourceLanguage, StringComparison.OrdinalIgnoreCase) == 0)
51+
sourcePair = info.TwoLetterISOLanguageName;
52+
53+
if (String.Compare(info.Name, destinationlanguage, StringComparison.OrdinalIgnoreCase) == 0 ||
54+
String.Compare(info.NativeName, destinationlanguage, StringComparison.OrdinalIgnoreCase) == 0 ||
55+
String.Compare(info.EnglishName, destinationlanguage, StringComparison.OrdinalIgnoreCase) == 0 ||
56+
String.Compare(info.DisplayName, destinationlanguage, StringComparison.OrdinalIgnoreCase) == 0 ||
57+
String.Compare(info.TwoLetterISOLanguageName, destinationlanguage, StringComparison.OrdinalIgnoreCase) == 0)
58+
destinationPair = info.TwoLetterISOLanguageName;
59+
if (sourcePair != "" && destinationPair != "")
60+
break;
61+
}
62+
return Translate(input, sourcePair + "|" + destinationPair);
63+
}
64+
65+
/// <summary>
66+
/// Translate Text using Google Translate API's
67+
/// Google URL - http://translate.google.com
68+
/// </summary>
69+
/// <param name="input">Input string</param>
70+
/// <param name="languagePair">2 letter Language Pair, delimited by "|".
71+
/// E.g. "ar|en" language pair means to translate from Arabic to English</param>
72+
/// <returns>Translated to String</returns>
73+
public static string Translate(string input, string languagePair)
74+
{
75+
string url = $"http://www.google.com/translate_t?hl=en&ie=UTF8&text={input}&langpair={languagePair}";
76+
var web = new WebClient();
77+
string html = web.DownloadString(url);
78+
string charset = Finder.FindBeetween(html, "charset=", "\"").Trim();
79+
byte[] converted = Encoding.Convert(Encoding.GetEncoding(charset), Encoding.UTF8, web.DownloadData(url));
80+
html = Encoding.UTF8.GetString(converted);
81+
string result = html.Substring(html.IndexOf("<span title=\"", StringComparison.Ordinal) + "<span title=\"".Length);
82+
result = result.Substring(result.IndexOf(">", StringComparison.Ordinal) + 1);
83+
result = result.Substring(0, result.IndexOf("</span>", StringComparison.Ordinal));
84+
var str = new StringBuilder();
85+
str.AppendLine($"<?xml version=\"1.0\" encoding=\"{charset}\" ?>");
86+
str.AppendLine("<html>");
87+
str.AppendLine("<head>");
88+
str.AppendLine("<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" />");
89+
str.AppendLine("</head>");
90+
str.AppendLine("<body>");
91+
str.AppendLine(result);
92+
str.AppendLine("</body>");
93+
str.AppendLine("</html>");
94+
var xml = new XmlDocument();
95+
xml.LoadXml(str.ToString());
96+
var xmlNode = xml.SelectSingleNode("/html/body");
97+
if (xmlNode != null) return xmlNode.InnerText.Trim();
98+
return input;
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)