Embedded localization files
The zero configuration way is to place embedded resources under "Resources" folder.
Localization.Default is configured to search for embedded resources under "Resources" folder.
// Add to localization
ILocalization localization = Localization.Default;
// Print
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
string print = text.Print(CultureInfo.InvariantCulture, new object[] { 1 });
WriteLine(print);
.AddFileSystemWithPattern(LocalizationFileSystemEmbedded.AppDomain, paths) adds file system that searches for embedded resources in all Assemblies in AppDomain.
// Add to localization
ILocalization localization = new Localization()
.AddFileFormats(LocalizationFileFormatYaml.Instance, LocalizationFileFormatXml.Instance, LocalizationFileFormatJson.Instance)
.AddFileSystemWithPattern(LocalizationFileSystemEmbedded.AppDomain, "*/*.Resources.{Key}", "*/*.Resources.{Culture}.{Key}", "*/{Key}", "*/{Key}.{Culture}");
// Print
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
string print = text.Print(CultureInfo.InvariantCulture, new object[] { 1 });
WriteLine(print);
LocalizationFilePatterns.ResourcesEmbedded Contains the default pattern for embdded resources.
// Add to localization
ILocalization localization = new Localization()
.AddFileFormats(LocalizationFileFormatYaml.Instance, LocalizationFileFormatXml.Instance, LocalizationFileFormatJson.Instance)
.AddFileSystemWithPattern(LocalizationFileSystemEmbedded.AppDomain, LocalizationFilePatterns.ResourcesEmbedded);
LocalizationFileSystemEmbedded(assembly) adds access to a specific assembly only.
// Get assembly
Assembly assembly = typeof(embedded).Assembly;
// Add to localization
ILocalization localization = new Localization()
.AddFileFormats(LocalizationFileFormatYaml.Instance, LocalizationFileFormatXml.Instance, LocalizationFileFormatJson.Instance)
.AddFileSystemWithPattern(new LocalizationFileSystemEmbedded(assembly), "*/*.Resources.{Key}", "*/*.Resources.{Culture}.{Key}", "*/{Key}", "*/{Key}.{Culture}");
// Print
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
string print = text.Print(CultureInfo.InvariantCulture, new object[] { 1 });
WriteLine(print);
LocalizationFileSystemEmbedded.AppDomain.PrintTree() displays resources that are made accessible for localization. This can be used for debugging.
// Print format
var format = LocalizationFileSystemPrintTreeExtensions.PrintFormat.DefaultLong | LocalizationFileSystemPrintTreeExtensions.PrintFormat.DirectorySlash;
// Print to tree
string tree = LocalizationFileSystemEmbedded.AppDomain.PrintTree(format: format);
// Write to console
WriteLine(tree);
├── docs/ │ ├── docs/docs.Resources.fi.image.png │ ├── docs/docs.Resources.fi.logo.svg │ ├── docs/docs.Resources.fi.Namespace.Welcome.l.yaml │ ├── docs/docs.Resources.fi.Namespace.l.yaml │ ├── docs/docs.Resources.fi.Resource1.resources │ ├── docs/docs.Resources.image.png │ ├── docs/docs.Resources.logo.svg │ ├── docs/docs.Resources.Namespace.Welcome.l.yaml │ ├── docs/docs.Resources.Namespace.l.yaml │ └── docs/docs.Resources.Resource1.resources
LocalizationFile can be added explicitly to localization.
// Create file reference
ILocalizationFile localizationFile = new LocalizationFile
{
FileName = "docs/docs.Resources.Namespace.l.yaml",
FileSystem = LocalizationFileSystemEmbedded.AppDomain,
FileFormat = LocalizationFileFormatYaml.Instance,
Culture = "",
Key = "Namespace"
}.SetReadOnly();
// Add to localization
ILocalization localization = new Localization().AddFile(localizationFile);
// Print
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
string print = text.Print(CultureInfo.InvariantCulture, new object[] { 1 });
WriteLine(print);
An embedded file can be read completely at initialization and have lines added explicitly.
// Get assembly
Assembly assembly = typeof(embedded).Assembly;
// Get data
using Stream stream = assembly.GetManifestResourceStream("docs.Resources.Namespace.l.yaml")!;
// Create reader
var reader = new LocalizationReaderYaml.FromStream(stream);
// Add to localization
ILocalization localization = new Localization().AddLines(reader);
// Print
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
string print = text.Print(CultureInfo.InvariantCulture, new object[] { 1 });
WriteLine(print);
Full Example
Full example
using System.Globalization;
using System.Reflection;
using Avalanche.Localization;
using Avalanche.Utilities;
using static System.Console;
class embedded
{
public static void Run()
{
{
// <01>
// Add to localization
ILocalization localization = Localization.Default;
// Print
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
string print = text.Print(CultureInfo.InvariantCulture, new object[] { 1 });
WriteLine(print);
// </01>
}
{
// <02>
// Add to localization
ILocalization localization = new Localization()
.AddFileFormats(LocalizationFileFormatYaml.Instance, LocalizationFileFormatXml.Instance, LocalizationFileFormatJson.Instance)
.AddFileSystemWithPattern(LocalizationFileSystemEmbedded.AppDomain, "*/*.Resources.{Key}", "*/*.Resources.{Culture}.{Key}", "*/{Key}", "*/{Key}.{Culture}");
// Print
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
string print = text.Print(CultureInfo.InvariantCulture, new object[] { 1 });
WriteLine(print);
// </02>
}
{
// <02B>
// Add to localization
ILocalization localization = new Localization()
.AddFileFormats(LocalizationFileFormatYaml.Instance, LocalizationFileFormatXml.Instance, LocalizationFileFormatJson.Instance)
.AddFileSystemWithPattern(LocalizationFileSystemEmbedded.AppDomain, LocalizationFilePatterns.ResourcesEmbedded);
// </02B>
// Print
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
string print = text.Print(CultureInfo.InvariantCulture, new object[] { 1 });
WriteLine(print);
}
{
// <03>
// Get assembly
Assembly assembly = typeof(embedded).Assembly;
// Add to localization
ILocalization localization = new Localization()
.AddFileFormats(LocalizationFileFormatYaml.Instance, LocalizationFileFormatXml.Instance, LocalizationFileFormatJson.Instance)
.AddFileSystemWithPattern(new LocalizationFileSystemEmbedded(assembly), "*/*.Resources.{Key}", "*/*.Resources.{Culture}.{Key}", "*/{Key}", "*/{Key}.{Culture}");
// Print
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
string print = text.Print(CultureInfo.InvariantCulture, new object[] { 1 });
WriteLine(print);
// </03>
}
{
// <14>
// Print format
var format = LocalizationFileSystemPrintTreeExtensions.PrintFormat.DefaultLong | LocalizationFileSystemPrintTreeExtensions.PrintFormat.DirectorySlash;
// Print to tree
string tree = LocalizationFileSystemEmbedded.AppDomain.PrintTree(format: format);
// Write to console
WriteLine(tree);
// </14>
}
{
// <21>
// Create file reference
ILocalizationFile localizationFile = new LocalizationFile
{
FileName = "docs/docs.Resources.Namespace.l.yaml",
FileSystem = LocalizationFileSystemEmbedded.AppDomain,
FileFormat = LocalizationFileFormatYaml.Instance,
Culture = "",
Key = "Namespace"
}.SetReadOnly();
// Add to localization
ILocalization localization = new Localization().AddFile(localizationFile);
// Print
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
string print = text.Print(CultureInfo.InvariantCulture, new object[] { 1 });
WriteLine(print);
// </21>
}
{
// <31>
// Get assembly
Assembly assembly = typeof(embedded).Assembly;
// Get data
using Stream stream = assembly.GetManifestResourceStream("docs.Resources.Namespace.l.yaml")!;
// Create reader
var reader = new LocalizationReaderYaml.FromStream(stream);
// Add to localization
ILocalization localization = new Localization().AddLines(reader);
// Print
ILocalizedText text = localization.LocalizableTextCached["Namespace.Apples"];
string print = text.Print(CultureInfo.InvariantCulture, new object[] { 1 });
WriteLine(print);
// </31>
}
}
}