PDA

View Full Version : چند زبانه كردن برنامه ----Multi_Language Program-----



happy65_sh
یک شنبه 02 اسفند 1388, 13:28 عصر
سلام
نه اينكه اينجا هر سوالي پرسيده مي شه خيلي زود جواب داده مي شه ما هم حال مي كنيم هي سوال بپرسيم هي بياييم ببينيم جواب ها صف كشيدن:شیطان::متعجب:قصد توهين ندارم فقط مي خواهم بدونم اينقدر wpf كار حرفه اي و نيمه حرفه اي اينجا كمه يا....؟؟؟؟

حالا يه سوال جديد براي چند زبانه كردن يك برنامه ي wpf هيچ راهي وجود داره؟:عصبانی++:

رضا عربلو
یک شنبه 02 اسفند 1388, 14:34 عصر
بله وجود دارد. مقاله زیر را مطالعه کنید.


Step 1) Place all string fragments in a separate stringtable resource file. example: stringResources.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">

<!-- String resource that can be localized -->
<system:String x:Key="All_Vehicles">All Vehicles</system:String>

</ResourceDictionary>
step 2: make copies for each language and add them (translated) to the merged dictionaries. example App.xaml

<Application x:Class="WpfStringTables.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="StringResources.de-DE.xaml" />
<ResourceDictionary Source="StringResources.nl-NL.xaml" />
<ResourceDictionary Source="StringResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
The last resource file with strings will be used to replace text parts in code.

step 3a: Use the text parts from the string table example Window1.xaml

<Window x:Class="WpfStringTables.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Button Margin="51,82,108,129" Name="button1" Content="{StaticResource All_Vehicles}"/>
</Grid>
</Window>
step 3b: load the resource from code

void Page_Load()
{
string str = FindResource("All_Vehicles").ToString();
}
step 4: switch to new culture at start of application Codesnippet from App.xaml.cs

public static void SelectCulture( string culture )
{
// List all our resources
List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
foreach ( ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries )
{
dictionaryList.Add( dictionary );
}
// We want our specific culture
string requestedCulture = string.Format( "StringResources.{0}.xaml", culture );
ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault( d => d.Source.OriginalString == requestedCulture );
if ( resourceDictionary == null )
{
// If not found, we select our default language
//
requestedCulture = "StringResources.xaml";
resourceDictionary = dictionaryList.FirstOrDefault( d => d.Source.OriginalString == requestedCulture );
}

// If we have the requested resource, remove it from the list and place at the end.\
// Then this language will be our string table to use.
if ( resourceDictionary != null )
{
Application.Current.Resources.MergedDictionaries.R emove( resourceDictionary );
Application.Current.Resources.MergedDictionaries.A dd( resourceDictionary );
}
// Inform the threads of the new culture
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture( culture );
Thread.CurrentThread.CurrentUICulture = new CultureInfo( culture );
}

majid325
دوشنبه 03 اسفند 1388, 07:26 صبح
سلام
نه اينكه اينجا هر سوالي پرسيده مي شه خيلي زود جواب داده مي شه ما هم حال مي كنيم هي سوال بپرسيم هي بياييم ببينيم جواب ها صف كشيدن:شیطان::متعجب:قصد توهين ندارم فقط مي خواهم بدونم اينقدر wpf كار حرفه اي و نيمه حرفه اي اينجا كمه يا....؟؟؟؟

:عصبانی++:

البته معمولا دوستان سوال هاشون یه جوری هست که باید به اندازه یک دوره اموزشی اول توضیح بدیم بعد جواب سوال رو بدیم.

happy65_sh
دوشنبه 03 اسفند 1388, 07:50 صبح
ضمن تشكر از آقاي عربلو
امتحان مي كنم اگه به مشكلي برخوردم مزاحم مي شم

البته معمولا دوستان سوال هاشون یه جوری هست که باید به اندازه یک دوره اموزشی اول توضیح بدیم بعد جواب سوال رو بدیم:متعجب:

happy65_sh
دوشنبه 03 اسفند 1388, 09:49 صبح
سلام و واقعا ممنون
آقاي عربلو شما يكي از بزرگترين مشكلاتم را مرتفع كردين...:تشویق:
فعلا كه تست كردم و عالي عمل كرد و مشكلي نبود واقعا سپاسگذارم:تشویق: