Localizable vs. Non-localizable Resources
Localizable resources are resources that require translation, e.g. strings for the user interface. By Smartsite convention, localizable resources should be embedded in satellite assemblies, each containing the translated resources for a specific culture. The satellite resource libraries are placed in separate subfolders of the main assembly, one folder per supported culture. Resources embedded in a satellite assembly may be overridden or extended by a custom xml resource file placed in the same folder as the satellite assembly.
Non-localizable resources are culture-independent and are never translated, e.g. filenames, XML files, bitmaps, icons, etc. Such resources are embedded into the main assembly and cannot be overridden or extended by a custom resource file.
A. To add a resource file to a project:
- Open the Solution Explorer
- Open the context menu of the project
- Select Add, New Item…
- Select Resource File
- Enter a name for the resource file
- Select OK
- Double click on the new resource file to open the Resource Editor
By default, Visual Studio will embed the resource file into the assembly and use the standard ResXFileCodeGenerator
custom tool to generate an associated strongly-typed resource class.
B. To add a localizable resource file to a project:
- Add a resource file as described in (A)
- Select the resource file in the Solution Explorer
- Open the Properties Window
- Change the value of the property Custom Tool to
SmartsiteResXFileCodeGenerator
(requires a reference toSmartsite.Tools
) - Open the Project Properties, tab Application, Assembly Information
- Combo Neutral Language, select the language of the default resources (en-US)
- Save and close the Project Properties
- Make a copy of the resource file (using Explorer)
Assign the same filename as the original resource file, but prepend the name of another culture (i.e.Resources.nl-NL.resx
) to the extension. - Switch to Visual Studio and open the Solution Explorer
- Click on Show All Files
- Select the newly made copy of the resource file
- Open the context menu and select Include in Project
Repeat steps 5-9 to add translated resources for other cultures. When the application is compiled, Visual Studio will create subfolders below the main assembly containing satellite resource assemblies for each available culture. When the application requests a localized resource, the resource manager will return a resource value from the satellite assembly that has the closest match to the current UI culture.
The embedded resource will serve as the ultimate fall-back resource; if a resource is requested for a certain culture but no matching resource satellite assembly exists, then the manager returns a value from the embedded neutral resource file.
Because the neutral resource file uses the Smartsite custom tool, the associated strongly-typed resource class internally creates a SmartsiteResourceManager
, allowing the embedded resources to be overriden/extended by a custom xlm resource file.
C. To expose resources from a class library and consume them from a client app:
- Create a class library project
- Add localized resource file for one or more cultures as descibed in (B)
- Ensure that the neutral resource file uses the
SmartsiteResXFileCodeGenerator
custom tool. TheSmartsiteResXFileCodeGenerator
custom tool declares the generated resource class and it's properties with public scope, so they are visible to external projects. - Create a client project
- Add a (project) reference to the previously created resource library
- Now you can retrieve resources exposed by the public resource class in the referenced resource library.
Note: Smartsite Source Control contains a solution ResLibDemo in the folder Test Projects that demonstrates the different methods.