Wednesday, October 6, 2010

Windows Phone 7 Quick Tip #15 – Five Minute Guide to Globalization

Globalizing your Windows Phone 7 Application will allow you to reach a much wider audience.  With the approach Microsoft is taking in there marketplace, this could mean the difference between your app making $500 or potentially > $1000 a month.  Globalizing your application is actually fairly simple but is much easier if you do from the start.  Globalization is much more than just translating the strings in your application, in addition you should also take a look at Localization Best Practices for Windows Phone for additional pointers.  This quick tip will just show you how to setup your project so you can start using String Resources rather than just putting the text the for the TextBoxes in your application.  Do this when you start your application, not at the tail end.  Hear me now, believe later :)

Our goal is to eliminate this:

<TextBlock Text="HelloWorld" />

And pull that string from here:

image

Here are the simple steps:

  1. Add a resource file to your project.
  2. I usually drag under the folder Properties, it just seems at home there.
  3. Open the resource file and add name value pair, the name goes in the first column and will be used to refer to your resource in XAML, the value is the default languages string.

Then change the Access Modifier to Public:

image

Next create a class that will create a static instance of the resources within your application.  The static class should be similar to:

using System;
using ResourceSample.Properties;

namespace ResourceSample
{
    public class StringResources
    {
        private static AppResources _resources;

        public static AppResources LocalizedResources
        {
            get
            {
                if (_resources == null)
                    _resources = new AppResources();

                return _resources;
            }
        }
    }
}

Then we need to create a static resource for this class in the App.xaml file.

image

Finally instead of this:

<TextBlock Text="HelloWorld" />

You can do this:

image

Once you have all the text for your application in a resource file, you can package up and deploy additional as described here.

Bonus Tip: Click here for how to localize the application title for your Windows Phone 7 application.

-twb

1 comment:

  1. I think there is a mistake in the XAML code example. The binding path should should be Path=LocalizedResources.HelloWorld
    not
    Path=Strings.HelloWorld

    ReplyDelete