Wednesday, 10 November 2021

Xamarin Forms - Translating drawer menu items at runtime

I'm following the exelent tutorial (Link) on Multilingual in Xamarin.Forms

Everything is working well but I have one issue.

In my application I'm using Navigation drawer by Syncfusion as I'm generating the menu Items in a ListView as seen below.

DrawerPage.xaml

<ListView x:Name="listView"
                  SelectionMode="Single"
                  RowHeight="70"
                  ItemSelected="listView_ItemSelected"
                  SeparatorColor="Transparent">

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Margin="32,10,0,0" 
                                         VerticalOptions="Center" >

                                <Grid RowDefinitions="1*, Auto">

                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="50"/>
                                        <ColumnDefinition Width="auto"/>
                                    </Grid.ColumnDefinitions>

                                    <Label Grid.Row="0"
                                           Grid.Column="0" 
                                           HorizontalTextAlignment="Start"
                                           HorizontalOptions="Start"
                                           FontSize="25"
                                           FontFamily="Material-Outlined"
                                           Style="{StaticResource IconLabelStyle}"
                                           Text="{Binding Icon}" />

                                    <Label Grid.Row="0"
                                           Grid.Column="1"
                                           HorizontalTextAlignment="Start"
                                           HorizontalOptions="Start"
                                           Margin="10,0,0,0" 
                                           Text="{Binding Name}" 
                                           FontSize="16"/>

                                </Grid>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

DrawerPage.cs

private static string MAP = Lang.ResourceManager.GetString("Map");
private static string MAPICON = IconFont.ResourceManager.GetString("LocationOn");
private static string SETTINGS = Lang.ResourceManager.GetString("Settings");
private static string SETTINGSICON = IconFont.ResourceManager.GetString("Settings");

public partial class DrawerPage : ContentPage {
    drawerNavItems();
}

private void drawerNavItems()
{
    List<MenuItem> itemList = new List<MenuItem>();
    itemList.Add(new MenuItem { Icon = MAPICON, Name = MAP });
    itemList.Add(new MenuItem { Icon = SETTINGSICON, Name = SETTINGS });
    listView.ItemsSource = itemList;
 }

The issue I'm having is that I don't understand how I'm supposed to use the helper class in the tutorial link above to translate the drawer menu items.

In Xaml we can just translate the strings like this

Text="{helpers:Translate Support}"

But how do I do the same in the code Behind ?



from Xamarin Forms - Translating drawer menu items at runtime

No comments:

Post a Comment