• 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

Console Application

In this tutorial we create new Hello World console application that utilizes localization.


First, setup a new solution and a C# project. It can be either a .NET Framework project or .NET Core.

New Project


Then, open "Manage NuGet Packages...",

Manage Nuget


... and search for NuGet package "Lexical.Localization" and click the download symbol. The other plugins such as "Lexical.Localization.Abstractions" don't need to be installed as they are automatically pulled as they are dependencies.

Add NuGet package



In the Program.cs code, replace the "Console.WriteLine("Hello World!");" with following code.

Console.WriteLine( LineRoot.Global.Type<Program>().Key("Hello").Inline("Hello World!") );
Console.ReadKey();

This needs an import to "Lexical.Localization". If you press Ctrl + . in Visual Studio, it should suggest to add that using statement. Code so far.

using Lexical.Localization;
using System;

namespace TutorialProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(LineRoot.Global.Type<Program>().Key("Hello").Inline("Hello World!"));
            Console.ReadKey();
        }
    }
}

The singleton instance LineRoot.Global is one of the ways get a reference to a ILineRoot. Localization keys are built from a root instance (ILine).

The key in the exmaple is appended with two parts that contribute to key identity. The first part .Type<Program>() creates a type section with value "TutorialProject.Program" which is derived from the class name. Sections give hints for grouping localization strings in langauage string files.

The second part .Key("Hello") creates the leaf part of the key with identifier "Hello". The combined identifier is "TutorialProject.Program:Hello" if name policy had colon as separator.

The last call .Inline("Hello World!")) adds an inlined string for root culture "". It would be possible to add more inlines, and with specific cultues, for example .Inline("sv", "Hej!")). Note that, inlining doesn't modify the identity of the key. It is recommended that default values are written for the "" root culture and not for "en" english culture, even if the default strings are in english language.



Run the application (F5), you should still see the "Hello world". But this time the string is localized, and can be extended with other languages.

Run



View at end of tutorial.

View

Back to top Copyright © 2015-2020 Toni Kalajainen