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.

Revision as of 11:29, 27 June 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

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
Compatibility
Platform(s): Windows Phone
Windows Phone 8
Windows Phone 7.5
Platform Security
Signing Required: Self-Signed
Capabilities: ID_CAP_NETWORKING
Article
Keywords: Google Translate
Translated:
By bintk
Last edited: hamishwillee (27 Jun 2013)

Contents

المقدمة

جوجل توفر واجهة برمجية لترجمة جوجل في نسخه مدفوعة .في هذه المقالة سوف نستخدم خدمة ويب ترجمة جوجل للحصول على رد في شكل 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>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="TextBlockLanguageFrom" FontFamily="Segoe WP" FontSize="40" Margin="{StaticResource PhoneMargin}" Foreground="{StaticResource PhoneAccentBrush}" Text="From : " />
<toolkit:ListPicker HorizontalAlignment="Left"
x:Name="ListPickerLanguageFrom"
Header=""
ItemsSource="{Binding language}"
SelectedIndex="5"
ExpansionMode="FullScreenOnly"
FullModeItemTemplate="{StaticResource LanguagesTemplate}"
SelectionChanged="ListPickerLanguageFrom_SelectionChanged" Width="290" />
</StackPanel>
<TextBox Name="TextBoxTranslateFrom" TextWrapping="Wrap" Width="460" Height="150" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</StackPanel>
<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"
x:Name="ListPickerLanguageTo"
Header=""
ItemsSource="{Binding language}"
SelectedIndex="7"
ExpansionMode="FullScreenOnly"
FullModeItemTemplate="{StaticResource LanguagesTemplate}"
SelectionChanged="ListPickerLanguageTo_SelectionChanged" Width="290" />
</StackPanel>
<TextBox Name="TextBoxTranslateTo" TextWrapping="Wrap" Width="460" Height="150" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</StackPanel>
<Button Content="Submit" Height="72" Click="Button_Click" ></Button>
</StackPanel>
</Grid>
  • كما نقوم بإنشاء قالب للموارد ListPicker في ملف MainPage.xaml
<phone:PhoneApplicationPage.Resources>
<library:Languages x:Key="Languages"/>
<DataTemplate x:Key="LanguagesTemplate">
<StackPanel Orientation="Horizontal">
<Rectangle
Fill="{StaticResource PhoneAccentBrush}"
Width="30"
Height="30"
Margin="0 0 12 0"/>
<TextBlock Text="{Binding}" FontFamily="Segoe WP" FontSize="51"/>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>

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

  • 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 = "http://translate.google.com/translate_t?text=" + 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));
}
else
{
MessageBox.Show("Enter text!");
}
}
void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
try
{
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.
233 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.

×