Implementation of localization in Silverlight

Source: Internet
Author: User

For the localization function, I personally feel that at the demand stage, it is necessary to determine whether the project needs to be localized, which can reduce a lot of work in the future. The Sl support for the localization function and ASP. NET

The same as in, mainly using the cultureinfo class.

Let's take a look at the specific process to implement localization. Let's take a look at my project structure:

Here, I put the resource file into a separate project, so that the structure is clear.

Step 1:

First, add several resource files under the ages project. Here, the Resource Name must follow.. Net naming rules. For example, if the current culture is Chinese, the system will automatically go to localization. gets the relevant characters in the zh-CN.resx. If you are not familiar with the naming of multilingual cultures, you can view the. NET globalization

Let's set a simple value,

A country field is defined here. The values in Chinese and English cultures are China and China.

Step 2:

Create value conversion class appresource. CS

NamespaceAges
 
{
 
Public ClassAppresource: ivalueconverter
 
{
Public Readonly StaticResourceManager manager =NewResourceManager ("Ages. Localization",Typeof(Localization). Assembly );
 
Private StaticCultureinfo culture = thread. currentthread. currentculture;
Public StaticCultureinfo uicultrue
 
{
 
Get{ReturnCulture ;}
 
Set{Culture =Value;}
}
 
Public StringGet (StringResource)
 
{
 
ReturnManager. getstring (resource, uicultrue );
}
 
Public Static StringGetvalue (StringResource)
 
{
 
ReturnManager. getstring (resource, uicultrue );
}
 
Public ObjectConvert (Object Value, Type targettype,ObjectParameter, system. Globalization. cultureinfo Culture)
 
{
VaR reader = (appresource)Value;
 
ReturnReader. Get ((String) Parameter );
 
}
 
 
Public ObjectConvertback (Object Value, Type targettype,ObjectParameter, system. Globalization. cultureinfo Culture)
 
{
 
Throw NewNotimplementedexception ();
}
 
}
 
}

This class mainly uses the ResourceManager object to perform localization operations.

Step 3:

After compilation, we introduce ages. dll in the localilizationinsilverlight project.

In app. XAML, we introduce this DLL and set it as a resource.

<Application
 
X:Class="Localilizationinsilverlight. app"
 
Xmlns=Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns:X=Http://schemas.microsoft.com/winfx/2006/xaml"
 
Xmlns:Local="CLR-namespace: ages; Assembly = ages"
 
>
<Application. Resources>
 
<Local:Appresource X:Key="Localization"></Local:Appresource>
</Application. Resources>
 
</Application>

Step 4:

Note that when you compile the solution, vs does not package the resource file into xap. You need to manually modify localilizationinsilverlight. csproj.

Supporttedcultures node in

<Supportedcultures>Zh-CN, en-GB
 
</Supportedcultures>

After the resource is introduced, we will use it on the page.

 
<Textblock Height="23"
Name="Textblock1"
 
Text="{Binding converterparameter = country,
 
Converter = {staticresource localization}, source = {staticresource localization }}"
Width="90" />

After running, we can see the Chinese character on the interface. We can change the current culture to test it.

 
PublicMainpage ()
 
{
Appresource. uicultrue =NewCultureinfo ("En-GB");
 
Initializecomponent ();
 
}

In this way, we change the current culture to en-GB before the page initializecomponent (), and the value of China will appear in textblock.

Extension: Apply the localization function to the headers attribute of the DataGrid.

Here we can first add a simple method in appresource. CS

Public Static StringGetvalue (StringResource)
 
{
 
ReturnManager. getstring (resource, uicultrue );
 
}

Then add a DataGrid to the UI.

 
<Data:DataGrid Autogeneratecolumns="False"
 
Height="200"
Name="Maid"
 
Width="200">
 
<Data:DataGrid. Columns>
<Data:Datagridtextcolumn Canuserreorder="True"
 
Canuserresize="True"
Canusersort="True"
 
Width="Auto" />
</Data:DataGrid. Columns>
 
</Data:DataGrid>

C #CodeYou just need to set the title:

 
  This . datagrid1.columns [0]. header = appresource. getvalue (" country  "); 

after completion, you can use the method in Step 4 to test

> code download: localized demovs2010 + sl3

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.