. , , , Google, , Google API Translate. WPF Json.NET, . !
, ( , ) . :
1. Google API Translate.
2. WPF .
3. C# Google.
4. WPF C#.
Google API Translate
, , google, . google , .
<a href="http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=">http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=</a>[ ]&langpair=[ ]
REST-, API -. , , REST.
Google :
{"responseData": {"translatedText":"[ ]"}, "responseDetails": null, "responseStatus": [ ]}
JSON ( , ). . JSON-, Json.NET. : http://json.codeplex.com. , . , , Json.NET.
WPF-
, WPF-. TextBlock, TextBox . 1.
. 1
, WPF, , - , . , ,
, . , , .
:
Stream GetHttpStream(string url). url.
String ConvertStreamTostring(Stream stmSource). stmSource .
void GetTranslateDirection (). , .. , . .
, C# 101 C# Samples. , .
4 :
HttpWebResponse, google ;
, bEnRus. 2- : . . , , GetTranslateDirection().
strOriginal;
strResult;
, , Translate, . , .
. , . TextBox ClassTranslator. Text ( , ) TextBox . Text. , Text . ( ), INotifyPropertyChanged. ( TextBox) . 1 : PropertyChanged.
:
class ClassTranslator: INotifyPropertyChanged { String strOriginal; String strResult; Boolean bEnRus; HttpWebResponse wresScrape; /// <summary> /// /// </summary> /// <param name="stmSource"> </param> /// <returns> </returns> private string ConvertStreamTostring(Stream stmSource) { } /// <summary> /// , /// /// </summary> /// <param name="url">, /// Google /// </param> /// <returns> , /// /// </returns> private Stream GetHttpStream(string url) { } #region Properties /// <summary> /// - /// strOriginal /// </summary> public string Original { /// /// set {strOriginal = value; OnPropertyChanged("Original");} get {return strOriginal;} } /// <summary> /// - /// strOriginal /// </summary> public string Result { get { return strResult;} } #endregion /// <summary> /// /// </summary> private void GetTranslateDirection() { } /// <summary> /// -, /// </summary> public void Translate() { } /// <summary> /// PropertyChanged /// </summary> public event PropertyChangedEventHandler PropertyChanged; /// <summary> /// , /// /// </summary> /// <param name="strPropertyName"> -, </param> protected void OnPropertyChanged(string strPropertyName) { } }
. , 3 . , 101 C# Samples, .
/// <summary> /// , /// /// </summary> private Stream GetHttpStream(string url) { /// HttpWebRequest wreqScrape = (HttpWebRequest)(WebRequest.Create(url)); /// wreqScrape.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)"; wreqScrape.Method = "GET"; wreqScrape.Timeout = 10000; try { /// , /// wresScrape = (HttpWebResponse)(wreqScrape.GetResponse()); return wresScrape.GetResponseStream(); } catch { /// /// throw new Exception("There was an error retrieving the Web page " + "you requested. Please check the Url and your connection to " + "the Internet, and try again."); } }
/// <summary> /// /// </summary> private string ConvertStreamTostring(Stream stmSource) { /// , /// StreamReader sr = null; /// - if (stmSource != null) { /// /// try { /// StreamReader sr = new StreamReader(stmSource); /// /// return sr.ReadToEnd(); } catch { /// /// /// throw new Exception(); } finally { /// /// - wresScrape.Close(); /// - sr.Close(); } } else { return null; } }
GetTranslateLang(). , . 2 , , , , . 5 (, , , ..), , , . .
/// <summary> /// /// </summary> private void GetTranslateDirection() { ///, - /// Byte bRus=0; ///, - /// Byte bEn = 0; /// - /// /// Byte bSuccess = (strOriginal.Length<5)?(Byte)strOriginal.Length:(Byte)5; /// ///: /// /// /// foreach (char c in strOriginal.ToUpper()) { /// /// if (c >= '' && c <= '') bRus++; else if (c >= 'A' && c <= 'Z') bEn++; ///: /// if (bRus == bSuccess) { bEnRus = false; break; } else if (bEn == bSuccess) { bEnRus = true; break; } } }
Translate. : google- strOriginal bEnRus, , strResult. , .
/// <summary> /// -, /// </summary> public void Translate() { /// GetTranslateDirection(); /// /// - StringBuilder sb=new StringBuilder(@"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q="); /// sb.Append(strOriginal); sb.Append("&langpair="); /// sb.Append((bEnRus)?"en|ru":"ru|en"); /// - strResult = ConvertStreamTostring(GetHttpStream(sb.ToString())); /// JSON- JObject o = JObject.Parse(strResult); /// strResult = o["responseData"].First().First().ToString(); /// /// OnPropertyChanged("Result"); }
- . , google :
{"responseData": {"translatedText":"[ ]"}, "responseDetails": null, "responseStatus": [ ]}.
, responseData . translatedText, .
strResult = o["responseData"].First().First().ToString();
, Json.NET :
, Project->Add Reference( Browse ), ;
using Newtonsoft.Json.Linq;
, : . , WPF , , .
, 2 TextBox, , - .
XAML:
<TextBox Height="212" HorizontalAlignment="Left" Margin="12,32,0,0" Name="textBoxOriginal" VerticalAlignment="Top" Width="234" Grid.ColumnSpan="3" /> <TextBox Height="211" HorizontalAlignment="Right" Margin="0,32,12,0" Name="textBoxResult" VerticalAlignment="Top" Width="238" Grid.Column="2" />
Text ClassTranslate. :
<TextBox Text="{Binding Path=Original,UpdateSourceTrigger=PropertyChanged}" />
<TextBox Text="{Binding Path=Result,UpdateSourceTrigger=PropertyChanged}"/>
Path , , .
, - . OneWay, .. . , ( ), - , . , , , strOriginal . , strOriginal -TwoWay. :
<TextBox Text="{Binding Path=Original, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" /> <TextBox Text="{Binding Path=Result, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
. , , , .. . ,
ClassTranslator translator = new ClassTranslator();
DataContext .
public MainWindow() { InitializeComponent(); MainGrid.DataContext=translator; }
. Translate
private void buttonTranslate_Click(object sender, RoutedEventArgs e) { translator.Translate(); }
, . - .
WPF Json-. , , =) . :
. 5 , , , 5 .
, HTTP, /. , , .
, , ( Google).
, , . !
Written by:
: vr-online.ru