• Avalanche
  • Avalanche.Core
  • Lexical

    Show / Hide Table of Contents
    • Avalanche.Localization
      • Introduction
      • Localization
      • LocalizationFile
      • LocalizationFiles
      • LocalizationFileSystem
      • LocalizationFileFormat
      • LocalizationLine
      • LocalizationLines
      • TemplateFormat
      • CultureProvider
      • FallbackCultureProvider
      • ResourceManager
      • LocalizationError
      • Microsoft.Extensions
        • Introduction
        • DependencyInjection
        • FileProvider
        • Logging
        • ITextLocalizer
        • IFileLocalizer
        • Localization
        • AspNetCore
      • Pluralization
        • Introduction
        • Multiple plural parameters
        • Custom PluralRules
        • Invariant Culture
        • Unit Prefix
        • IPluralRule
        • IPluralNumber
        • IPluralRules
        • CLDRs
        • Unicode.CLDR41
        • Unicode.CLDR40
      • Articles
        • Alphabet localization
        • Benchmarks
        • Caching
        • Class Library
        • Demo
        • Diagnostics
        • Embedded resources
        • Emplacement
        • File localization
        • Text localization
    • Avalanche.Message
      • Introduction
      • IMessage
      • IMessageProvider
      • IMessageDescription
      • IMessageDescriptions
      • MessageLevel
      • Message printing
      • Messages and Exceptions
      • Logging
      • Validation
      • Localization
    • Avalanche.StatusCode
      • Introduction
      • HResult
        • Introduction
        • HResult.Facilities
        • BasicMessages
        • RpcMessages
        • DispatchMessages
        • ItfMessages
        • Win32Messages
        • ClrMessages
      • System
        • Introduction
        • AccessControlMessages
        • AggregateMessages
        • AppDomainMessages
        • ArgumentMessages
        • ArgumentNullMessages
        • ArgumentOutOfRangeMessages
        • ArithmeticMessages
        • ArrayMessages
        • AssemblyMessages
        • BadImageFormatMessages
        • CodeContractMessages
        • CodePageMessages
        • CollectionsMessages
        • CompilerServiceMessages
        • CryptographyMessages
        • CultureMessages
        • DiagnosticsMessages
        • EventSourceMessages
        • ExecutionEngineMessages
        • FormatMessages
        • HostProtectionMessages
        • IOMessages
        • IndexOutOfRangeMessages
        • InteropServiceMessages
        • InvalidCastMessages
        • InvalidOperationMessages
        • IsolatedStorageMessages
        • LazyMessages
        • MarshalerMessages
        • MemoryMessages
        • MiscellaneousMessages
        • NotImplementedMessages
        • NotSupportedMessages
        • ObjectDisposedMessages
        • OperationCanceledMessages
        • OverflowMessages
        • PlatformMessages
        • PolicyMessages
        • PrincipalMessages
        • ProgramMessages
        • ReferenceMessages
        • ReflectionMessages
        • RegionMessages
        • RemotingMessages
        • ResourcesMessages
        • SecurityMessages
        • SerializationMessages
        • StackMessages
        • TaskMessages
        • TextMessages
        • ThreadingMessages
        • TimeZoneMessages
        • TypeMessages
        • XmlMessages
    • Avalanche.Template
      • Introduction
      • TemplateFormats
      • ITemplatePrintable
      • ITemplateFormatPrintable
      • ITemplateText
      • ITemplateBreakdown
      • ITemplateFormat
      • ITemplateFormats
      • Extract Arguments
      • Emplacement
    • Avalanche.Tokenizer
      • Introduction
      • IToken
      • ITokenizer
      • Tokenizers
    • Avalanche.Service
      • Introduction
      • Service
        • IService
        • Construction
        • Query
        • CancellationToken
        • CachePolicy
        • Cast
        • Scope
        • Dispose
      • Handler
        • Introduction
        • IHandler
        • Cancel
        • Recursion
        • Cyclicity
        • Delegates
        • Casting
        • Invokable
        • Handlers
      • Request
        • Introduction
        • IRequest
        • [Request]
        • [ContextParameter]
        • Print Tree
      • Dependency Injection
        • Introduction
        • ServiceRequest<T>
        • Handler
        • CachePolicy
        • CancellationToken
        • QueryLogger
      • Examples
        • Expression Example
        • Mapper Example
      • ServiceMessages
    • Avalanche.Writer
      • Introduction
      • Writer
      • Referer
      • TypeCast
      • DefaultConstructor
      • DelegateWriter
      • WriterPipe
      • ConstantWriter
      • PassthroughWriter
      • Context
      • WriterMessages
    • Avalanche.Accessor
      • Introduction
      • AccessorServices
      • List
        • ListAccessor
        • ListConstructor
        • ListDeconstructor
        • ListDefaultConstructor
        • ListGet
        • ListSet
        • ListAdd
        • ListInsert
        • ListClear
        • ListCount
        • ListIndexOf
        • ListRemoveAt
        • ListReferer
      • Map
        • MapAccessor
        • MapConstructor
        • MapDeconstructor
        • MapDefaultConstructor
        • MapSet
        • MapGet
        • MapRemove
        • MapClear
        • MapCount
        • MapReferer
      • Record
        • RecordAccessor
        • RecordConstructor
        • RecordDeconstructor
        • RecordDefaultConstructor
      • Field
        • FieldAccessor
        • FieldReader
        • FieldWriter
        • FieldReferer
      • Dependency Injection
        • Introduction
      • AccessorMessages
    • Avalanche.Emit
      • Introduction
      • TypeBuilder
      • ConstructorBuilder
      • MethodBuilder
      • PropertyBuilder
      • FieldBuilder
      • Emit
      • Utilities
    • Avalanche.DataType
      • Introduction
      • IDataTypeBase
      • IDataType
      • IRecordType
      • IFieldType
      • IUnionType
      • IListType
      • IMapType
      • IValueType
      • IEnumerationType
      • IIntegerType
      • IRealType
      • IStringType
      • IAnyType
      • DataTypeRequest
      • PrintTree
      • DataTypeMessages
    • Avalanche.FileSystem
      • Introduction
      • Abstractions
        • IFileSystem
          • IFileSystemBrowse
          • IFileSystemCreateDirectory
          • IFileSystemDelete
          • IFileSystemFileAttribute
          • IFileSystemMount
          • IFileSystemMove
          • IFileSystemObserve
          • IFileSystemOpen
        • IEvent
        • IEntry
        • IOption
        • IToken
      • FileSystem
      • VirtualFileSystem
      • MemoryFileSystem
      • EmbeddedFileSystem
      • HttpFileSystem
      • Decoration
      • IFileProvider
      • Events
      • Utilities
        • Dispose
        • File Scanner
        • Visit Tree
        • File Operation
    • Avalanche.Identity
      • Introduction
      • Identity
      • IdentityParts
      • IdentityInterner
      • IdentityComparer
      • Print Tree
      • IdentityAccessors
        • Introduction
        • TypeName
    • Avalanche.Utilities
      • Introduction
      • Collections
        • Tuples
        • StructList
        • ArrayList
        • BijectionMap
        • LocakableDictionary
        • LockableList
        • MapList
        • Pipe
        • RingQueue
        • EnumerableExtensions
        • TupleUtilities
        • ArrayUtilities
      • Comparers
        • IGraphComparer
        • IGraphComparable
        • AlphaNumericComparer
        • EnumerableComparer
        • EnumerableGraphComparer
        • ReferenceComparer
        • KeyValuePairComparer
        • DefaultComparerProvider
        • RecordComparer
      • Cloners
        • ICloner
        • IGraphCloner
        • IGraphCloneable
        • EnumerableCloner
        • FieldCloner
        • PassthroughCloner
        • RecordCloner
        • ClonerProvider
      • Dispose
        • IDisposeAttachable
        • IDisposeBelatable
      • Provider
        • Introduction
        • ProviderBase
        • Delegate
        • Concatenation
        • Cache
        • ResultCapture
        • AsReadOnly
        • AsService
      • Record
        • IRecordDescription
        • IFieldDescription
        • IConstructorDescription
        • IConstructionDescription
        • IParameterDescription
        • IRecordProviders
        • RecordDelegates
          • RecordCreate
          • RecordClone
          • RecordCopy
          • IRecordDelegates
        • FieldDelegates
          • FieldRead
          • FieldWrite
          • RecreateWith
          • IFieldDelegates
      • String
        • IEscaper
        • UnicodeString
      • Miscellaneous
        • IIdGenerator
        • Permutation
        • IReadOnly
        • IUserDataContainer
        • Void
    • Avalanche.Core
      • License
      • Order

    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();
    

    .resx file

    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);
    

    .resx file

    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>
            }
        }
    }
    
    Back to top Copyright © 2022 Toni Kalajainen, contact@avalanche.fi