Getting Started
Work with Translation Manager
1. Add Tlumach to your project:
a) via NuGet
Add a package reference to "Tlumach" to your project
via NuGet package manager GUI in Visual Studio
via the command line:
dotnet add package Tlumach
- using the text editor - add the following reference to your project:
<ItemGroup>
<PackageReference Include="Tlumach" Version="1.*" />
</ItemGroup>
b) with Source Code
- Check out Tlumach from the Tlumach repository on GitHub
- Add Tlumach.Base and Tlumach 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
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. Include translation files into your project
The minimal addition you need to make to your project are the "strings.cfg" file and the "strings.toml" file from the previous step.
Add "strings.cfg" and "strings.toml" to the project as Embedded Resource. You can use the IDE for this or edit the project file as text and add these lines:
<ItemGroup>
<EmbeddedResource Include="strings.cfg" />
<EmbeddedResource Include="strings.toml" />
</ItemGroup>
Alternatively, if you plan to load translations from the disk, you can both files as Content and specify that they should be copied to the output directory:
<Content Include="strings.cfg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="strings.toml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
But then, you will need to set LoadFromDisk property to true (you will create an instance of TranslationManager in your code as described below).
5. Initialize parsers
Before you can load a configuration and create a translation manager, you need to initialize the parsers for the formats you are using. In our sample, we use INI format for settings and TOML format for translations. So, you need to call the Use method of those two parsers:
IniParser.Use();
TomlParser.Use();
6. Create TranslationManager
If files are included as resources, just call :
using System.Reflection;
...
TranslationManager translationManager = new TranslationManager(Assembly.GetExecutingAssembly(), "strings.cfg");
If files are located on the disk, use :
TranslationManager translationManager = new TranslationManager("strings.cfg");
translationManager.LoadFromDisk = true;
7. Use translations in your code
The "hello" string from "string.toml" can be loaded via GetValue(string):
string helloValue = translationManager.GetValue("hello").Text;
To access the translation for a specific culture (e.g., in a server application), use the GetValue(string, CultureInfo) overload:
string helloValue = translationManager.GetValue("hello", new CultureInfo("de-DE")).Text;
To switch current language (the one used for the GetValue(string) method), assign a new value to CurrentCulture:
CultureInfo deCulture = new CultureInfo("de-DE");
translationManager.CurrentCulture = deCulture;
Remember that you need locale-specific files for other languages. For this, read about Translation Files and Formats