Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

استخدام ترجمة جوجل في ويندوز فون

From Wiki
Jump to: navigation, search
WP Metro Icon Web.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code Example
Installation file: Media:TranslatorExample.zip
Tested with
SDK: Windows Phone 7.1+ SDK
Compatibility
Platform(s): Windows Phone 7.5+
Windows Phone 8
Windows Phone 7.5
Dependencies: Json.NET
Article
Translated:
By bintk
Last edited: hamishwillee (27 Jun 2013)

يوضح هذا المقال كيفية استخدام خدمة ترجمة جوجل في تطبيقات ويندوز فون..

Contents

المقدمة

لقد تعبت في تصفح شبكة الإنترنت لإيجاد خدمة ترجمة مجانية ,و التي يمكن إستخدامها ولكنني وجدت حلاً يمكن تنفيذه علي هاتف ويندوز فون 8 ( لم أختبرها علي ويندوز فون 7) بإستخدام لغه ياهو كويري للوصول لخدمة جوجل ولكن الحد الاقصي لهذه الخدمة هو 20000 استعلام للعنوان التعريفي الواحد " اي بي " ولا يشترط التسجيل لانها خدمة عامه راجع هذا الجدول Yahoo Query Language , http://www.datatables.org.

Note.pngNote: Google Translate APIمتوفرة بشكل مدفوع ولكن بإستخدم لغه ياهو كويري متوفر بحرية وبشكل مجاني من خلال الطريقة أعلاة

التنفيذ

سوف تحتاج إلي إضافه ملفين إلي تطبيقك . The main class and an event argument class. Additionally, Json.NET is requied for the class to work. It's a free package.

Translator.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json;
using YourNameSpace;
 
public delegate void TranslatedString(TranslatedStringEventArgs e);
 
namespace YourNameSpace
{
class Translator
{
public event TranslatedString TranslatedString;
public string translatingString;
 
// Supporting function to make the URI generation simpler.
private Uri constructUri(string to, string text)
{
string url = @"http://query.yahooapis.com/v1/public/yql?q=" + Uri.EscapeDataString("select * from google.translate where q=\"" + text + "\" and target=\"" + to + "\";") + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
return new Uri(url, UriKind.Absolute);
}
 
public void TranslateString(string to, string text)
{
// getting the translation via YQL, setting up a WebClient for this
WebClient wc = new WebClient();
wc.OpenReadCompleted += wc_OpenReadCompleted;
wc.OpenReadAsync(constructUri(to, text));
translatingString = text;
}
 
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
// getting a new class to return and filling in the inital translate string
TranslatedStringEventArgs tsea = new TranslatedStringEventArgs();
tsea.initialString = translatingString;
 
// checking if the translation succeeded
if (e.Error == null)
{
// setting the return values
tsea.Error = false;
tsea.ErrorMessage = "";
 
//helper variables for converting
string resultString = "";
byte[] byteArrayForResultString = new byte[e.Result.Length];
 
//converting the returned value to string - that's what Json.NET eats
e.Result.Read(byteArrayForResultString, 0, Convert.ToInt32(e.Result.Length));
resultString = UTF8Encoding.UTF8.GetString(byteArrayForResultString, 0, byteArrayForResultString.Length);
 
// try to parse the results
try
{
// doing the actual work
Newtonsoft.Json.Linq.JObject obj = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(resultString);
 
// Since everything is called "json" in the json array (pretty straightforward, but not that practical if you ask me),
// we have to navigate to our string manually.
tsea.translatedString = ((((((((((((((((Newtonsoft.Json.Linq.JContainer)(obj)).First).First).Last).First).First).First).First).First).First).First).First).First).First).First).ToString();
}
// handle the exceptions if there are
catch (Exception serializer_exception)
{
tsea.Error = true;
tsea.ErrorMessage = "Error in JSON Serializing: " + serializer_exception.Message + Environment.NewLine + resultString;
}
}
else
{
tsea.Error = true;
tsea.ErrorMessage = "Error in WebClient: " + e.Error.Message;
}
TranslatedString(tsea);
}
}
}

TranslatedStringEventArgs.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace YourNameSpace
{
public class TranslatedStringEventArgs:EventArgs
{
public string initialString { get; set; }
public string translatedString {get; set; }
public bool Error { get; set; }
 
public string ErrorMessage { get; set; }
 
}
}

الاستخدام

The usage is very simple. As shown below, instantiate the Translator object t and pass the language and string to be translated as arguments to TranslateString.

Translator t = new Translator();
t.TranslatedString += t_TranstlatedString;
t.TranslateString("de","How are you?");

Here is the translated event handler,

void t_TranstlatedString(TranslatedStringEventArgs e)
{
if (e.Error == false)
{
MessageBox.Show(e.translatedString);
}
else
{
MessageBox.Show(e.ErrorMessage, ":(", MessageBoxButton.OK);
}
}

الكود المصدري

يمكنك تحميل الكود المصدري لهذا المثال من هنا : File:TranslatorExample.zip

This page was last modified on 27 June 2013, at 11:22.
224 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×