Binding Resource Key ( in a Multilingual app) with StringFormat option.

N_BauaN_Baua INMember ✭✭✭✭✭

Hi guys,

Has anyone tried binding the resource Keys with the StringFormat options?
I have a label like :

<Label Text="{Binding CurrAge, StringFormat='Current age: {0}'}" />

While the above works goof for English version.
I am planning to add Resource keys for a multi-lingual application.
I do not know how to use both StringFormat and ResourceKey at once,

For example

// keyCurrAge is key which holds value => 'Current age: {0}'
<Label Text="{Binding Resources[keyCurrAge]}" />

I tried following, but not working

<Label Text="{Binding Resources[keyCurrAge] , StringFormat='Current age: {0}'}" />

Any ideas, thoughts? (Other than using two different labels, please)

Regards,
N Baua

Answers

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    I'm using resx files for translating my app, and i had the same problem. I don't know if it will help you, but this is how it works for me:

    in my resx file i have for example this definition:

      <data name="FrageNr" xml:space="preserve">
        <value>Frage: {0}</value>
      </data>
    

    In the xaml i then have this (where i18n:Translate is an IMarkupExtension that gets the translated text from the correct resx file):

    <Label Text="{Binding Nr, StringFormat={i18n:Translate FrageNr}}"

    So with your code i'd try something like

    <Label Text="{Binding CurrAge, StringFormat={Resources[keyCurrAge]}}"

    Hope this helps.

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @DirkWilhelm ,

    No Sir, that;s not working.

    It gave me error like this
    Position 47:24. Type Resources[keyCurrAge] not found in xmlns http://xamarin.com/schemas/2014/forms

    Even though I have added the resource key in all the Resx files and clean-rebuild the project.
    [ Could you just test this on a sample app, please! , I am at a loss of ideas now, :wink: ]

    Thanks for your suggestion though.

    Regards,
    N Baua

  • hanumantgadehanumantgade Member

    Below solution worked for my code.. Thanks @DirkWilhelm .

    <Label Text="{Binding Nr, StringFormat={i18n:Translate FrageNr}}"

Sign In or Register to comment.