Skip to content

Multiple versions of the same language

Johan Svensson edited this page May 16, 2023 · 1 revision

E.g. When you want to have multiple versions of the same text for different users/customers.

To solve this, I can think of a couple solutions.

  • You could use a file based ResourceManager (supported in 1.0.3) and override e.g. "LabelWelcome" for each customer. Either by downloading a customer specific ".resources" file or creating one on the fly with ResourceWriter.
.UseLocalizationResourceManager(settings =>
{
    settings.AddFileResource("CustomerResources", FileSystem.Current.AppDataDirectory);
    settings.AddResource(AppResources.ResourceManager);
    settings.RestoreLatestCulture(true);
});

Note: The order that the resources are added is important! (The library will search from "top to bottom"!)

  • Another solution, is to include all the different texts in AppResources.resx and use a Fody weaver to replace the generated ResourceManager with a custom RersourceManager that, on the fly, replaces "LabelWelcome" based on customer specific settings. For this I have use the Substitute.Fody library.

Something like this:

[assembly: Substitute(typeof(System.Resources.ResourceManager), typeof(CustomResourceManager))]
namespace MyResources
{
    public class CustomResourceManager : ResourceManager
    {
        private static Settings settings;

        public CustomResourceManager(string baseName, System.Reflection.Assembly assembly) : base(baseName, assembly)
        {
            settings = ServiceResolver.Resolve<Settings>();
        }

        public override string GetString(string name, CultureInfo culture)
        {
            var custom = settings.GetCustomText(name);
            var text = base.GetString(string.IsNullOrEmpty(custom) ? name : custom, culture);
            return text ?? base.GetString(name, culture);
        }
    }
}

Clone this wiki locally