Localize a Silverlight 4 Application

Posted by: Suprotim Agarwal , on 1/28/2011, in Category Silverlight 2, 3, 4 and 5
Views: 47165
Abstract: In this article, I will demonstrate how to localize a Silverlight 4 application. In simple words, localization is the customization of applications for a given culture or locale.

In this article, I will demonstrate how to localize a Silverlight application. In simple words, localization is the customization of applications for a given culture or locale. If you have been doing ASP.NET development, you probably are already familiar with localizing applications. The process of localizing a Silverlight application is quite similar, i.e. using Resource files. You create a resource file for each culture or language your application plans to support and Silverlight will use the CurrentUICulture of the UI thread to decide which resource to use. Let us see the steps in detail.

Note: Whenever I talk to developers about localization, they often refer to localization as translation of text in different languages. Although that’s true, localization is much more than that. Depending on the culture, you can display icons, images, flow of text (right to left or vice-versa), audio and much more, for a specific culture or language that your application supports.

Step 1: Create a new Silverlight project and call it ‘SliverlightLocalize’. Drag and drop a textbox from the Toolbox to the Silverlight designer.

Step 2: Right click your Silverlight project > Add > New Item > Select ‘Resources File’ and rename it as localstring.resx. In the resource file, add a Name and Value as shown below

Also make sure the access modifier combo box says ‘Public’.

Step 3: Let us add support for the Spanish language in our application. Make a copy of this resource file by copying it from the project in the Solution Explorer and pasting it back ‘twice’ inside the project. Rename it to localstring.es.resx and localstring.es-ES.resx. What we have done is created an invariant resource file (es), as well as a culture-specific resource file(es-ES). This is a good practice often referred to as the fallback culture practice and you should adopt this for every language/culture you support.

Now type the Spanish translation for Hello World in both the files as shown below

Step 4: Copy all the three resource files in the Properties folder of your project (you can also use a separate folder). The structure should look similar to the following

Step 5: Now that the resource files are set, let us bring them to use. Open MainPage.xaml (where you added the textbox) and add a namespace that points to the local namespace of the resource file.

Now use the localized resource string as a static resource

Now write the following mark up in the TextBox control to data-bind to the static resource

Step 6: If you are an ASP.NET developer, you may be tempted to run the application since you have done all that’s needed to localize the application. However in Silverlight, there are some additional steps. You need to update your Silverlight project to set the <SupportedCultures> attribute. Right click on the project and choose ‘Unload project’. Right click the project again and choose ‘Edit SilverlightLocalize.csproj’. This brings up the metadata. Look for the <SupportedCultures> tag and add modify it to add the Spanish culture to it.

Note: If you are supporting multiple cultures, add all of them, separated with a comma

Now right click on your project and say ‘Reload Project’.

You are all set. Run the application. Got an error? This is due to a bug in how Visual Studio handles the resource constructor. You may encounter an error that says “No matching constructor found on type”

This error occurs since the resource file class constructor is still set to ‘internal’. So open localstring.Designer.cs and change the constructor access modifier to ‘public’.

Note: Make sure that every time you open the resource designer to add new Name/Value entries, change the constructor access modifier to ‘public’. I told you, its buggy and annoying too.

Run the application again. You should see ‘Hello World’ if the culture on your machine is not set to Spanish. On my machine, the culture set is English.

The resource file to be used, is determined by the CurrentUICulture which in turn is based on the culture set on the user’s machine. So to test the Spanish resource file, you can easily override this setting using the following code in the App.xaml constructor:

Now if you run the application, you will see the text in Spanish.

I hope you liked the article and I thank you for viewing it.

The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by sergey on Friday, January 28, 2011 12:54 PM
thanks for the step instructions as i have understood the process of localizing sl apps. please write more articles like these
Comment posted by Andrea Boschin on Saturday, January 29, 2011 8:33 AM
Hi Jeremy, Useful article. I have only to say I always avoid to manually change generated files because often VS suddenly erase my changes :)
Fyi, I created a T4 to work around this problem: http://www.silverlightplayground.org/post/2010/11/25/Simplify-localization-with-a-T4-template.aspx
HTH
Comment posted by achim on Tuesday, February 8, 2011 5:43 AM
Hi - thanks for the article. Especially the part where you set the CurrentCulture and the CurrentUICulture. For me this did the trick everything else I tried before didn't work (like setting the language inside IE etc).
But now another question: Even if I set set CurrentCulture AND CurrentUICulture the numbers I display formatted using the "StringFormat='C'" always display with a "$" sign instead of "€" sign which woiuld expected when using German culture. Do you know the reason? Here some code to clarify the situation:

<sdk:DataGrid x:Name="dataGridBooking"
     Grid.Row="0"
     Grid.Column="3"
     AutoGenerateColumns="False"
     ItemsSource="{Binding ElementName=t_PM_BookingDomainDataSource, Path=Data}"
     RowDetailsVisibilityMode="VisibleWhenSelected"
     IsReadOnly="True"
     MinHeight="180"
     MaxHeight="180"
     Style="{StaticResource FormFieldStyle}">
<sdk:DataGrid.Columns>
   <sdk:DataGridTextColumn x:Name="bookingDateColumn"
            Header="gebucht"
            Width="Auto"
            Binding="{Binding Path=BookingDate, StringFormat='dd.MM.yyyy'}">
   </sdk:DataGridTextColumn>
   <sdk:DataGridTextColumn x:Name="amountColumn"
            Binding="{Binding Path=Amount, StringFormat='C'}"
            Header="Betrag"
            Width="Auto" />
   <sdk:DataGridTextColumn x:Name="bookingTypeNoColumn"
            Binding="{Binding Path=BookingTypeNo}"
            Header="Buchungstyp"
            Width="Auto" />
   <sdk:DataGridCheckBoxColumn x:Name="rebookedColumn"
            Binding="{Binding Path=Rebooked}"
            Header="rückgebucht"
            Width="Auto" />
</sdk:DataGrid>

Whereas the column "bookingDateColumn" displays just perfectly the column "amountColumn" always displays the "$" sign. I appreciate every hint which sets onto the right track.
Thank you
achim
Comment posted by Suprotim Agarwal on Wednesday, February 9, 2011 3:10 AM
achim: Try adding this line of code in the constructor (original tip by Tim Heuer):

this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
Comment posted by Excellent Tutorial on Wednesday, May 25, 2011 11:36 AM
I looked all examples last two days and found the answer here.
Key Steps are;
*** How to modify the .csproj file (inload and edit)
*** Modifying the Resource.Designer.cs file (changing constructor internal to public)
I spend one day and did not see any of those key steps in other sources.

Thank you very much.
Comment posted by Shelby on Tuesday, July 12, 2011 9:06 AM
If you see that compilation error it is because the CustomBuildTool is set to ResXFileCodeGenerator instead of PublicResXFileCodeGenerator.  Changing the access modifier as you stated above should fix that (from internal to Public).

Changing it manually in that class is a bad choice because it is auto-generated code and each change that causes a regeneration of the file will result in needing to change it again... and again.
Comment posted by Suprotim Agarwal on Wednesday, July 13, 2011 6:38 AM
Shelby: I hate editing the auto-generated code, but is there an alternative in SL 4? If yes, please share. I would love to mention it in the article.
Comment posted by Abi on Tuesday, February 12, 2013 8:56 AM
Thank you!! :D

I was looking for hours. The displayed values didn't match the current culture. The problem was that I didn't give any value in the tag <SupportedCultures>


:)
Comment posted by Diogo on Friday, August 9, 2013 9:07 AM
great article... thanks!
Comment posted by Gill on Tuesday, September 3, 2013 6:20 AM
Excellent post. Just one question: if I want to bind my localization text in my xaml.cs file, how do I do this?
Comment posted by Troute on Tuesday, September 10, 2013 9:52 AM
I use an online localization tool that works great with both resx and xml files. I recommend you try it, it saves a lot of trouble and has many helpful features: https://poeditor.com/.
Comment posted by MT_1990 on Sunday, February 1, 2015 5:43 AM
I would only add that source code should be separated from language project. It would be better not to create localizations in Visual Studio. Using satellite assemblies localization can be created using only .dll/.exe (compiled project).

This way adding a new language does not require recompiling the project. What is more translation could be made even by the end-user.

I found localization tool called LSACreator (http://www.tbs-apps.com/lsacreator - free for non-commercial use or buy option) which does exactly that. It is simple and has same useful features. I am not sure are there any other tools which use only .dll/.exe.
Comment posted by Alex on Monday, February 23, 2015 5:05 AM
Amazing. I have finaly found a solution that works! Thank you!