The Resource Fallback Process

Release 1.0 - ...

The ResourceManager class has built-in support for resource fallback. This means that when you attempt to access a resource that doesn’t exist for the given culture, the ResourceManager attempts to “fall back” to a less specific culture. The less specific culture is the Parent of the culture, so a specific culture falls back to a neutral culture, which falls back to the invariant culture. You can think of this as inheritance for resources. This behavior ensures that you do not duplicate resources and that as your application’s resources get more specific, you need to detail only the differences from the “parent”, just as you would with class inheritance.

String Resources in Fallback and Satellite Assemblies
String Resources in Fallback and Satellite Assemblies

The fallback process enables you to create only those resources that are different from their parent. Only if the string is not present in the main fallback assembly is an exception thrown; this makes sense because to ask for a string that doesn’t exist is clearly a programmer error.

The NeutralResourcesLanguage attribute enables you to declare the culture of the main assembly. The purpose of this is to save unnecessary searching for resource assemblies. You use it like this:

Smartsite SXML CopyCode image Copy Code
[assembly: NeutralResourcesLanguageAttribute("en-US")]

This line can go anywhere (for example, at the top of Form1.cs), but it is best placed with other similar assembly attributes in AssemblyInfo.cs. With this attribute in place when CurrentUICulture equals “en-US”, ResourceManager.GetString looks in the main assembly immediately without performing unnecessary lookups in the “en-US” or “en” satellite folders. In Visual Studio 2005, you can set this same attribute in the Assembly Information dialog (click the Assembly Information… button in the Application tab of the project’s properties).