rskibbe.I18n 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package rskibbe.I18n --version 1.0.1
NuGet\Install-Package rskibbe.I18n -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="rskibbe.I18n" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add rskibbe.I18n --version 1.0.1
#r "nuget: rskibbe.I18n, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install rskibbe.I18n as a Cake Addin
#addin nuget:?package=rskibbe.I18n&version=1.0.1

// Install rskibbe.I18n as a Cake Tool
#tool nuget:?package=rskibbe.I18n&version=1.0.1

Description

An infrastructure package helping you translating "things". Install corresponding sub-packages to help you translate Winforms & WPF applications - easily. Multi-Language / internationalization apps - here we come!

Getting started

After installation, just go ahead and import the corresponding namespace:

C#

using rskibbe.I18n.Models;

Examples

From there, you can build your own Translator by using the TranslatorBuilder:

Setup

Simplest, with autodetection

If you just want to "make it work", you can easily create your Translator in 1-2 simple lines. This will use the internal autodetection feature to estimate, which techniques to use. It will prioritize Json-style loading methods - if you want to learn more about this, go to "Loading Methods". If it can't find the Json ones, it will default to the first ones to find.

   // create and save the instance for later, DI, etc.
   var translator = Translator.Builder
        .Build();

    // don't forget to store it in static Property
    // to make other helpers available
    Translator.Instance = translator;

Setting up the translator should be done as early as possible, like in some sort of bootstrapping process, or the first Form.

With readily available loaders

You can tell the Translator "how to load the available languages" and "how to load the translations" by providing existing implementations of ILanguagesLoader / ITranslationTableLoader.

For this, you can take a look at my other sub-packages like (coming soon!):

Those will provide you with basic loader implementations. After you've installed those, just go ahead and provide the corresponding implementations:

   // don't forget the imports somewhere above..
   using rskibbe.I18n.Json;

   // create and save the instance for later, DI, etc.
   var translator = Translator.Builder
        .WithLanguagesLoader<JsonLanguagesLoader>()
        .WithTranslationTableLoader<JsonTranslationTableLoader>()
        .Build();

    // don't forget to store it in static Property
    // to make other helpers available
    Translator.Instance = translator;

With custom loaders

You can tell the Translator "how to load the available languages" and "how to load the translations" by providing custom implementations of ILanguagesLoader / ITranslationTableLoader.

   // create and save the instance for later, DI, etc.
   var translator = Translator.Builder
        .WithLanguagesLoader<InMemoryLanguagesLoader>()
        .WithTranslationTableLoader<InMemoryTranslationTableLoader>()
        .Build();

    // don't forget to store it in static Property
    // to make other helpers available
    Translator.Instance = translator;

InMemoryLanguagesLoader implementation:

public class InMemoryLanguagesLoader : ILanguagesLoader
{
	// empty constructor is necessary
    public InMemoryLanguagesLoader() { }

    public Task<List<ILanguage>> LoadLanguagesAsync()
    {
        var languages = new List<ILanguage>()
        {
            new Language("Deutsch", "de-DE"),
            new Language("English", "en-US")
        };
        return Task.FromResult(languages);
    }
}

InMemoryTranslationTableLoader implementation:

public class InMemoryTranslationTableLoader : ITranslationTableLoader
{

    public List<ITranslationTable> AllTranslationTables { get; protected set; }
    
	// empty constructor is necessary
    public InMemoryTranslationTableLoader()
    {
        var someExampleTranslationKey = "frmAnmeldung.titel";

        var german = new Language("Deutsch", "de-DE");
        var germanTranslations = new Dictionary<string, ITranslation>()
            {
                { someExampleTranslationKey , new Translation(german, someExampleTranslationKey, "Titel") }
            };

        var english = new Language("English", "en-US");
        var englishTranslations = new Dictionary<string, ITranslation>()
            {
                { someExampleTranslationKey , new Translation(english, someExampleTranslationKey, "Title") }
            };

        var translationTables = new List<ITranslationTable>()
            {
                new TranslationTable(german, germanTranslations),
                new TranslationTable(english, englishTranslations)
            };

        AllTranslationTables = translationTables;
    }

    public Task<List<ITranslationTable>> LoadTranslationTablesAsync(params string[] isoCodes)
    {
        var translationTables = AllTranslationTables.Where(x => isoCodes.Contains(x.Language.Iso)).ToList();
        return Task.FromResult(translationTables);
    }
}

Preparation

Depending on your use case, you need to like execute the loading of the available languages by a call to the Translator LoadLanguagesAsync() method. Otherwise, you won't know, what languages are available, right? Something like a Form Load EventHandler could be the right place, remember to use async/await if wanted. You can then see the available languages inside the Languages-Property.

private async void Form1_Load(object sender, EventArgs e)
{
    // the next line, or if you have a direct reference, just use your reference of the translator..
    await Translator.Instance.LoadLanguagesAsync();
    // what languages are "available"
    // Translator.Instance.Languages
    // prefill UI, etc..
}

The package also provides a basic DefaultLanguageItemViewModel or a LanguageItemViewModel as base to work on.

Usage

After all setup and preparation steps are completed, you can then start using the Translator like this:

// get the current language
// var currentLanguage = Translator.Instance.Language

// change the current language
// using an implementation of ILanguage / the iso code string
// if you try to set it to the same language twice, the call will be ignored
await Translator.Instance.ChangeLanguageAsync("de-DE");

Functions & Methods

LoadLanguagesAsync

Loads the available languages provided by the ILanguagesLoader implementation. Will trigger the loading of the ILanguagesLoader.LoadLanguagesAsync method.

ChangeLanguageAsync

Both overloads willl try to change to the new target language (iso string, ILanguage impl. object). This will tigger the loading of the new Translations or skip if the language specified has already been set.

This function does not include the NewLine String itself.

Events

LanguageChanged

Obviously occurs after the languaged has been successfully changed.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on rskibbe.I18n:

Package Downloads
rskibbe.I18n.Json

An extension package providing default implementations for working with JSON file based translation

rskibbe.I18n.Ini

An extension package providing default implementations for working with Ini file based translation

rskibbe.I18n.Winforms

An extension package of rskibbe.I18n helping to translate Windows Forms (Winforms) projects.

rskibbe.I18n.Wpf

An extension package of rskibbe.I18n helping to translate WPF (Windows Presentation Foundation) projects.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.14 255 9/3/2023
1.0.13 203 3/14/2023
1.0.12 551 11/11/2022
1.0.10 720 10/29/2022
1.0.9 325 10/29/2022
1.0.8 776 10/29/2022
1.0.7 538 10/29/2022
1.0.6 612 10/28/2022
1.0.5 527 10/27/2022
1.0.4 345 10/27/2022
1.0.3 528 10/27/2022
1.0.2 865 10/24/2022
1.0.1 519 10/24/2022
1.0.0 540 10/24/2022