ILocalizationFiles
ILocalizationFiles is table for managing file resources.
/// <summary>Localization file properties and providers.</summary>
public interface ILocalizationFiles
{
/// <summary>Localization file formats.</summary>
/// <remarks>Reference to this field can used by the instances in <see cref="FileProviders"/>.</remarks>
IList<ILocalizationFileFormat> FileFormats { get; set; }
/// <summary>File systems.</summary>
/// <remarks>Reference to this field can used by the instances in <see cref="FileProviders"/>.</remarks>
IList<ILocalizationFileSystem> FileSystems { get; set; }
/// <summary>File systems where file listings are cached. This enumerable is derived from <see cref="FileSystems"/> and contains cache decorated references.</summary>
/// <remarks>Reference to this field can used by the instances in <see cref="FileProviders"/>.</remarks>
IEnumerable<ILocalizationFileSystem> FileSystemsListCached { get; set; }
/// <summary>File name patterns without extension, e.g. "{Culture}/{Namespace}". Template text can uses parameters: "Culture", "Namespace", "Name", "Key".</summary>
/// <remarks>Reference to this field can used by file provider instances.</remarks>
IList<ITemplateFormatPrintable> FilePatterns { get; set; }
/// <summary>Explicitly assigned culture and key invariant localization files.</summary>
/// <remarks>Reference to this field can used by the instances in <see cref="FileProviders"/>.</remarks>
IList<ILocalizationFile> Files { get; set; }
/// <summary>Localization file query providers</summary>
IList<IProvider<(string? culture, string? key), IEnumerable<ILocalizationFile>>> FileProviders { get; set; }
/// <summary>Localization file query providers</summary>
IList<IProvider<(string? culture, string? key), IEnumerable<ILocalizationFile>>> FileProvidersCached { get; set; }
/// <summary>Query provider that uses optional culture and key constraints. Sources content from the providers in <see cref="FileProviders"/>. (no caching)</summary>
IProvider<(string? culture, string? key), IEnumerable<ILocalizationFile>> Query { get; set; }
/// <summary>Query provider that uses optional culture and key constraints. Sources content from the providers in <see cref="FileProviders"/>. (cached)</summary>
IProvider<(string? culture, string? key), IEnumerable<ILocalizationFile>> QueryCached { get; set; }
}
ILocalizationFiles is a property of ILocalization.
ILocalization localization = new Localization();
ILocalizationFiles localizationFiles = localization.Files;
FileFormats is a list where supported file formats are managed.
ILocalization localization = new Localization();
ILocalizationFileFormat fileformat = LocalizationFileFormatYaml.Instance;
localization.Files.FileFormats.Add(fileformat);
FileSystems is a list where supported file systems are managed.
ILocalization localization = new Localization();
ILocalizationFileSystem filesystem = new LocalizationFileSystem(AppDomain.CurrentDomain.BaseDirectory, "ApplicationRoot");
localization.Files.FileSystems.Add(filesystem);
FilePatterns is a list where supported file patterns are managed.
ILocalization localization = new Localization();
ITemplateFormatPrintable pattern = new TemplateText("Resources/{Culture}/{Key}", TemplateFormat.BraceAlphaNumeric);
localization.Files.FilePatterns.Add(pattern);
Files is a list where explicit files are managed.
ILocalization localization = new Localization();
// Create file reference
ILocalizationFile localizationFile = new LocalizationFile
{
FileName = "Resources/localization.l.yaml",
FileSystem = LocalizationFileSystem.ApplicationRoot,
FileFormat = LocalizationFileFormatYaml.Instance,
Culture = "",
Key = "Namespace"
}.SetReadOnly();
// Add localization file (reference)
localization.Files.Files.Add(localizationFile);
FileProviders and FileProvidersCached are lists where queryable file providers are managed. A cached and non-cached version of file provider must be added to both lists.
// Create localization
ILocalization localization = new Localization();
// Create provider
IProvider<(string? culture, string? key), IEnumerable<ILocalizationFile>> provider =
new ResourceManagerFileProvider(docs.Resources.Resource1.ResourceManager);
// Add provider (both are required)
localization.Files.FileProviders.Add(provider);
localization.Files.FileProvidersCached.Add(provider.ValueResultCaptured().Cached().ValueResultOpened());
// Get file reference
ILocalizationFile localizationFile = localization.FileQueryCached[("", "docs.Resources.Resource1.logo")].FirstOrDefault()!;
// Read file
byte[] data = localizationFile.ReadFully();
Query is concatenation of all the providers in the FileProviders property. This provider caches nothing and reads everything fresh from sources.
// Get default localization
ILocalization localization = Localization.Default;
// Query
IEnumerable<ILocalizationFile> files = localization.Files.QueryCached[("en", "Namespace.Apple")];
QueryCached is concatenation of all the providers in the FileProvidersCache property. This provider caches the results and the source files.
// Get default localization
ILocalization localization = Localization.Default;
// Query
IEnumerable<ILocalizationFile> files = localization.Files.QueryCached[("en", "Namespace.Apple")];
Full Example
Full example
using Avalanche.Localization;
using Avalanche.Template;
using Avalanche.Utilities;
using Avalanche.Utilities.Provider;
public class localizationfiles
{
public static void Run()
{
{
// <01>
ILocalization localization = new Localization();
ILocalizationFiles localizationFiles = localization.Files;
// </01>
}
{
// <02>
ILocalization localization = new Localization();
ILocalizationFileFormat fileformat = LocalizationFileFormatYaml.Instance;
localization.Files.FileFormats.Add(fileformat);
// </02>
}
{
// <03>
ILocalization localization = new Localization();
ILocalizationFileSystem filesystem = new LocalizationFileSystem(AppDomain.CurrentDomain.BaseDirectory, "ApplicationRoot");
localization.Files.FileSystems.Add(filesystem);
// </03>
}
{
// <04>
ILocalization localization = new Localization();
ITemplateFormatPrintable pattern = new TemplateText("Resources/{Culture}/{Key}", TemplateFormat.BraceAlphaNumeric);
localization.Files.FilePatterns.Add(pattern);
// </04>
}
{
// <05>
ILocalization localization = new Localization();
// Create file reference
ILocalizationFile localizationFile = new LocalizationFile
{
FileName = "Resources/localization.l.yaml",
FileSystem = LocalizationFileSystem.ApplicationRoot,
FileFormat = LocalizationFileFormatYaml.Instance,
Culture = "",
Key = "Namespace"
}.SetReadOnly();
// Add localization file (reference)
localization.Files.Files.Add(localizationFile);
// </05>
}
{
// <06>
// Create localization
ILocalization localization = new Localization();
// Create provider
IProvider<(string? culture, string? key), IEnumerable<ILocalizationFile>> provider =
new ResourceManagerFileProvider(docs.Resources.Resource1.ResourceManager);
// Add provider (both are required)
localization.Files.FileProviders.Add(provider);
localization.Files.FileProvidersCached.Add(provider.ValueResultCaptured().Cached().ValueResultOpened());
// Get file reference
ILocalizationFile localizationFile = localization.FileQueryCached[("", "docs.Resources.Resource1.logo")].FirstOrDefault()!;
// Read file
byte[] data = localizationFile.ReadFully();
// </06>
}
{
// <07>
// Get default localization
ILocalization localization = Localization.Default;
// Query
IEnumerable<ILocalizationFile> files = localization.Files.QueryCached[("en", "Namespace.Apple")];
// </07>
}
{
// <08>
// Get default localization
ILocalization localization = Localization.Default;
// Query
IEnumerable<ILocalizationFile> files = localization.Files.QueryCached[("en", "Namespace.Apple")];
// </08>
}
}
}