• Lexical
Show / Hide Table of Contents
  • Lexical.FileSystem
    • Introduction
    • Abstractions
      • IFileSystem
        • IFileSystem
        • IFileSystemBrowse
        • IFileSystemCreateDirectory
        • IFileSystemDelete
        • IFileSystemFileAttribute
        • IFileSystemMount
        • IFileSystemMove
        • IFileSystemObserve
        • IFileSystemOpen
      • IEvent
      • IEntry
      • IOption
      • IToken
    • FileSystem
    • VirtualFileSystem
    • MemoryFileSystem
    • EmbeddedFileSystem
    • HttpFileSystem
    • Decoration
    • IFileProvider
    • Utility
      • DisposeList
      • FileScanner
      • VisitTree
      • File Operation
  • Lexical.FileProvider
    • Introduction
    • Package
    • Package.Abstractions
    • Root
    • Zip
    • Dll
    • SharpCompress
    • SharpZipLib
    • FileScanner
    • Utils
  • Lexical.Localization
    • Introduction
    • Tutorial
    • Asset
      • IAsset
      • IStringAsset
    • Line
      • ILine
      • ILineFactory
      • ILineRoot
      • ILineFormat
      • ILineLogger
      • LineComparer
    • File
      • ILineReader
      • ILineWriter
      • Ini
      • Json
      • Xml
      • Resx
      • Resources
    • Miscellaneous
      • Plurality
      • ICulturePolicy
      • IStringFormat
      • Dependency Injection
    • Practices
      • Class Library
      • Class Library DI
      • Class Library DI opt.
  • Lexical.Utilities
    • Introduction
    • UnicodeString
    • FileScanner
    • Permutation
    • Tuples
    • StructList

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);
Back to top Copyright © 2015-2020 Toni Kalajainen