Table of Contents

Getting Started

Integration with WinUI

These steps also apply to Uno Platform applications. Uno implements the WinUI API on Android, iOS, macOS, Linux, and WebAssembly, and the Tlumach.WinUI package can be used by all targets of an Uno application: it contains the WinUI builds for net9.0-windows10.0.19041.0/net10.0-windows10.0.19041.0, which are used on Windows, and builds for plain net9.0/net10.0, which are used on all other platforms, including the mobile targets (the package has no separate Android or iOS builds). Wherever this page says "WinUI", the same applies to Uno, with one difference in XAML: Uno does not track changes in x:Bind paths that start with a static member, so bind to translation units through instance properties as described in Integration with XAML. The samples/Tlumach.Sample.Uno project shows a complete Uno application.

Threading: XAML bindings in WinUI and Uno must be updated on the UI thread. The TranslationUnit class from Tlumach.WinUI takes care of this: when the current language is changed from any thread, the units post their change notifications to the UI thread. The unit uses the synchronization context of the thread on which it was created or, if that thread has none, of the thread on which XAML first reads the unit's value.

1. Add Tlumach to your project:

a) via NuGet

Add a package reference to "AlliedBits.Tlumach.WinUI" to your project (it brings the core package, "AlliedBits.Tlumach", with it)

  • via NuGet package manager GUI in Visual Studio

  • via the command line:

dotnet add package AlliedBits.Tlumach.WinUI
  • using the text editor - add the following reference to your project:
<ItemGroup>
    <PackageReference Include="AlliedBits.Tlumach.WinUI" Version="2.*" />
</ItemGroup>

b) with Source Code

  • Check out Tlumach from the Tlumach repository on GitHub
  • Add Tlumach.Base, Tlumach, and Tlumach.WinUI projects to your solution and reference them from your project(s).

2. Create a configuration file

Please see the detailed description of the configuration file here.

A simple configuration file for a start looks like this:

defaultFile=strings.toml
generatedClass=Strings
generatedNamespace=Tlumach.Sample

Save it to "strings.cfg".

The "strings.toml" file is a default file, i.e., a file with strings that will be retrieved by default.

3. Create a default translation file

Here is the minimal translation file in TOML format:

hello="Hello!"

Save it to "strings.toml".

4. Create and set up a translation project

This is a C# project separate from your main code project(s); this translation project does not need any source code in it.

Please see the detailed description of setting up a translation project here.

The minimal addition you need to make to the new empty project are the "strings.cfg" file and the "strings.toml" file from the previous step.

Add "strings.cfg" to the project as an additional file. You can use the IDE for this or edit the project file as text and add these lines:

<ItemGroup>
    <AdditionalFiles Include="strings.cfg" />
</ItemGroup>

Next, add "strings.toml" to the project as Embedded Resource:

<ItemGroup>
    <EmbeddedResource Include="strings.toml" />
</ItemGroup>

Alternatively, if you plan to load translations from the disk, you can add a file as Content, but then, you will need to set LoadFromDisk property to true. The TranslationManager instance will be accessible to you as a static object named "Tlumach.Sample.Strings.TranslationManager".

Important: In WinUI, you also need to add the following lines to your translation projects to tell Generator from which namespace to take the TranslationUnit class (Tlumach.WinUI has its own class with this name):

<PropertyGroup>
    <TlumachGeneratorUsingNamespace>Tlumach.WinUI</TlumachGeneratorUsingNamespace>
</PropertyGroup>
<ItemGroup>
    <!-- Makes the property visible to analyzers/generators -->
    <CompilerVisibleProperty Include="TlumachGeneratorUsingNamespace" />
</ItemGroup>

5. Add required references to your translation project

If you use NuGet, add a package reference to the AlliedBits.Tlumach.WinUI package to your translation project. The generated class uses the TranslationUnit class of Tlumach.WinUI (set by TlumachGeneratorUsingNamespace above), and the package brings the core package, "AlliedBits.Tlumach", and Generator with it:

<ItemGroup>
    <PackageReference Include="AlliedBits.Tlumach.WinUI" Version="2.*" />
</ItemGroup>

Because of this reference, the translation project must target net9.0 or later (for example, the same target framework as your application); Tlumach.WinUI is not built for .NET Standard.

If you are using Tlumach Source Code, add project references as follows:

    <ItemGroup>
        <ProjectReference Include="Tlumach\src\Tlumach.Generator\Tlumach.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
		<ProjectReference Include="Tlumach\src\Tlumach.WinUI\Tlumach.WinUI.csproj" />
        <ProjectReference Include="Tlumach\src\Tlumach.Base\Tlumach.Base.csproj" />
        <ProjectReference Include="Tlumach\src\Tlumach\Tlumach.csproj" />
    </ItemGroup>

6. Reference the translation project from your main project

7. Build translation project

This step is needed so that Generator creates the source code with generated translation units, which you will reference in your code.

8. Use generated translation units in your WinUI project

The "hello" string from "strings.toml" is available in your main project as a static object named "Tlumach.Sample.Strings.hello", to which you can bind your XAML attributes.

Switching languages

To switch current language (the one used in the XAML bindings), assign a new value to CurrentCulture:

using Tlumach.Sample;
...
CultureInfo deCulture = new CultureInfo("de-DE");
Strings.TranslationManager.CurrentCulture = deCulture;

Remember that you need locale-specific files for other languages. For this, read about Translation Files and Formats