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
