liveBook · Manning

Resource file

As explained in .NET in Action, Second Edition

A resource file is an externalized application-data file containing content such as localized strings separately from source code.

A resource file keeps application content, such as sensor descriptions, outside C# source code. Code looks up a stable key instead of storing a display string. Resources can also hold non-string assets, such as images, although the lightweight .restext format holds strings only. section 12.2

To serve localized descriptions in a .NET application:

  • Create SensorNames.restext with entries such as ExhaustAirTemp=Exhaust Air Temperature. The file without a culture code supplies the default resources. section 12.2 section 12.3
  • Include *.restext as EmbeddedResource in the project file. The build embeds generated .resources data, not the original text file. section 12.2
  • Create a ResourceManager for the resource root name and assembly, then call GetString with the stable sensor name. The description no longer needs to be passed into the measurement constructor. section 12.2
  • Add SensorNames.es-MX.restext or SensorNames.ar-SA.restext with the same keys and translated values. Culture-specific resources are built into satellite assemblies. section 12.3 section 12.5.3
Listing 12.7 Using ResourceManager to get localized sensor description
using System.Resources;                                   #1
  
public class Measurement
{
  private static readonly ResourceManager s_resMan =      #2
    new ("AcController.SensorNames",                      #3
    typeof(Measurement).Assembly);                        #4
  
  public string SensorName { get; set; }
  public decimal Value { get; set; }
  public string Description {
    get => s_resMan.GetString(SensorName)!;               #5
  }
  
  public Measurement(string sensorName, decimal value)    #6
  {
    SensorName = sensorName;
    Value = value;
  }
}

The Measurement implementation shows the resource root name, assembly selection, and GetString lookup in one place. section 12.2

Do not assemble a localized sentence by concatenating a translated description and a value: word order may differ. A resource value can contain a placeholder, such as CoolantTemp=Coolant Temperature: {0} C, so each language can position the value appropriately. If that sentence belongs to a client display, client-owned resources avoid making the service dictate its layout. section 12.5.1

Test the returned descriptions for expected strings. After changing only an embedded .restext file, use dotnet build --no-incremental if the incremental build does not pick up the change. Give translators context through .restext comments; a bare string can be ambiguous. section 12.2 section 12.3 section 12.5.4

How .NET in Action, Second Edition covers it

How deep it goes
Works through file contents, project embedding, ResourceManager lookup, culture-specific assemblies, and a build pitfall; does not detail the generated resource format.
Examples
Uses sensor-name .restext entries, Spanish and Arabic translations, response tests, and a temperature label with a placeholder.
What it assumes
Assumes a C# .NET project and project-file edits; introduces .restext entries and ResourceManager lookup through a working application.
What you can do after
Apply it via ResourceManager to retrieve localized sensor descriptions from embedded resource files.
How it connects
Connects resource files to ResourceManager, embedded resources, satellite assemblies, localization, and string formatting.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.