• 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

Localization Asset

This tutorial shows how to load language strings from an external file.

Add new text file "HelloWorld.ini" into the C# project.

Add new

new .ini


Paste the following text to the HelloWorld.ini, and then save the document.

Type:TutorialProject.Program:Key:Hello = Hello World!

[Culture:en]
Type:TutorialProject.Program:Key:Hello = Hello World!

[Culture:fi]
Type:TutorialProject.Program:Key:Hello = Hei Maailma!

[Culture:sv]
Type:TutorialProject.Program:Key:Hello = Hej världen!

[Culture:de]
Type:TutorialProject.Program:Key:Hello = Hallo Welt!


Go to properties of HelloWorld.ini and change Copy to Output Directory to Copy always. Now the file will be copied to the .exe folder.

Copy always


Next, open the Program.cs and modify the code to the following.

using Lexical.Localization;
using Lexical.Localization.Asset;
using System;
using System.Globalization;

namespace TutorialProject
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Create a loader
            IAsset asset = IniLinesReader.Default.FileAsset("HelloWorld.ini");

            // Add asset to global singleton instance
            LineRoot.Builder.AddAsset(asset);
            LineRoot.Builder.Build();

            // Take reference of the root
            ILineRoot root = LineRoot.Global;

            // Create key
            ILine key = root.Type<Program>().Key("Hello").Format("Hello World!");

            // Print with current culture
            Console.WriteLine(key);

            // Print with other cultures
            CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de");
            Console.WriteLine(key);

            CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fi");
            Console.WriteLine(key);

            CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("sv");
            Console.WriteLine(key);

            Console.ReadKey();
        }
    }
}

Now run the program.

Hallo Welt

Back to top Copyright © 2015-2020 Toni Kalajainen