Watch your Language!

The journey with WP7 continues. I have made several attempts to add multilanguage support to my WP7 apps with little success. The stumbling block in the process has been reaching the point which can be best described on a flowchart as “Magic Happens”. I am old school, so my approach to adding alternate languages is to use something like LabelText = FirstLabel(langid) where FirstLabel is an array of the string in various languages. In the Dot Net World, it is done with resource files, collections of triplets (label, value, comment) with a resource file for each language. One piece of magic is that the file names for the ressource files contain two “.”s. The identifier for the supported language is between the two “.”s. I have seen one WP7 book where the first period appears to have been replaced by a “_” by an over zealous editor.

So you just have to fill in the resource files and everything works. Not really, The text in the XAML code and the C# code are handled differently. The XAML is handled by creating a class to handle the resouce file.

namespace DBC
public class LocalizedStrings {
public LocalizedStrings(){ }
private static DBC.AppResources localizedresources = new DBC.AppResources();

public DBC.AppResources Localizedresources { get { return localizedresources; } }
} }

You will also have to add

<Application.Resources>
<local:LocalizedStrings xmlns:local ="clr-namespace:DBC" x:Key="LocalizedStrings" />
</Application.Resources>

to App.XAML.
Within the XAML, lblHelp is the label within the resource file AppResources that will be retrieved.

Text="{Binding Path=Localizedresources.lblHelp, Source={StaticResource LocalizedStrings}}"

In the C# code it is easier,

button.Text = AppResources.lblChallenge;

Of course, that is not all, you still have to tell the system what languages are supported. You will need to edit the project.csproj file (not the project.csproj.user file), but not in Visual Studio., use NotePad.

<SupportedCultures>
de-DE;es-ES;en-UK;en-US;fr-FR
</SupportedCultures>

The application bars do not support binding, so you will have to create the text directly, usually in the OnNavigatedTo event.

var button = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
button.Text = AppResources.lblChallenge;
button = (ApplicationBarIconButton)ApplicationBar.Buttons[1];
button.Text = AppResources.lblLdrBoard;
button = (ApplicationBarIconButton)ApplicationBar.Buttons[2];
button.Text = AppResources.lblHelp;

// and now for the AppBar menus
var menuitem = (ApplicationBarMenuItem)ApplicationBar.MenuItems[0];
menuitem.Text = AppResources.lblOptions;
menuitem = (ApplicationBarMenuItem)ApplicationBar.MenuItems[1];
menuitem.Text = AppResources.lblNewEvent;

I use a browser and a webpage to handle the help file, so it required some special attention.

CultureInfo cc;
cc = Thread.CurrentThread.CurrentUICulture;
string langCode = cc.TwoLetterISOLanguageName;
string HelpFileName;
switch (langCode)
{
case "fr": HelpFileName = "HelpFR.htm"; break;
case "de": HelpFileName = "HelpDE.htm"; break;
case "pl": HelpFileName = "HelpPL.htm"; break;
default: HelpFileName = "Help.htm"; break;
}

Depending on the name you use for the app, you may want to customize the tiles. Peter Tihanyi has a good explanation here http://engine-designs.com/wp7-appreslib-dll-generator.html .

While adding the languages, I did run into a problem with accents not being shown properly in the help file. This was fixed by changing the encoding from UTF8 to Unicode when the webpage is loaded to isolated storage. Actually, it partially fixed it. I am stil looking fora permenant solution.

//byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(fileAsString);
byte[] fileBytes = System.Text.Encoding.Unicode.GetBytes(fileAsString);

So how do you test? Testing can be done by changing region+langauge options of Settings on the phone or the emulator. Unlike the world of the PC, this change will require the device to be rebooted with the associated delays as this happens. So if you do do testing, do as much as possible before switching.

I have yet to submit the XAP, so I still have questions about how you can submit a single XAP, but there does not appear to be a way to submit alternate screen shots or marketplace descriptions in the alternate langauges. It appears that specifying SupportedCultures will trigger the publishing process to request more material. Okay, the app has been submitted and the certification process does detect the supported languages and provides a list of the supported down the right side. This is very easy to miss the first time. as you click the language, boxes for app name, short description, detailed description, keywords email and artwork for that language replace the ones for the English version. So be prepared with this material before submission.

I am also trying to work out how I was able to get it to work in French and German, but even though it is supported in WP7.1, Polish did not work. This one is now fixed. It was a problem with a corrupt resource file. I could add or change information in the resource file and the only indicator of a problem was that it did not work. So I had to rebuild it. This is not as bad as it seems. Step 1: Select all rows in the resource file and paste them into Excel. Step 2: Delete the resource file and create a new one with the language in the name (AppResources.pl.resx (there is that second period again) ). Step 3: Copy ALL the records from Excel into the resource file. If you have not touched the Excel file and it is still open, all the records should still be selected from when you did the paste. When you do paste into the resource file, make sure you select at least two cells, otherwise all the information goes into a single cell like the label (no bonus points for figuring out how I worked that one out. ;-)).

This is a good reference: http://windowsteamblog.com/windows_phone/b/wpdev/archive/2012/04/02/globalizing-and-localizing-a-windows-phone-app.aspx

Hopefully this has revealed some of the secrets behind the Magic Happens part of adding languages to a WP7 app. This is a work in progress and I will be adding to it as I continue to implement the languages.

John Marshall… Visio MVP       Visio.MVPs.org

Published by johnvisiomvp

The original Visio MVP. I have worked with the Visio team since 1993