I want to be able to retrieve a language dependent string during runtime, why I created the class below. It let's me get the value in the preferred language whenever I want with getValue():
internal class ResourceString : IResourceString
{
public ResourceString(string id, ResourceManager rm, params IResourceString[] parameters)
{
Id = id;
Rm = rm;
m_Parameters = parameters ?? Array.Empty<IResourceString>();
}
public string Id { get; }
private ResourceManager Rm { get; }
private readonly IResourceString[] m_Parameters;
public string GetValue(CultureInfo cultureInfo)
{
if (m_Parameters.Count() > 0)
{
return string.Format(Rm.GetString(Id, cultureInfo), m_Parameters.Select(p => p.GetValue(cultureInfo)));
}
else
{
return Rm.GetString(Id, cultureInfo);
}
}
};
//Extension to get a specific value downstream
internal static class MlExtension
{
public static string GetValue(this IResourceString source)
{
return source.GetValue(CultureInfo.CurrentCulture);
}
}
Problem with that is, when creating the ResourceString, you have to pass the key of the Resource file (using nameof()) as a string, named id here, which is easily overlooked:
correct:
new ResourceString(nameof(Guid.Rule_Name), Guid.ResourceManager)
not correct:
new ResourceString(Guid.Rule_Name, Guid.ResourceManager)
Is there a smart way to somehow indicate, that the key has to be passed, not the value, or even force it?