Conventions for internationalization of Silverlight applications

Source: Internet
Author: User
Many methods have been used to internationalize Silverlight. Article Which of the following details can be regarded Terrylee This article (http://kb.cnblogs.com/page/42913/ ). As pointed out in this article, Silverlight has many flaws in international support, but the most unacceptable problem is that the syntax is too lengthy:


< Textblock Text = "{Binding alabel, source = {staticresource mystrings }}"   />


if you have to manually enter such a long string in each part of the text on the interface, sooner or later you will have to drive crazy people who write Code . Even if you really have the perseverance to enter all the input, such a huge amount of XAML is undoubtedly a kind of torment for those who look at the code. As mentioned above, the internationalization scheme proposed in some articles seems to be incomplete, and a convertparameter will be added after binding. Do these people have special preferences for the cloth?
in order to reduce the workload of internationalization and make the code clean and fresh, I adopted another agreed-based internationalization solution. In fact, the reason behind this is that it is not worth a penny, that is, to make all text input to be translated on the Interface follow a certain format, such as "RS: alabel ", Program uses visualtreehelper, a convenient class to traverse all components on the interface and find all the words that comply with the rules, then it is translated into the content of the current language. This solution is simple and works very well, because we need more than 90% of the international components on the interface, including textblock, button, and DataGrid, it does not matter if special controls cannot be internationalized in this way. After all, most of the work can be done easily.
In other words, when you add a control, write as follows:

< Textblock Text = "RS: alabel"   />


Is this much simpler?
It is also easy to traverse components and perform Code internationalization. Basically, it is a recursive call to visualtreehelper:

Void Mainpage_loaded ( Object Sender, routedeventargs E)
{
Localizerecursive ( This );
}

VoidLocalizerecursive (uielement ELEM)
{
Getlocalizer (ELEM. GetType (). localizer (ELEM );

Int Childcount = Visualtreehelper. getchildrencount (ELEM );
For ( Int I =   0 ; I < Childcount; I ++ )
{
VaR child = Visualtreehelper. getchild (ELEM, I) As Uielement;
If (Child ! =   Null )
Localizerecursive (child );
}
}

ilocalizer getlocalizer (type)
{< br> ilocalizer localizer = null ;< br> If ( ! _ localizers. trygetvalue (type, out localizer ))
localizer = New nulllocalizer ();
return localizer;
}

static void registerlocalizers ()
{< br> _ localizers. add ( typeof (textblock ), New textblocklocalizer ());
_ localizers. add ( typeof (button ), New buttonlocalizer ());
_ localizers. add ( typeof (DataGrid ), New datagridlocalizer ());
}< br>


Some localizer code is not provided here because it involves some business things. Basically, it is nothing more than string SEARCH replacement.
in this way, the page's XAML code becomes much cleaner than before.

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.