codingseb / Localization

A suite to localize C# and WPF projects easily based on file format you choose.
MIT License
37 stars 6 forks source link

Plural translations #8

Open rukshanfonseka opened 11 months ago

rukshanfonseka commented 11 months ago

There isn't an example of a plural translation using the Tr and bindings. How can I do this?

codingseb commented 11 months ago

Yes it need more documentation but in short : you can split a translation with | and specify an integer model to choose the translation to use. The translation can be split in 2 or in 3 depending if you want a specific text when the model is 0. you can also use {model} to inject the value in the text.

Example.loc.json

{
    "PluralizedTextIdWithZero": {
        "en": "No element found | One element found | {model} elements found",
        "...":"..."
    },
    "PluralizedTextId": {
        "en": "element | elements",
         "...":"..."
    }
}

In C#

Loc.Tr("PluralizedTextIdWithZero", 0); // --> No element found
Loc.Tr("PluralizedTextIdWithZero", 1); // --> One element found
Loc.Tr("PluralizedTextIdWithZero", 2); // --> 2 elements found
Loc.Tr("PluralizedTextIdWithZero", 10); // --> 10 elements found

Loc.Tr("PluralizedTextId", 0); // --> element
Loc.Tr("PluralizedTextId", 1); // --> element
Loc.Tr("PluralizedTextId", 2); // --> elements
Loc.Tr("PluralizedTextId", 10); // -->elements

In XAML

<!-- With static model -->
<TextBlock Text="{Tr PluralizedTextIdWithZero, Model=1}" />

<!-- With dynamic bindable model (DataContext) -->
<TextBlock Text="{Tr PluralizedTextIdWithZero, ModelBinding={Binding MyObservableCollection.Count}}" />