ResourceManager Provider
.AddResourceManagerProvider() adds file and line providers that load ResourceManager from the Assemblies in the AppDomain.
// Create localization and add resource manager provider
ILocalization localization = new Localization()
.AddResourceManagerProvider();
For text resources only ResourceManagerLineProvider.AssemblyLoader and ResourceManagerLineProvider.AssemblyLoaderCached load texts from .dlls and ResourceManagers indicated by key.
// Create localization and add resource manager provider
ILocalization localization = new Localization()
.AddLineProvider(ResourceManagerLineProvider.AssemblyLoader, ResourceManagerLineProvider.AssemblyLoaderCached);
// Get localizable text
ILocalizedText text = localization.LocalizableTextCached["docs.Resources.Resource1.Apples"];
// Print to localization
WriteLine(text.Print(CultureInfo.InvariantCulture, new object[] { 1 })); // "You've got 1 apple(s)."
For file resources only ResourceManagerFileProvider.AssemblyLoader and ResourceManagerFileProvider.AssemblyLoaderCached load files from .dlls and ResourceManagers indicated by key.
// Create localization and add resource manager provider
ILocalization localization = new Localization()
.AddFileProvider(ResourceManagerFileProvider.AssemblyLoader, ResourceManagerFileProvider.AssemblyLoaderCached);
// Get localizable file
ILocalizable<ILocalizationFile> localizableFile = localization.LocalizableFileCached["docs.Resources.Resource1.logo"];
// Localize to invariant ""
ILocalizationFile? localizationFile = localizableFile.Localize("")?.Value;
// Read file
byte[] data = localizationFile!.ReadFully();
.LocalizableTextsQueryCached[null] queries for all lines in in all ResourceManagers in the AppDomain.
// Create localization and add resource manager provider
ILocalization localization = new Localization()
.AddLineProvider(ResourceManagerLineProvider.AssemblyLoader, ResourceManagerLineProvider.AssemblyLoaderCached);
// Get localizable text
IEnumerable<ILocalizedText> texts = localization.LocalizableTextsQueryCached[null];
// Print texts
foreach(var text in texts)
WriteLine(text.Print(CultureInfo.InvariantCulture, new object[] { 1 })); // "You've got 1 apple(s)."
.LocalizableFilesQueryCached[null] queries for all files in in all ResourceManagers in the AppDomain.
// Create localization and add resource manager provider
ILocalization localization = new Localization()
.AddFileProvider(ResourceManagerFileProvider.AssemblyLoader, ResourceManagerFileProvider.AssemblyLoaderCached);
// Get localizable file
IEnumerable<ILocalizable<ILocalizationFile>> localizableFiles = localization.LocalizableFilesQueryCached[null];
// Visit each file
foreach (var localizableFile in localizableFiles)
{
// Localize to invariant ""
ILocalizationFile? localizationFile = localizableFile.Localize("")?.Value;
// Read file
byte[] data = localizationFile!.ReadFully();
}
If depependency injection is used, the .AddAvalancheLocalizationResourceManagerProvider() adds assembly loading providers.
// Add service descriptors
IServiceCollection serviceCollection = new ServiceCollection()
.AddAvalancheLocalizationService()
.AddAvalancheLocalizationResourceManagerProvider();
// Build service
using ServiceProvider service = serviceCollection.BuildServiceProvider();
// Get Localization
ILocalization localization = service.GetService<ILocalization>()!;
// Get text
ILocalizedText localizedText = localization.LocalizedTextCached[("", "docs.Resources.Resource1.Apples")];
// Print text
WriteLine(localizedText.Print(new object[] { 2 }));
// Get localizable file
ILocalizable<ILocalizationFile> localizableFile = localization.LocalizableFileCached["docs.Resources.Resource1.logo"];
// Localize to invariant ""
ILocalizationFile? localizationFile = localizableFile.Localize("")?.Value;
// Read file
byte[] data = localizationFile!.ReadFully();
Warning
The provider may a ResourceManager type that has not been loaded into AppDomain. The caller may need to preload respective Assembly beforehand, or invalidate cache after loading. Assembly is loaded by taking a reference to a Type or instantiating an object from the Assembly.
ResourceManager Instance
.AddResourceManager(resourceManager) adds a single ResourceManager instance as line and file provider.
// Get resource manager
ResourceManager resourcemanager = docs.Resources.Resource1.ResourceManager;
// Create localization and add resource manager as provider
ILocalization localization = new Localization().AddResourceManager(resourcemanager);
Resource manager can be added to the localization singleton Localization.Default just as well.
// Get resource manager
ResourceManager resourceManager = docs.Resources.Resource1.ResourceManager;
// Add resource manager to localization singleton
Localization.Default.AddResourceManager(resourceManager);
Localization provides lines from resources manager. Note however, that Pluralization cannot be supported from resource manager.
ILocalization localization = new Localization().AddResourceManager(docs.Resources.Resource1.ResourceManager);
ILocalizedText? text = localization.LocalizableTextCached["docs.Resources.Resource1.Apples"];
WriteLine(text.Print(CultureInfo.InvariantCulture, new object[] { 1 }));
.AddResourceManager(resourceManager, namespace) adds a ResourceManager with a overriding namespace.
ILocalization localization = new Localization().AddResourceManager(docs.Resources.Resource1.ResourceManager, "Namespace");
ILocalizedText? text = localization.LocalizableTextCached["Namespace.Apples"];
WriteLine(text.Print(CultureInfo.InvariantCulture, new object[] { 1 }));
Files are now accessible from ResourceManager.
// Create Localization
ILocalization localization = new Localization().AddResourceManager(docs.Resources.Resource1.ResourceManager);
// Get file reference
ILocalizationFile localizationFile = localization.FileQueryCached[("", "docs.Resources.Resource1.logo")].FirstOrDefault()!;
// Read file
byte[] data = localizationFile.ReadFully();
All files can be queried.
// Create Localization
ILocalization localization = new Localization().AddResourceManager(docs.Resources.Resource1.ResourceManager);
// Get all files
ILocalizationFile[] allLocalizationFiles = localization.FileQueryCached[(null, null)].ToArray()!;
Localizable file can localize to different cultures.
// Create Localization
ILocalization localization = new Localization().AddResourceManager(docs.Resources.Resource1.ResourceManager);
// Get localizable file
ILocalizable<ILocalizationFile> localizable1 = localization.LocalizableFileCached["docs.Resources.Resource1.logo"];
// Localize to invariant ""
ILocalizationFile? localizationFile = localizable1.Localize("")?.Value;
// Read file
byte[] data = localizationFile!.ReadFully();
Localizing returns file of active culture.
// Create Localization
ILocalization localization = new Localization().AddResourceManager(docs.Resources.Resource1.ResourceManager);
// Create localizing file
ILocalizing<ILocalizationFile> localizing = localization.LocalizingFileCached[(CultureProvider.CurrentCulture.Instance, "docs.Resources.Resource1.logo")];
// Localize to CultureProvider.CurrentCulture
ILocalizationFile? localizationFile = localizing.Value?.Value;
// Read file
byte[] data = localizationFile!.ReadFully();
If depependency injection is used, ServiceDescriptors of ResourceManager instances can be added to service collection.
// Get ResourceManager
ResourceManager resourceManager = docs.Resources.Resource1.ResourceManager;
// Add service descriptors
IServiceCollection serviceCollection = new ServiceCollection()
.AddSingleton(typeof(ResourceManager), resourceManager)
.AddAvalancheLocalizationService();
// Build service
using ServiceProvider service = serviceCollection.BuildServiceProvider();
// Get Localization
ILocalization localization = service.GetService<ILocalization>()!;
// Get text
ILocalizedText localizedText = localization.LocalizedTextCached[("", "docs.Resources.Resource1.Apples")];
// Print text
WriteLine(localizedText.Print(new object[] { 2 }));
Warning
A ILocalized<T> that originates from a ResourceManager may indicate wrong source culture. The culture property may display the requested culture instead of the actual source culture. This is due to a bug in ResourceManager.GetResourceSet(tryParents: false) which returns parent resources despite 'false' argument. The issue cannot be workarounded currently.
Full Example
Full example
using System.Globalization;
using System.Resources;
using Avalanche.Localization;
using Avalanche.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using static System.Console;
class resourcemanager
{
public static void Run()
{
{
// <11>
// Create localization and add resource manager provider
ILocalization localization = new Localization()
.AddResourceManagerProvider();
// </11>
}
{
// <13>
// Create localization and add resource manager provider
ILocalization localization = new Localization()
.AddLineProvider(ResourceManagerLineProvider.AssemblyLoader, ResourceManagerLineProvider.AssemblyLoaderCached);
// Get localizable text
ILocalizedText text = localization.LocalizableTextCached["docs.Resources.Resource1.Apples"];
// Print to localization
WriteLine(text.Print(CultureInfo.InvariantCulture, new object[] { 1 })); // "You've got 1 apple(s)."
// </13>
}
{
// <14>
// Create localization and add resource manager provider
ILocalization localization = new Localization()
.AddFileProvider(ResourceManagerFileProvider.AssemblyLoader, ResourceManagerFileProvider.AssemblyLoaderCached);
// Get localizable file
ILocalizable<ILocalizationFile> localizableFile = localization.LocalizableFileCached["docs.Resources.Resource1.logo"];
// Localize to invariant ""
ILocalizationFile? localizationFile = localizableFile.Localize("")?.Value;
// Read file
byte[] data = localizationFile!.ReadFully();
// </14>
}
{
// <15>
// Create localization and add resource manager provider
ILocalization localization = new Localization()
.AddLineProvider(ResourceManagerLineProvider.AssemblyLoader, ResourceManagerLineProvider.AssemblyLoaderCached);
// Get localizable text
IEnumerable<ILocalizedText> texts = localization.LocalizableTextsQueryCached[null];
// Print texts
foreach(var text in texts)
WriteLine(text.Print(CultureInfo.InvariantCulture, new object[] { 1 })); // "You've got 1 apple(s)."
// </15>
}
{
// <16>
// Create localization and add resource manager provider
ILocalization localization = new Localization()
.AddFileProvider(ResourceManagerFileProvider.AssemblyLoader, ResourceManagerFileProvider.AssemblyLoaderCached);
// Get localizable file
IEnumerable<ILocalizable<ILocalizationFile>> localizableFiles = localization.LocalizableFilesQueryCached[null];
// Visit each file
foreach (var localizableFile in localizableFiles)
{
// Localize to invariant ""
ILocalizationFile? localizationFile = localizableFile.Localize("")?.Value;
// Read file
byte[] data = localizationFile!.ReadFully();
}
// </16>
}
{
// <19>
// Add service descriptors
IServiceCollection serviceCollection = new ServiceCollection()
.AddAvalancheLocalizationService()
.AddAvalancheLocalizationResourceManagerProvider();
// Build service
using ServiceProvider service = serviceCollection.BuildServiceProvider();
// Get Localization
ILocalization localization = service.GetService<ILocalization>()!;
// Get text
ILocalizedText localizedText = localization.LocalizedTextCached[("", "docs.Resources.Resource1.Apples")];
// Print text
WriteLine(localizedText.Print(new object[] { 2 }));
// Get localizable file
ILocalizable<ILocalizationFile> localizableFile = localization.LocalizableFileCached["docs.Resources.Resource1.logo"];
// Localize to invariant ""
ILocalizationFile? localizationFile = localizableFile.Localize("")?.Value;
// Read file
byte[] data = localizationFile!.ReadFully();
// </19>
}
{
// <21>
// Get resource manager
ResourceManager resourcemanager = docs.Resources.Resource1.ResourceManager;
// Create localization and add resource manager as provider
ILocalization localization = new Localization().AddResourceManager(resourcemanager);
// </21>
}
{
// <22>
// Get resource manager
ResourceManager resourceManager = docs.Resources.Resource1.ResourceManager;
// Add resource manager to localization singleton
Localization.Default.AddResourceManager(resourceManager);
// </22>
}
{
// <23>
ILocalization localization = new Localization().AddResourceManager(docs.Resources.Resource1.ResourceManager);
ILocalizedText? text = localization.LocalizableTextCached["docs.Resources.Resource1.Apples"];
WriteLine(text.Print(CultureInfo.InvariantCulture, new object[] { 1 }));
// </23>
}
{
// <24>
ILocalization localization = new Localization().AddResourceManager(docs.Resources.Resource1.ResourceManager, "Namespace");
ILocalizedText? text = localization.LocalizableTextCached["Namespace.Apples"];
WriteLine(text.Print(CultureInfo.InvariantCulture, new object[] { 1 }));
// </24>
}
{
// <25>
// Create Localization
ILocalization localization = new Localization().AddResourceManager(docs.Resources.Resource1.ResourceManager);
// Get file reference
ILocalizationFile localizationFile = localization.FileQueryCached[("", "docs.Resources.Resource1.logo")].FirstOrDefault()!;
// Read file
byte[] data = localizationFile.ReadFully();
// </25>
}
{
// <26>
// Create Localization
ILocalization localization = new Localization().AddResourceManager(docs.Resources.Resource1.ResourceManager);
// Get all files
ILocalizationFile[] allLocalizationFiles = localization.FileQueryCached[(null, null)].ToArray()!;
// </26>
}
{
// <27>
// Create Localization
ILocalization localization = new Localization().AddResourceManager(docs.Resources.Resource1.ResourceManager);
// Get localizable file
ILocalizable<ILocalizationFile> localizable1 = localization.LocalizableFileCached["docs.Resources.Resource1.logo"];
// Localize to invariant ""
ILocalizationFile? localizationFile = localizable1.Localize("")?.Value;
// Read file
byte[] data = localizationFile!.ReadFully();
// </27>
}
{
// <28>
// Create Localization
ILocalization localization = new Localization().AddResourceManager(docs.Resources.Resource1.ResourceManager);
// Create localizing file
ILocalizing<ILocalizationFile> localizing = localization.LocalizingFileCached[(CultureProvider.CurrentCulture.Instance, "docs.Resources.Resource1.logo")];
// Localize to CultureProvider.CurrentCulture
ILocalizationFile? localizationFile = localizing.Value?.Value;
// Read file
byte[] data = localizationFile!.ReadFully();
// </28>
}
{
// <29>
// Get ResourceManager
ResourceManager resourceManager = docs.Resources.Resource1.ResourceManager;
// Add service descriptors
IServiceCollection serviceCollection = new ServiceCollection()
.AddSingleton(typeof(ResourceManager), resourceManager)
.AddAvalancheLocalizationService();
// Build service
using ServiceProvider service = serviceCollection.BuildServiceProvider();
// Get Localization
ILocalization localization = service.GetService<ILocalization>()!;
// Get text
ILocalizedText localizedText = localization.LocalizedTextCached[("", "docs.Resources.Resource1.Apples")];
// Print text
WriteLine(localizedText.Print(new object[] { 2 }));
// </29>
}
}
}