The Lng class is a static utility for managing multilingual translations of text elements in an application. It loads translations from a specified ODS file (Languages.ods) and provides methods to retrieve translations in different languages.
Mtf.LanguageService
Mtf.LanguageService.EnumsMtf.LanguageService.InterfacesMtf.LanguageService.Ods- System libraries:
System,System.Collections.Generic,System.Globalization,System.IO,System.Linq
- Type:
string - Description: The filename of the ODS file containing language translations.
- Type:
Language(enum) - Description: The default language for translations, determined by the system's current culture.
- Type:
Dictionary<(Language, string), List<string>> - Description: A dictionary containing all language elements, keyed by language and element identifier.
- Type:
ILanguageElementLoader - Description: A loader for parsing the ODS file and retrieving language elements. Defaults to
OdsLanguageElementLoader.
- Description: Retrieves a translation for a given element identifier in the default language.
- Parameters:
elementIdentifier(string): The identifier of the element to translate.index(int): The index of the translation (default is 0).
- Returns:
string: The translated text or the element identifier itself if no translation exists.
- Description: Retrieves a translation for a given element identifier in the specified language.
- Parameters:
toLanguage(Language): The target language.elementIdentifier(string): The identifier of the element to translate.index(int): The index of the translation (default is 0).
- Returns:
string: The translated text or the element identifier itself if no translation exists.
- Description: Translates a specific text from one language to another.
- Parameters:
fromLanguage(Language): The source language of the element.languageElement(string): The text to be translated.toLanguage(Language): The target language.
- Returns:
string: The translated text or the original text if no translation exists.
- Description: Sets the
DefaultLanguagefield based on the current system culture. - Returns:
- None
- Description: Retrieves a specific translation from the dictionary based on the identifier, index, and language.
- Parameters:
elementIdentifier(string): The identifier of the element.index(int): The index of the translation.language(Language): The target language (default is English).
- Returns:
string: The translated text or null if no translation exists.
InvalidOperationException- Thrown if the
Languages.odsfile is not found in the application directory.
- Thrown if the
using Mtf.LanguageService;
class Program
{
static void Main()
{
// Retrieve a translation in the default language
string translatedText = Lng.Elem("HelloWorld");
// Retrieve a translation in a specific language
string translatedInSpanish = Lng.Elem(Language.Spanish, "HelloWorld");
// Translate between languages
string translated = Lng.Translate(Language.English, "Hello World", Language.French);
Console.WriteLine(translatedText);
Console.WriteLine(translatedInSpanish);
Console.WriteLine(translated);
}
}
- Ensure the
Languages.odsfile is located in the base directory of the application. - Update the
Languages.odsfile to include new translations as needed. - Default language is automatically determined by the system's culture but can be changed programmatically.