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. 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 ExampleTested with
Devices(s): Nokia Lumia 800, 820,920
Platform(s): Windows Phone
Windows Phone 8
Windows Phone 7.5
Platform Security
Signing Required: Self-Signed
Keywords: Google Translate
By bintk
Last edited: hamishwillee (27 Jun 2013)



جوجل توفر واجهة برمجية لترجمة جوجل في نسخه مدفوعة .في هذه المقالة سوف نستخدم خدمة ويب ترجمة جوجل للحصول على رد في شكل HTML وتحليل النص المترجم باستخدام RegularExpressions.


انشئ تطبيق ويندوز فون

انشئ تطبيق ويندوز فون جديد لتنفيذ هذا المشروع بداخلة

إضافة عناصر التحكم لواجهة المستخدم

  • We create two ListPicker which is a part of Windows Phone Toolkit to select To and From of the language. Two TextBox to write and display our text. And a submit Button to process the web request.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"  DataContext="{StaticResource Languages}">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="TextBlockLanguageFrom" FontFamily="Segoe WP" FontSize="40" Margin="{StaticResource PhoneMargin}" Foreground="{StaticResource PhoneAccentBrush}" Text="From : " />
<toolkit:ListPicker HorizontalAlignment="Left"
ItemsSource="{Binding language}"
FullModeItemTemplate="{StaticResource LanguagesTemplate}"
SelectionChanged="ListPickerLanguageFrom_SelectionChanged" Width="290" />
<TextBox Name="TextBoxTranslateFrom" TextWrapping="Wrap" Width="460" Height="150" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<StackPanel Margin="0,30,0,20">
<StackPanel Orientation="Horizontal" >
<TextBlock x:Name="TextBlockLanguageTo" FontFamily="Segoe WP" FontSize="40" Margin="{StaticResource PhoneMargin}" Foreground="{StaticResource PhoneAccentBrush}" Text="To : " />
<toolkit:ListPicker HorizontalAlignment="Left" Margin="63,0,0,0" VerticalAlignment="Center"
ItemsSource="{Binding language}"
FullModeItemTemplate="{StaticResource LanguagesTemplate}"
SelectionChanged="ListPickerLanguageTo_SelectionChanged" Width="290" />
<TextBox Name="TextBoxTranslateTo" TextWrapping="Wrap" Width="460" Height="150" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Button Content="Submit" Height="72" Click="Button_Click" ></Button>
  • كما نقوم بإنشاء قالب للموارد ListPicker في ملف MainPage.xaml
<library:Languages x:Key="Languages"/>
<DataTemplate x:Key="LanguagesTemplate">
<StackPanel Orientation="Horizontal">
Fill="{StaticResource PhoneAccentBrush}"
Margin="0 0 12 0"/>
<TextBlock Text="{Binding}" FontFamily="Segoe WP" FontSize="51"/>

التعليمات البرمجية

  • Add the following class to display the list of language in the ListPicker
    public class Languages : INotifyPropertyChanged
public IEnumerable<string> language { get; private set; }
public Languages()
language = "Arabic, Bulgarian, Chinese, Danish, Dutch, English, Filipino, Finnish, French, German, Greek, Hebrew, Hindi, Hungarian, Icelandic, Indonesian, Irish, Italian, Japanese, Korean, Latvian, Lithuanian, Macedonian, Malay, Maltese, Norwegian, Persian, Polish, Portuguese, Romanian, Russian, Serbian, Slovak, Slovenian, Spanish, Swedish, Thai, Turkish, Ukrainian, Vietnamese".Split(new Char[] { ',' });
private void InvokePropertyChanged(string propertyName)
var handler = PropertyChanged;
if (null != handler)
handler(this, new PropertyChangedEventArgs(propertyName));
public event PropertyChangedEventHandler PropertyChanged;
  • Add the following code to select To and From of the language
  //selected language from 
private void ListPickerLanguageFrom_SelectionChanged(object sender, SelectionChangedEventArgs e)
AppModel.LanguageSelectedIndexFrom = 0;
AppModel.LanguageSelectedIndexFrom= ((ListPicker)sender).SelectedIndex;
AppModel.LanguageCodeFrom = SelectedLanguage.GetSelectedLanguageCode(AppModel.LanguageSelectedIndexFrom);
//selected language to
private void ListPickerLanguageTo_SelectionChanged(object sender, SelectionChangedEventArgs e)
AppModel.LanguageSelectedIndexTo = 0;
AppModel.LanguageSelectedIndexTo = ((ListPicker)sender).SelectedIndex;
AppModel.LanguageCodeTo = SelectedLanguage.GetSelectedLanguageCode(AppModel.LanguageSelectedIndexTo);
  • Add the following code to translate the text using a web service request and parse the HTML response using regular expression.
  //translate language
private void Button_Click(object sender, RoutedEventArgs e)
if (TextBoxTranslateFrom.Text != "")
string url = "" + TextBoxTranslateFrom.Text + "&sl=" + AppModel.LanguageCodeFrom + "&tl=" +AppModel.LanguageCodeTo;
WebClient webclient = new WebClient();
webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient_DownloadStringCompleted);
webclient.DownloadStringAsync(new Uri(url, UriKind.RelativeOrAbsolute));
MessageBox.Show("Enter text!");
void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
if (e.Error == null)
Match CompareText = Regex.Match(e.Result, "(?<=<span id=result_box class=\"short_text\">)(.*?)(?=</span>)");
string[] TranslatedText = Regex.Split(CompareText.Value, "\">");
// MessageBox.Show(TranslatedText.GetValue(1).ToString());
TextBoxTranslateTo.Text = TranslatedText.GetValue(1).ToString();
catch (Exception ex) { }


هذا المثال لايعرض الكثير من القدرات التي يمكن اطلاقها بقليل من الابداع و التعامل الذكي مع الادوات البرمجية و واجهة البرمجة الخاصة بترجمة جوجل

مقالات متعلقة

Using Google Translate on Windows Phone - مثال رائع مستخدم فية Yahoo Query Language

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

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