The .NET Framework for Silverlight uses a hub-and-spoke model to package and deploy resources. The hub is the main assembly that contains the nonlocalizable executable code and the resources for a single culture, which is called the neutral or default culture. The default culture is the fallback culture for the application. The spokes connect to satellite assemblies, and each satellite assembly contains the resources for a single supported culture, but does not contain any code. At run time, a resource is loaded from the appropriate resource file, depending on the value of the application's current user interface (UI) culture, which is defined by the CultureInfo..::.CurrentUICulture [ http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentuiculture(VS.95).aspx ] property.
The Hierarchical Organization of Resources
To understand how resources are loaded, it is useful to think of them as organized in a hierarchical manner. A localized application can have resource files at three levels:
At the top of the hierarchy are the fallback resources for the default culture, for example, English ("en"). These are the only resources that do not have their own file; they are stored in the main assembly.
At the second level are the resources for any neutral cultures. A neutral culture is associated with a language but not a region. For example, French ("fr") is a neutral culture.
At the bottom of the hierarchy are the resources for any specific cultures. A specific culture is associated with a language and a region. For example, French Canadian (fr-CA) is a specific culture.
When an application tries to load a localized resource, the Resource Manager does the following:
It tries to load the resource that belongs to the user's specific culture. If there is no satellite assembly for the specific culture, or if the resource cannot be found in it, the search for the resource continues with step 2.
If there is no satellite assembly for the specific culture or the resource cannot be found in it, or if resources for the cultures defined by operating-system fallback logic cannot be found, the Resource Manager tries to load the resource from the satellite assembly for the parent of the current UI culture (the neutral culture).
If the operating system provides a list of preferred fallback languages, the Resource Manager iterates the list and tries to load the resource for each culture until it finds a match. If there is no match, or if the operating system does not support fallback logic, it continues to the next step.
If that satellite assembly does not exist, or if the resource cannot be found in it, the Resource Manager loads the fallback resources from the assembly that is specified by the System.Resources..::.NeutralResourcesLanguageAttribute [ http://msdn.microsoft.com/en-us/library/system.resources.neutralresourceslanguageattribute(VS.95).aspx ] class.
If no resources are found, resources for the invariant culture are used.
Important Note: |
|---|
It is best to generalize as much as possible when deciding whether to store resources in satellite assemblies for specific cultures or for the neutral culture. For example, when you localize an application for the French (France) culture ("fr-FR"), we recommend that you store all resources that are common to French cultures in the satellite assembly for the neutral culture. This enables users of other French-speaking cultures, such as the French (Canada) culture ("fr-CA"), to access localized resources instead of the resources for the default culture. |
Including Resources in a Silverlight-based Application
Visual Studio and the .NET Framework common language runtime handle most of the details of compiling the resource files and loading the appropriate resource for the user's current culture, provided that the resource files have been named correctly and the project has been configured properly. For information about including localized resource files in your application, see How to: Add Resources to a Silverlight-based Application [ http://msdn.microsoft.com/en-us/library/dd941931(VS.95).aspx ] .
After you add the localized resource files to your application, you can use the ResourceManager [ http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager(VS.95).aspx ] class to retrieve string resources. The following example illustrates how to retrieve a string named Greeting. (The Visual Basic code relies on the My.Resources class, which wraps calls to ResourceManager [ http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager(VS.95).aspx ] methods.) When you use the ResourceManager [ http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager(VS.95).aspx ] class, you do not have to specify the resources that you want to retrieve, unless you want to retrieve localized resources other than the resources for the current UI culture. The runtime automatically tries to retrieve the correct resource based on the current thread's CultureInfo..::.CurrentUICulture [ http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentuiculture(VS.95).aspx ] property. If a culture-specific resource for the current culture cannot be found, the runtime uses the method described in The Hierarchical Organization of Resources to provide a resource.
Dim greeting As String = My.Resources.StringLibrary.Greeting
ResourceManager rm = null;
rm = new ResourceManager("SilverlightApplication.StringLibrary", Assembly.GetExecutingAssembly());
string greeting = rm.GetString("Greeting");
When the project is compiled, the resources for the default culture are included in the main assembly for the Silverlight-based application, which is included in the .xap file. In addition, the satellite assembly for each culture that is specified by the <SupportedCultures> property in the project file is included in the application's .xap file. Each satellite assembly is also listed in the <Deployment.Parts> section of the AppManifest.xml file.
In the application's .xap file, each satellite assembly is packaged in a subdirectory of the application directory. The individual satellite assemblies are all named applicationName.Resources.dll and are stored in a subdirectory whose name specifies the culture that is represented by the satellite assembly's resources. For example, resources for the German (Germany) culture ("de-DE") are stored in the de-DE subdirectory.
This approach creates a download that includes all the localizable resources of a Silverlight-based application. If an application has been localized for many cultures or the size of localized resources is significant, this method of deployment will increase the size of the application and lengthen its download time. Instead, you can create separate versions of a Silverlight-based application that target specific cultures.
Making XAML Localizable
To make your application localizable, you should also modify the strings that are defined in your application's XAML. For example, your application might include a Button [ http://msdn.microsoft.com/en-us/library/system.windows.controls.button(VS.95).aspx ] control that is defined as follows:
<Button Content="Click Me!" />
Instead of providing a single static string, you can make the XAML localizable so that the text displayed by the Button [ http://msdn.microsoft.com/en-us/library/system.windows.controls.button(VS.95).aspx ] control reflects the user's culture.
Making XAML localizable is similar to localizing code: You place all the localizable strings into a separate resource (.resx) file, and you use XAML code that extracts the strings from that file. To extract the strings, you use the {Binding} markup extension to bind a XAML property to the strongly typed wrapper that Visual Studio generates for resource files. For instructions, see How to: Make XAML Content Localizable [ http://msdn.microsoft.com/en-us/library/dd882554(VS.95).aspx ] .
You can also make XML content, rich text, non-string values, and non-dependency properties localizable. These and other scenarios are discussed in the topics listed in the See Also section at the end of this topic.