Skip to content
JPVenson edited this page May 10, 2022 · 11 revisions

Morestachio has an Optional module that provides an way to access Translations for your template called MorestachioLocalizationService.

Morestachio translations contain 4 mayor parts:

  • An instance of IMorestachioLocalizationService
  • An Formatter to access an Translation over the formatter syntax LocalizationFormatter
  • The MorestachioLocalizationTagProvider to provide translations with the {{#Loc ""|Expression}}
  • An MorestachioCustomCultureLocalizationBlockProvider block that allows you to change the Culture midtemplate

To use the build-in module you have to call the ParserOptionsBuilder.WithLocalizationService extension method on an ParserOptionsBuilder. This will register all nesseary parts. The Method has an parameter where you should setup an instance of IMorestachioLocalizationService.

The default Implementation of IMorestachioLocalizationService is called MorestachioLocalizationService. It has a list of ITranslationResource and must be init by calling the MorestachioLocalizationService.Load method.
It also provides an optional argument transformReferences that will search in all translation entries and replaces all parts like this {Other.Key.To.Translation} with the given translation from the same culture.

Example

Key DE-DE EN-US Result-DE Result-US
KeyA Hallo Hello Hallo Hello
KeyB Welt World Welt World
KeyC {KeyA} - {KeyB} {KeyA} - {KeyB} Hallo - Welt Hello - World

To get an Translation in your Template you can ether use an string from your data and invoke the Loc("KeyA") formatter on it (ether the global or the SourceObject aware one) or you use the {{#Loc "KeyA"}} tag for it. Both the Formatter and the Tag will use the set ParserOptionsBuilder.WithCultureInfo(CultureInfo) to the the translation. If you want to use a different Culture within a certain region in the template, you can use the {{#LocCulture "DE-DE"}} ... {{/LocCulture}} block to change the culture.

You can also use numeric placeholders in your translations that can be replaced from the template:

Key DE-DE EN-US
KeyA Hallo, {0} Hello, {0}
KeyB Welt World

To replace the {0} from your template you can ether use the Loc formatters rest parameter to add any number of replacements like: {{Loc('KeyA', 'KeyB')}} our you can use the {{#LOCP expression}} ... {{/LOCP}} block.
LOCP accepts three kinds of children:

  • {{#LOCPARAM expression}}
    LOCPARAM is a single expression that will be used as replacement in your translation
  • {{#LOC expression}}
    LOC can be used to replace an placeholder from an translation
  • {{#LOCP expression}}
    LOCP can also be used as a children with the same features allowing it to be nested
    Example
{{#LOCP 'KeyA'}}
   {{#LOC 'KeyB'}}
{{/LOCP}}

will output "Hello, World" for EN and "Hallo, Welt" for DE.

be aware that all other keywords inside a LOCP will not be executed