Printing localization template
Message descriptions can be printed into a template file, which can be used as a localization file template for translations to other languages.
IMessageDescriptions can be scanned from dependency injection.
// Service collection
IServiceCollection serviceCollection = new ServiceCollection()
.AddAvalancheLocalizationService()
.AddSingleton<FileMessages>();
// Service provider
using var services = serviceCollection.BuildServiceProvider();
// Get all message description tables in dependency injection
IEnumerable<IMessageDescriptions> list = serviceCollection
.Where(sd => sd.ServiceType.IsAssignableTo(typeof(IMessageDescriptions)))
.Select(sd => (IMessageDescriptions)services.GetService(sd.ServiceType)!);
public class FileMessages : LocalizedMessageDescriptionsBase
{
public FileMessages(ILocalization? localization, ICultureProvider? cultureProvider = default) : base(localization, cultureProvider, baseCode: 0x20A10000) { }
public readonly MessageDescription Delete = new Line("Delete");
public readonly MessageDescription FileCount = new Line("File Count: {count}");
public readonly MessageDescription Bytes = new Line("Bytes");
}
.ToYamlText() and .ToYamlNode() prints IMessageDescriptions (Avalanche.Message.Localization.dll) into Yaml text.
// Print all tables
WriteLine(list.ToYamlText());
// Print one table
WriteLine(list.First().ToYamlText());
.WriteYamlFile(filePath) and .WriteYamlFiles(dirPath) write to file(s).
list.WriteYamlFile("template.l.yaml");
list.First().WriteYamlFile("template-statuscodes.l.yaml");
list.WriteYamlFiles(Directory.GetCurrentDirectory());
FileMessages:
Culture: ''
Items:
- Key: FileMessages:Bytes
TemplateFormat: BraceNumeric
Text: Bytes
- Key: FileMessages:Delete
TemplateFormat: BraceNumeric
Text: Delete
- Key: FileMessages:FileCount
TemplateFormat: BraceAlphaNumeric
Text: 'File Count: {count}'
...
On Asp.Net in Program.cs, before "app.Run();"
// Print localization template
string localizationTemplate = builder.Services
.Where(sd => sd.ServiceType.IsAssignableTo(typeof(IMessageDescriptions)))
.Select(sd => (IMessageDescriptions)app.Services.GetService(sd.ServiceType)!)
.ToYamlText();
// Write to console
System.Console.WriteLine(localizationTemplate);
The following snippet creates a .template.l.yaml when running from IDE in Debug mode. Template can be maintained in source control.
// Print localization template.l.yaml when running from IDE in Debug
if (Debugger.IsAttached && MessageDescriptionsTemplateExtensions.TryGetSourcePath(out string sourcePath) && Path.Exists(sourcePath))
{
// Write to file
serviceCollection // 'builder.Services' in Asp.Net
.Where(sd => sd.ServiceType.IsAssignableTo(typeof(IMessageDescriptions)))
.Select(sd => (IMessageDescriptions)services.GetService(sd.ServiceType)!) // 'app.Services' in Asp.Net
.WriteYamlFiles(sourcePath);
}
Full Example
Full example
using System.Collections.Generic;
using System;
using System.Globalization;
using System.Text;
using Avalanche.Localization;
using Avalanche.Message;
using Avalanche.StatusCode;
using Avalanche.Template;
using Avalanche.Utilities;
using Microsoft.Extensions.DependencyInjection;
using YamlDotNet.RepresentationModel;
using static System.Console;
using static di;
using System.Diagnostics;
class localizationtemplate
{
public static void Run()
{
{
// <11>
// Service collection
IServiceCollection serviceCollection = new ServiceCollection()
.AddAvalancheLocalizationService()
.AddSingleton<FileMessages>();
// Service provider
using var services = serviceCollection.BuildServiceProvider();
// Get all message description tables in dependency injection
IEnumerable<IMessageDescriptions> list = serviceCollection
.Where(sd => sd.ServiceType.IsAssignableTo(typeof(IMessageDescriptions)))
.Select(sd => (IMessageDescriptions)services.GetService(sd.ServiceType)!);
// </11>
// <12>
// Print all tables
WriteLine(list.ToYamlText());
// Print one table
WriteLine(list.First().ToYamlText());
// </12>
// Write to file
// <13>
list.WriteYamlFile("template.l.yaml");
list.First().WriteYamlFile("template-statuscodes.l.yaml");
list.WriteYamlFiles(Directory.GetCurrentDirectory());
// </13>
// <14>
// Print localization template.l.yaml when running from IDE in Debug
if (Debugger.IsAttached && MessageDescriptionsTemplateExtensions.TryGetSourcePath(out string sourcePath) && Path.Exists(sourcePath))
{
// Write to file
serviceCollection // 'builder.Services' in Asp.Net
.Where(sd => sd.ServiceType.IsAssignableTo(typeof(IMessageDescriptions)))
.Select(sd => (IMessageDescriptions)services.GetService(sd.ServiceType)!) // 'app.Services' in Asp.Net
.WriteYamlFiles(sourcePath);
}
// </14>
}
}
}
// <100>
public class FileMessages : LocalizedMessageDescriptionsBase
{
public FileMessages(ILocalization? localization, ICultureProvider? cultureProvider = default) : base(localization, cultureProvider, baseCode: 0x20A10000) { }
public readonly MessageDescription Delete = new Line("Delete");
public readonly MessageDescription FileCount = new Line("File Count: {count}");
public readonly MessageDescription Bytes = new Line("Bytes");
}
// </100>