(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

Internationalization/Globalization in Silverlight - Part 5 - MVVM Approach

(1 votes)
Ross Wozniak
>
Ross Wozniak
Joined Feb 19, 2010
Articles:   9
Comments:   0
More Articles
11 comments   /   posted on Feb 26, 2010

This article is compatible with the latest version of Silverlight.

It’s about time for a step by step walkthrough of the internationalization process. For this example, I will use my preferred approach, MVVM. Note that most of the steps would be the same for other internationalization methods that I outlined in my previous post, Implementation Options.

Scenario

In this example we will pretend that we are tasked with building an application whose requirements state that the default language will be English, but that users will also be accessing it from the U.K. and Germany.

I apologize for the small size of the screenshots. I’ll try to update some of these to a bigger size when I have a chance.

1. Let’s start by creating a new project in VisualStudio using the Silverlight Application template, and naming it InternationalizationApp1:

2. When the New Silverlight Application dialog appears, uncheck the ‘Host the Silverlight application…’ checkbox, and click OK:

3. Right-click on the project and add a New Folder:

clip_image006

4. Name the folder Assets, then create a child folder called Resources:

5. Right-click and add a New Item:

clip_image010

6. In the Add New Item dialog select Resources File, name it MyStrings.resx, and click Add:

You should now see the VisualStudio Resource Editor:

7. Add a string called Label_Color, with a value of ‘Color:’

8. Add another string called Label_Date, with a value of ‘Date:’

9. Save your changes:

10. Now let’s add a British version. Copy MyStrings.resx and paste it back in the Resources directory:

clip_image018

11. Rename it to MyStrings.en-GB.resx:

12. Delete the code file, MyStrings.en-GB.Designer.cs:

clip_image022

13. With MyStrings.en-GB.resx selected, click the Properties tab:

14. Next to Custom Tool, remove the string ‘ResXFileCodeGenerator’:

15. Change the Access Modifier to ‘No code generation’

16. Copy MyStrings.en-GB.resx and paste it in the Resources folder:

17. Rename it to MyStrings.de-DE.resx:

We now have our British and German versions of the .resx file. Only the resource file for our default language, English, will have a code file associated with it. The other two do not need one.

18. In MyStrings.en-GB.resx, change the value of Label_Color to ‘Colour’ (add a ‘u’ before the ‘r’ at the end).

19. Delete Label_Date (by right-clicking at the left end of its row and clicking Delete).

20. Save your changes:

21. In MyStrings.de-DE.resx, change the value of Label_Color to ‘Farbe:’.

22. Change the value of Label_Date to ‘Datum:’

23. Save your changes.

We are now done creating our resource strings for English, British and German. Now we simply need to display them in our UI.

24. Right-click on the project and create folders called ‘Views’ and ‘ViewModels’:

clip_image038

25. Right-click on the Views folder.

26. Select Add New Item.

27. In the Add New Item dialog, select Silverlight on the left side.

28. Select Silverlight User Control, name the file MyView.xaml, and click Add:

29. Right-click on the ViewModels folder.

30. Select Add > Class.

31. In the Add New Item dialog, name the file MyViewModel.cs, and click Add:

32. In the MyViewModel class, add the following read-only properties:

public class MyViewModel
{
    public string ResourceLabelColor
    {
        get { return MyStrings.Label_Color; }            
    }
 
    public string ResourceLabelDate
    {
        get { return MyStrings.Label_Date; }
    }
}

33. You will also need to add the following using statement:

using InternationalizationApp1.Assets.Resources;

34. In the code-behind of the MyView UserControl, MyView.xaml.cs, add a line that sets the DataContext to an instance of the view model class:

public partial class MyView
{
    public MyView()
    {
        InitializeComponent();
        DataContext = new MyViewModel();
    }
}

35. You will also need to add the following using statement:

using InternationalizationApp1.ViewModels;

36. In MyView.xaml, add the following xml namespace at the top:

xmlns:Views="clr-namespace:InternationalizationApp1.Views"

37. And replace the Grid with the following:

<Grid x:Name="LayoutRoot" Background="White" Margin="12">
    <Views:MyView/>
</Grid>

38. Now hi Ctrl+F5 to see what we have:

Please keep in mind that we are not attempting to make anything pretty here, just trying to grasp the basics of internationalization.

39. Now let’s add the lines of code to the Application_Startup method in App.xaml.cs:

private void Application_Startup(object sender, StartupEventArgs e)
{
    // Set the culture, for testing purposes
    var culture = new CultureInfo("en-GB");
    Thread.CurrentThread.CurrentCulture = culture;
    Thread.CurrentThread.CurrentUICulture = culture;
 
    this.RootVisual = new MainPage();
}

40. There’s still one more thing we need to do to make this work. Right-click on the project and selected Unload Project:

41. Now right-click on the project again and selected Edit InternationalizationApp1.csproj:

clip_image048

42. Locate the SupportedCultures node, and replace it with this:

<SupportedCultures>en-US;en-GB;de-DE</SupportedCultures>

43. Save your changes

44. Right-click on the project again and select Reload Project:

Now hit Ctrl+F5 again:

clip_image052

Notice that the spelling of Color is now Colour.

Now let’s try German.

In App.xaml.cs’ Application_Startup method, change “en-GB” to “de-DE”.

Hit Ctrl+F5 one more time:

Note that when we created the British .resx file, MyStrings.en-GB.resx, we changed the Label_Color string to ‘Colour’, but then we deleted the Label_Date string. The reason for this is that ‘Date’ is spelled the same in the England, so there is no reason to have the key in that .resx file. Because it does not exist, the framework will obtain the string from the default resource file, MyStrings.resx.


Subscribe

Comments

  • -_-

    RE: Internationalization/Globalization in Silverlight - Part 5 - MVVM Approach


    posted by Steve Crane on Mar 02, 2010 17:20
    Nice article. We use a different method of languaging applications that is not resource-based or locale-dependent so I have not done it the resource way before.

    I have encountered a problem following this in VS2008. The using InternationalizationApp1.Assets.Resources shows an error unless I place an empty .cs file in the Resources folder and even then the MyStrings references show errors of not existing in the current context. I need to head off home now but will continue later this evening or in the morning. Should you see this before then and offer a solution, that would be great.
  • -_-

    RE: Internationalization/Globalization in Silverlight - Part 5 - MVVM Approach


    posted by Ross on Mar 02, 2010 17:36
    Hmm, trying adding MyStrings.resx under Assets/Resources (make sure you select Resources File from the Add New Item dialog). You should then be able to expand the arrow next to MyStrings.resx and see MyStrings.Designer.cs. If you open this file its namespace should be InternationalizationApp1.Assets.Resources, which should match up with the using statement you added to your view model class.
  • -_-

    RE: Internationalization/Globalization in Silverlight - Part 5 - MVVM Approach


    posted by Steve Crane on Mar 03, 2010 08:47
    I just deleted all the .resx files and re-created them. I'm pretty sure that I didn't do anything differently yet this time there is a MyStrings.Designer.cs that wasn't there the first time round. And of course that solves the problem.
  • -_-

    RE: Internationalization/Globalization in Silverlight - Part 5 - MVVM Approach


    posted by Ross on Mar 03, 2010 10:12
    Glad to hear it Steve.
  • -_-

    RE: Internationalization/Globalization in Silverlight - Part 5 - MVVM Approach


    posted by Steve Crane on Mar 03, 2010 11:56
    I have finished playing with your sample including adding some tweaks like dynamic switching between locales. In the process I have learned some cool stuff I never used before.

    There appears to be an error/omission in your steps above. I found that the changes you describe in steps 36 and 37 had to be made to Mainpage.xaml rather than MyView.xaml, and that some markup has to be added to MyView.xaml that displays the resource strings by binding to the properties exposed by MyViewModel.cs. Without these changes the project runs but no text is displayed.

    Should anyone be interested in viewing the solution with my tweaks, the source can be found here.
  • -_-

    RE: Internationalization/Globalization in Silverlight - Part 5 - MVVM Approach


    posted by K.Kong on May 10, 2010 12:16

    Coming from a Windows Forms I18N/I10N background, this is the information I was looking for now that I am starting on SilverLight.  Thank you for the information.

    I would like to use the browser language setting to change the resource file so I guess I should pass the language parameter to App.xaml.cs.  How about if I allow the user to switch language mid-way?  How can I change the thread culture?

  • -_-

    RE: Internationalization/Globalization in Silverlight - Part 5 - MVVM Approach


    posted by Ross on May 10, 2010 17:06

    I think you could do it by having a method like this, and calling it when they do something...like selecting a different language from a dropdown.

     

            private static void ShowGerman()
            {
                var cultureInfo = new CultureInfo("de");
                Thread.CurrentThread.CurrentCulture = cultureInfo;
                Thread.CurrentThread.CurrentUICulture = cultureInfo;
            }

  • -_-

    RE: Internationalization/Globalization in Silverlight - Part 5 - MVVM Approach


    posted by Ravindra on Aug 03, 2010 10:00

    thanks for your sheer thought of sharing. thanks again and keep rocking

    +Ravindra

  • -_-

    RE: Internationalization/Globalization in Silverlight - Part 5 - MVVM Approach


    posted by Navin on Jan 16, 2011 08:35

    Incomplete Example.. Many Steps Missing


  • -_-

    RE: Internationalization/Globalization in Silverlight - Part 5 - MVVM Approach


    posted by cloudringer on Feb 18, 2011 06:58

    Article on localization of Silverlight and WPF applications using MVVM.

    http://wp.me/p1ih9J-U

  • ChrisKlug

    Re: Internationalization/Globalization in Silverlight - Part 5 - MVVM Approach


    posted by ChrisKlug on Dec 06, 2011 11:45

    I would suggest having a look at http://bit.ly/iFkGeX to see some options for globalization in Silverlight using MVVM. I do not recommend sprinkling the VM's with translated strings as the VM's easily become overrun with stuff that has nothing to do with them. 

    The VM is supposed to give the view a model it can pull the information and functionality needed from. It is not really supposed to be responsible for translating the UI. And on top of that, doing it like this is coupling your view/viewmodel to the actual resource management chosen.

    That's my 2 cents at least... Sorry for budding in...

Add Comment

Login to comment:
  *      *       
Login with Facebook

From this series