Table of Contents

Class TemplateTranslator

Namespace
Tlumach.Templating
Assembly
Tlumach.dll

Retrieves translations for template engines. It resolves the culture of a call, looks up a text by key or by translation unit, fills the placeholders of the text with the values of the call, and handles missing keys.

Texts are read from translation entries without the encoding of WebEncodeValues, so that a template engine encodes them exactly once. An instance does not change after it is created and may be used by many renders concurrently.

The fallback to the values of a translation unit (the values cached with CachePlaceholderValue(string, object?) and the values supplied by OnPlaceholderValueNeeded) is process-wide: generated units are static singletons, so their subscribers and cached values are shared by all renders. It is not suitable for per-user values in concurrent renders; pass such values as arguments of the call.

public sealed class TemplateTranslator
Inheritance
TemplateTranslator
Inherited Members

Constructors

TemplateTranslator(TranslationManager, TemplateTranslationOptions?)

Initializes a new instance of the TemplateTranslator class.

public TemplateTranslator(TranslationManager manager, TemplateTranslationOptions? options = null)

Parameters

manager TranslationManager

The translation manager to take the texts from.

options TemplateTranslationOptions

The options. Their values are copied.

Properties

CultureArgumentName

Gets the name of the named argument that sets the culture of a call.

public string CultureArgumentName { get; }

Property Value

string

Manager

Gets the translation manager, from which the texts are taken.

public TranslationManager Manager { get; }

Property Value

TranslationManager

Methods

ResolveCulture(object?, CultureInfo?)

Resolves the culture of a call: the explicit culture of the call if given, otherwise the culture of the render unless it is null or invariant, otherwise CurrentCulture.

public CultureInfo ResolveCulture(object? explicitCulture, CultureInfo? renderCulture)

Parameters

explicitCulture object

The culture argument of the call: a CultureInfo, a culture name, or null.

renderCulture CultureInfo

The culture of the render as the template engine knows it, or null.

Returns

CultureInfo

The culture of the call.

ToCulture(object?)

Converts a culture given by a template to a CultureInfo.

public static CultureInfo? ToCulture(object? value)

Parameters

value object

A CultureInfo, a culture name, or null.

Returns

CultureInfo

The culture, or null for null and an empty name.

Exceptions

CultureNotFoundException

The name is not a valid culture name.

ArgumentException

The value is neither a CultureInfo nor a string.

Translate(object?, TemplateArguments, CultureInfo)

Returns the text for the key or the translation unit, with the placeholders filled, not encoded.

public string Translate(object? keyOrUnit, TemplateArguments arguments, CultureInfo culture)

Parameters

keyOrUnit object

A key (string) or a BaseTranslationUnit.

arguments TemplateArguments

The values of the placeholders.

culture CultureInfo

The culture of the call, usually obtained from ResolveCulture(object?, CultureInfo?).

Returns

string

The text.

TranslateMarkup(object?, TemplateArguments, CultureInfo, Func<string, string>)

Returns the text for the key or the translation unit as HTML: the translation is trusted HTML and is not encoded, while the values of the placeholders are encoded with encode. Strings and other objects (converted to strings) are encoded, TemplateMarkup values are inserted as they are, and numbers, dates and times are formatted for the culture and not encoded. All other values, including other IFormattable types such as Uri, are converted to text (an IFormattable is formatted for culture) and encoded.

public string TranslateMarkup(object? keyOrUnit, TemplateArguments arguments, CultureInfo culture, Func<string, string> encode)

Parameters

keyOrUnit object

A key (string) or a BaseTranslationUnit.

arguments TemplateArguments

The values of the placeholders.

culture CultureInfo

The culture of the call, usually obtained from ResolveCulture(object?, CultureInfo?).

encode Func<string, string>

The function that HTML-encodes a value.

Returns

string

The HTML.

TryTranslate(object?, TemplateArguments, CultureInfo, out string)

Returns the text for the key or the translation unit, with the placeholders filled, not encoded, if the key or the entry of the unit exists.

Unlike Translate(object?, TemplateArguments, CultureInfo), a missing key is not handled: OnMissingKey is not called and MissingKey is not applied.

public bool TryTranslate(object? keyOrUnit, TemplateArguments arguments, CultureInfo culture, out string text)

Parameters

keyOrUnit object

A key (string) or a BaseTranslationUnit.

arguments TemplateArguments

The values of the placeholders.

culture CultureInfo

The culture of the call.

text string

Upon return, the text, or an empty string if the key or the entry is missing.

Returns

bool

true if the key or the entry of the unit exists.

TryTranslateMarkup(object?, TemplateArguments, CultureInfo, Func<string, string>, out string)

Returns the text for the key or the translation unit as HTML, as TranslateMarkup(object?, TemplateArguments, CultureInfo, Func<string, string>) does, if the key or the entry of the unit exists.

A missing key is not handled: OnMissingKey is not called and MissingKey is not applied.

public bool TryTranslateMarkup(object? keyOrUnit, TemplateArguments arguments, CultureInfo culture, Func<string, string> encode, out string html)

Parameters

keyOrUnit object

A key (string) or a BaseTranslationUnit.

arguments TemplateArguments

The values of the placeholders.

culture CultureInfo

The culture of the call.

encode Func<string, string>

The function that HTML-encodes a value.

html string

Upon return, the HTML, or an empty string if the key or the entry is missing.

Returns

bool

true if the key or the entry of the unit exists.