xamlxamarinxamarin.formspickeritemsource

How to show Picker items according to localization language in Xamarin.Forms xaml


A standard way to create a picker with xaml is the following.

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/picker/populating-itemssource

<Picker x:Name="picker"
        Title="Select a monkey"
        TitleColor="Red">
  <Picker.ItemsSource>
    <x:Array Type="{x:Type x:String}">
      <x:String>Baboon</x:String>
      <x:String>Capuchin Monkey</x:String>
      <x:String>Blue Monkey</x:String>
      <x:String>Squirrel Monkey</x:String>
      <x:String>Golden Lion Tamarin</x:String>
      <x:String>Howler Monkey</x:String>
      <x:String>Japanese Macaque</x:String>
    </x:Array>
  </Picker.ItemsSource>
</Picker>

Instead of using hardcode text as "Baboon", "Capuchin Monkey", etc. I want to use localization strings as extension markup (x:Static).

Use of static markup

Default language resource

Spanish language resource

How can I achive that?


Solution

  • A workaround is add the items directly to the picker when the View is instantiated.

    1-) Normal localization resources,

    default language "en-US"

    English resources

    Spanish resources

    2-) Declare a basic picker

    3-) Populate the picker in the corresponding view controller

    Result:

    English

    Spanish