IStringAsset
StringAsset is language string container. Asset is populated from different IEnumeration sources, which become effective when Load() is called.
The default way is to add a reader IEnumerable<ILine>.
var source = new List<ILine> {
LineAppender.Default.Type("MyController").Key("hello").Format("Hello World!"),
LineAppender.Default.Type("MyController").Key("hello").Culture("en").Format("Hello World!"),
LineAppender.Default.Type("MyController").Key("hello").Culture("de").Format("Hallo Welt!")
};
// Keys can be filtered
ILine filterKey = LineAppender.Default.Culture("de");
IAsset asset = new StringAsset().Add(source, "{Culture:}[Type:][Key]").Load();
foreach (var _key in asset.GetLines(filterKey))
Console.WriteLine(_key.Print(LineFormat.ParametersInclString));
Some source have keys as strings that cannot be parsed into a structured key. These assets can be added as .Add(IEnumerable<KeyValuePair<string, string>>, ILineFormat).
An instance of ILineFormatPrinter must be provided to convert requesting keys to strings that can used to match the key in the source asset. These values use CSharpFormat string format.
// Create localization source
var source = new Dictionary<string, string> {
{ "MyController:hello", "Hello World!" },
{ "en:MyController:hello", "Hello World!" },
{ "de:MyController:hello", "Hallo Welt!" }
};
// Create asset with string source
IAsset asset = new StringAsset().Add(source, "{Culture:}[Type:][Key]").Load();
StringAsset can import key-string pairs as well. .Add(IEnumerable<KeyValuePair<ILine, string>>, ILineFormat keyPolicy) adds language string source with ILine based keys.
// Create localization source
var source = new Dictionary<ILine, string> {
{ new LineRoot().Type("MyController").Key("hello"), "Hello World!" },
{ new LineRoot().Type("MyController").Key("hello").Culture("en"), "Hello World!" },
{ new LineRoot().Type("MyController").Key("hello").Culture("de"), "Hallo Welt!" }
};
// Create asset with string source
IAsset asset = new StringAsset().Add(source).Load();
Keys can be enumerated with GetAllKeys().
// Extract all keys
foreach (var _key in asset.GetUnformedLines(null))
Console.WriteLine(_key);