Walkthrough: Localizing a LightSwitch Application
You can create a LightSwitch application that automatically displays UI in a variety of languages through a Silverlight client, an HTML client, or both. In this walkthrough, you’ll create an application and both types of clients and then localize it into German (or another language for which you can provide translations).
To localize the application, you’ll add a resource identifier for each display string in the Screen Designer. Then, for both the client and server tiers, you'll create a resource file for the default language and one for the localized language. You can localize an application even if it has only one type of client. We've included both types so that you can practice localizing each.
Note
If you are localizing only a SilverLight client, you may need to upgrade it first. On the menu bar, choose Project, Upgrade Project. If the Upgrade Project command doesn’t appear, the project is already upgraded.
This walkthrough shows how to accomplish the following tasks:
Create the Application
Localize the Server Tier
Localize the Silverlight Client Screen
Add Default Language Resources
Add Localized Resources
Call a Resource From Code
Localize an HTML Client
Prerequisites
You need Microsoft LightSwitch for Visual Studio Update 2 to localize a LightSwitch application.
To create the application
Create an application by using either the LightSwitch HTML Application (Visual Basic) template or the LightSwitch HTML Application (Visual C#) template, and then name it Localization Sample.
Add a table, name it Contact, and then add the following fields:
Name
Type
Required
ContactName
String
Yes
ContactPhone
Phone Number
Yes
In Solution Explorer, open the shortcut menu for the HTML Client node, and then choose Add Screen.
Add a Browse Data screen that's named BrowseContacts, and then choose Contacts as the Screen Data.
In Solution Explorer, open the shortcut menu for the HTML Client node, and then choose Add Screen.
Add a Add/Edit Details screen that's named AddEditContact, and then choose Contact as the Screen Data.
Open the BrowseContacts screen and in the Screen Designer, open the shortcut menu for the Rows Layout | Contact List node, and then choose Add Button.
In the Add Button dialog box, in the showTab list choose showAddEditContact.
In the Contact field enter (New Contact), and then choose the OK button.
In Solution Explorer, open the shortcut menu for the Localization Sample node, and then choose Add Client.
Accept the default Desktop Client, and then choose the OK button.
In Solution Explorer, open the shortcut menu for the Desktop Client node, and then choose Add Screen.
Add a New Data screen that's named NewContact, and then choose Contact as the Screen Data.
In the Screen Designer, choose the Add node, and then choose Add Text.
In the Edit Text dialog box, enter Add a new contact here.
To localize the server tier
In Solution Explorer, open the Contacts entity.
In the Entity Designer, choose the ContactName field.
In the Properties window, choose the Display Name property, and then enter $(ContactName).
The $() notation signifies that the value for the Display Name property is a resource identifier. At run time, LightSwitch will retrieve the actual value from a resource file.
Note
Resource identifiers can contain only letters and numbers, not spaces.
In the Entity Designer, choose the ContactPhone field.
In the Properties window, choose the Display Name property, and then enter $(ContactPhone).
In Solution Explorer, switch to File View.
Open the shortcut menu for the Localization Sample.Server node, choose Add, and then choose New Item.
Add a Resources File, and then name it Service.resx.
Important
You must always name the server resource file for any LightSwitch application Service.resx, and the file must be in the root node of the server project.
Add the following values to the resource file:
Name
Value
ContactName
Name
ContactPhone
Phone
The values in the Name column match the resource identifiers that you added earlier, but the values don't have the $() notation. The Value strings will appear on any screen that uses the Contacts entity.
In Solution Explorer, open the shortcut menu for the Localization Sample.Server node, choose Add, and then choose New Item.
Add a Resources File, and then name it Service.de-DE.resx.
Add the following values to the resource file:
Name
Value
ContactName
Kontaktname
ContactPhone
Telefonnummer
The localized Value string will appear on any screen that uses the Contacts entity. Now you can run the application and verify that the strings for ContactName and ContactPhone appear correctly. You can also deploy the application to a computer that's set to German and verify that the localized strings appear.
Tip
As a best practice, consider localizing the strings for all entities on the server tier first. Those strings will appear on any screen that uses the entities, and you can override the strings at the screen level as needed.
To localize the Silverlight client screen
In Solution Explorer, switch to Logical View, and then open the NewContact screen.
In the Screen Designer, choose the Rows Layout | New Contact node.
In the Properties window, choose the Display Name property, and then enter $(AddContact).
In the Screen Designer, open the shortcut menu for the Text node, and then choose Edit Content.
In the Properties window, replace the existing text with $(Text).
If you run the application at this point, you’ll notice that the actual resource identifiers appear on the screen. Don’t worry — you’ll fix that in the next step by creating a default file for language resources.
To add a default file for language resources
In Solution Explorer, switch to File View.
Open the shortcut menu for the Localization Sample.DesktopClient node, choose Add, and then choose New Item.
Add a Resources File, and then name it Client.resx.
Important
For any Silverlight client of a LightSwitch application, you must always name the default file for language resources Client.resx, and the file must be in the root node of the client project.
Add the following values to the resource file:
Name
Value
AddContact
Add Contact
Text
Add the contact information, and then choose the Save button.
The values in the Name column match the resource identifiers that you added earlier, but the values don't have the $() notation. The values in the Value column are the strings that will appear when a user runs the application in the language that is set as the Default Language in the application settings.
To add a localized resource file
In Solution Explorer, switch to File View.
Open the shortcut menu for the Localization Sample.DesktopClient node, choose Add, and then choose New Item.
Add a Resources File, and then name it Client.de-DE.resx.
Important
You must always name localized resource files Client.LocaleID.resx, where LocaleID is the Windows Locale ID for the language that you’re targeting, and the files must be in the root node of the client project. If you chose a language other than German, enter the Locale ID for that language instead of de-DE, and provide your own translations in the next step.
Add the following values to the resource file:
Name
Value
AddContact
Einen Newen Kontakt erstellen
Text
Fügen Sie die Kontaktinformationen hinzu und speichern Sie dann.
These strings will appear when you run the application on a computer that's set to German (or whatever language you chose). If you want to localize into multiple languages, add a resource file for each additional language.
Note
For a Silverlight client that’s localized into multiple languages, the active Windows language pack determines which language will appear.
To call a resource from code
In Solution Explorer, switch to File View, open the Service.resx file, and then add the following values to it:
Name
Value
ErrorMessage
Name can’t contain an exclamation mark.
Open the Service.de-DE.resx file, and then add the following values:
Name
Value
ErrorMessage
Name darf keine Ausrufezeichen enthalten.
In Solution Explorer, switch to Logical View, and then open the Contacts entity.
In the Entity Designer, in the Write Code list, choose the Contacts_Validate method.
Note
Make sure that the entity itself is selected and not an entity field, otherwise a build error will occur.
In the Code Editor, add an Imports or using statement:
Imports My.Resources
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Resources; using System.Text; using Microsoft.LightSwitch; using Microsoft.LightSwitch.Security.Server; namespace LightSwitchApplication
Add the following code to the Contacts_Validate method:
If entity.ContactName.Contains(“!”) Then results.AddEntityError(Service.ErrorMessage) End If
if (entity.ContactName.Contains("!")) { ResourceManager serviceResources = new ResourceManager( "LightSwitchApplication.Service", Assembly.GetExecutingAssembly()); results.AddEntityError(serviceResources.GetString("ErrorMessage")); }
Now you can run the application, enter a new contact name that contains an exclamation mark, save, and verify that the error message appears. You can also deploy the application to a computer that's set to German and verify that the message appears in German.
To localize the HTML client
In Solution Explorer, switch to Logical View, and then open the BrowseContacts screen.
In the Screen Designer, choose the Show Add Edit Contact node.
In the Properties window, choose the Display Name property, and then enter $(add).
In Solution Explorer, switch to File View.
Expand the Localization Sample.HTMLClient node, open the shortcut menu for the Content node, choose Add, and then choose New Folder.
Name the folder Resources.
Open the shortcut menu for the Resources node, choose Add, and then choose New Item.
Add a Resources File (.resjson) item, and then name it client.lang-en-US.resjson.
Important
In any HTML client for a LightSwitch application, you must always name the default file for language resources client.lang-LocaleID.resjson, where LocaleID is the Windows Locale ID for the language on your development computer. Unlike the Silverlight client, the HTML client doesn't have the concept of a default language.
In the Code Editor, replace the code with the following:
{ “add” : “Add a Contact”, “errorMessage” : “Name can’t contain an exclamation mark.“ }
Open the shortcut menu for the Resources node, choose Add, and then choose New Item.
Add another Resources File (.resjson) item, and then name it client.lang-de-DE.resjson.
In the Code Editor, replace the code with the following:
{ “add” : “Hinzufügen eines Kontakts”, “errorMessage” : “Name darf keine Ausrufezeichen enthalten.“ }
In Solution Explorer, switch to Logical View, open the shortcut menu for the Localization Sample.HTMLClient node, and then choose Set as StartUp Client.
Open the AddEditContact screen, and then, in the Screen Designer, in the Write Code list, replace the code in the beforeApplyChanges method with the following:
myapp.AddEditContact.beforeApplyChanges = function (screen) { if (screen.Contact.ContactName.indexOf('!') != -1) { screen.findContentItem("ContactName").validationResults = [ new msls.ValidationResult( screen.Contact.details.properties.ContactName, WinJS.Resources.getString("/client/errorMessage").value ) ]; return false; } };
Now you can run the application and verify that the string for the Add a Contact button and the error message appear correctly. You can also deploy the application to a computer with a browser that's set to German and verify that the localized strings appear.
Note
For an HTML client that’s localized into multiple languages, the browser language setting determines which language will appear.
Next Steps
That’s all you must do to localize a LightSwitch application. You may have noticed that many parts of the user interface, such as the screen command bar and the navigation menu, are localized automatically. You can override the automatic translations by adding resource identifiers to the Display Name or Description properties of the appropriate elements. In fact, you can localize just about any part of your LightSwitch application by using the techniques that you’ve just learned.