How to Implement Silverlight4 in multiple languages

Source: Internet
Author: User

1: create a new folder "Resouce" in the project, and then add the resource file "AppString. resx ", if you create an AppString. resx copy, change the file name to the corresponding language name,

Such as AppString. en-US.resx ., Change the access modifier of AppString. resx to Public.

 

2: Open the cs file of AppString. resx and check whether the class access modifier is Public. If not, change it to Public.

3: Open the App. xmal file and add the following code to bind the resource file with characters on other pages.

4: You can use this resource file on other pages. I use three languages here.

5: the language is switched. I use local storage to save the language selected by the user, and create a class to read the language selected by the user.

Copy codeThe Code is as follows:
Public class Configure
{
Static System. Globalization. CultureInfo currentCulture;
Public static System. Globalization. CultureInfo CurrentCulture
{
Get
{

If (currentCulture = null)
{
Try
{
System. IO. IsolatedStorage. isolatedstoragesetemedietting = System. IO. IsolatedStorage. IsolatedStorageSettings. ApplicationSettings;
If (appSetting. Contains ("language "))
{
CurrentCulture = new System. Globalization. CultureInfo (string) appSetting ["language"]);
}
}
Catch (Exception e)
{
}

}
If (currentCulture = null)
{
CurrentCulture = new System. Globalization. CultureInfo ("en-us ");

}
Return currentCulture;
}
Set
{
CurrentCulture = value;
System. Threading. Thread. CurrentThread. CurrentCulture = currentCulture;
System. Threading. Thread. CurrentThread. CurrentUICulture = currentCulture;

Try
{
System. IO. IsolatedStorage. isolatedstoragesetemedietting = System. IO. IsolatedStorage. IsolatedStorageSettings. ApplicationSettings;
If (appSetting. Contains ("language "))
{
AppSetting ["language"] = currentCulture. Name;
Deleetting. Save ();
}
Else
{
Deleetting. Add ("language", currentCulture. Name );
}

}
Catch (Exception e)
{
}
}
}

}

The code for the "Switch" button

Copy codeThe Code is as follows:
Private void button3_Click (object sender, RoutedEventArgs e)
{
Configure. CurrentCulture = new CultureInfo (comboBox1.SelectionBoxItem. ToString ());
// If (Configure. CurrentCulture. Name = "zh-CN ")
//{
// Configure. CurrentCulture = new CultureInfo ("en-US ");
//}
// Else
// Configure. CurrentCulture = new CultureInfo ("zh-CN ");

}

6: Finally, the Code started by the application, that is, read the language saved by the user. In the App. xmal. cs file,

Copy codeThe Code is as follows:
Private void Application_Startup (object sender, StartupEventArgs e)
{

CultureInfo culture = Configure. CurrentCulture;
Thread. CurrentThread. CurrentUICulture = culture;
Thread. CurrentThread. CurrentCulture = culture;
This. RootVisual = new MainPage ();
}

Note: After you press the switch button, you must log on to the application again to see the effect, not even if you switch.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.