• 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

Xml File

Example of .xml localization file.

<?xml version="1.0" encoding="UTF-8"?>
<Localization xmlns:Culture="urn:lexical.fi:Culture" 
              xmlns:Type="urn:lexical.fi:Type" 
              xmlns:Key="urn:lexical.fi:Key">
  <Type:ConsoleApp1.MyController>
    <Key:Success>Success</Key:Success>
    <Key:Error>Error (Code=0x{0:X8})</Key:Error>
  </Type:ConsoleApp1.MyController>
  <Culture:en>
    <Type:ConsoleApp1.MyController>
      <Key:Success>Success</Key:Success>
      <Key:Error>Error (Code=0x{0:X8})</Key:Error>
    </Type:ConsoleApp1.MyController>
  </Culture:en>
  <Culture:fi>
    <Type:ConsoleApp1.MyController>
      <Key:Success>Onnistui</Key:Success>
      <Key:Error>Virhe (Koodi=0x{0:X8})</Key:Error>
    </Type:ConsoleApp1.MyController>
  </Culture:fi>
</Localization>

Keys can be defined with xml attributes.

<?xml version="1.0" encoding="UTF-8"?>
<Localization xmlns:Culture="urn:lexical.fi:Culture">
  <Culture:sv Type="ConsoleApp1.MyController" Key="Success">Det Funkar</Culture:sv>
  <Culture:sv Type="ConsoleApp1.MyController" Key="Error">Det funkar inte (Kod=0x{0:X8})</Culture:sv>
</Localization>

And with "Line" xml element.

<?xml version="1.0" encoding="UTF-8"?>
<Localization xmlns="urn:lexical.fi">
  <Line Culture="sv" Type="ConsoleApp1.MyController" Key="Success">Det Funkar</Line>
  <Line Culture="sv" Type="ConsoleApp1.MyController" Key="Error">Det funkar inte (Kod=0x{0:X8})</Line>
</Localization>

If a parameter occurs more than once as attribute, then consecutive attributes are suffixed with "_#", e.g. Section="value" Section_1="value" Section_2="value".

<Culture:sv Section="Controllers" Type="MyController" Section_1="Errors" Key="Error">
    Det funkar inte (Kod=0x{0:X8})
</Culture:sv>

Code example:

// Read .xml file as lines
IEnumerable<ILine> lines = 
    XmlLinesReader.Default.ReadLines("localization.xml");

// Read .xml file as string-key lines
IEnumerable<KeyValuePair<string, IString>> stringlines =
    XmlLinesReader.Default.ReadUnformedLines("localization.xml", LineFormat.Parameters);

// Read .xml as tree
ILineTree tree = XmlLinesReader.Default.ReadLineTree("localization.xml");

// Read IAsset
IAsset asset = XmlLinesReader.Default.FileAsset("localization.xml");

Escape Rules

Colon : doesn't need escaping, but to use ':' in key, <Line> xml element must be used.

ILine key = root.Key("X:Y").Format("Text:Text");
<Line Key="X:Y">Text:Text</Line>


New line in key is escaped as &#10;. New line in value is as is, however, new line before and after first non-whitespace is trimmed out.

ILine key = root.Key("X\nY").Format("line1\r\nline2");
<Line Key="X&#10;Y">line1
line2
</Line>


Back-slashes don't need escaping.

ILine key = root.Key("X\\Y").Format("\\Folder\\File");
<Line Key="X\Y">\Folder\File</Line>


Control characters are escaped with pattern &#ii;.

ILine key = root.Key("\u0000\u0001").Format("\u0002\u0003");
<Line Key="&#0;&#1;">&#2;&#3;</Line>


Quotes are escaped as &quot; in the xml attributes that keys use.

ILine key = root.Key("\"String\"").Format("\"In quotes\"");
<Line Key="&quot;String&quot;">"In quotes"</Line>


Braces { and } don't need escaping.

ILine key = root.Key("").Format("Hello mr. {0}");
<Key:String>Hello mr(s). {0}</Key:String>

However, if braces are not intended to formulate arguments, then they are escaped with back-slashes \.

ILine key = root.Key("").Format("\\{In curly brackets\\}");
<Key:String>\{In curly brackets\}</Key:String>
Back to top Copyright © 2015-2020 Toni Kalajainen