Skip to content

Plural translations #8

Open
Open

Description

@rukshanfonseka

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

Activity

  1. added
    documentation
    enhancement
    on Sep 28, 2023
  2. self-assigned this
    on Sep 28, 2023
  3. codingseb commented on Sep 28, 2023

    @codingseb
    Owner

    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}}" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

documentationenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions