Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to translate my label with Binding an enum type in a ListView

Bigoudi88Bigoudi88 Member ✭✭

Hi,
I try to show some text in my label who's from a class with enum values then i need to translate it, I use this in my xaml :
xmlns:translator ="clr-namespace:MultilingualSample.Droid.Helpers"

Then I try this but it doesn't work:

Any Idea ? Thanks a lot !!!!

Tagged:

Best Answer

  • Bigoudi88Bigoudi88 Member ✭✭
    edited August 4 Accepted Answer

    Sorry i found it !!!

    I creared this class:

     public class LanguageConverter : IValueConverter
        {
            const string ResourceId = "CanyonProject.AppResources.AppResources";
            ResourceManager resmgr = new ResourceManager(ResourceId, typeof(TranslateExtension).GetTypeInfo().Assembly);
    
    
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return resmgr.GetString(value.ToString(), CultureInfo.CurrentUICulture);
            }
    
    
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    

    Then i add this to my xaml

     <ContentPage.Resources>
            <ResourceDictionary>
                <converters:LanguageConverter x:Key="translationok" />
            </ResourceDictionary>
        </ContentPage.Resources>
    
    

    Then this work
    <Label Text="{Binding TypeObstacle, Converter= { StaticResource translationok}}" TextColor="Blue" FontSize="18" Grid.Column="2"/>

Answers

  • Bigoudi88Bigoudi88 Member ✭✭
    edited August 4 Accepted Answer

    Sorry i found it !!!

    I creared this class:

     public class LanguageConverter : IValueConverter
        {
            const string ResourceId = "CanyonProject.AppResources.AppResources";
            ResourceManager resmgr = new ResourceManager(ResourceId, typeof(TranslateExtension).GetTypeInfo().Assembly);
    
    
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return resmgr.GetString(value.ToString(), CultureInfo.CurrentUICulture);
            }
    
    
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    

    Then i add this to my xaml

     <ContentPage.Resources>
            <ResourceDictionary>
                <converters:LanguageConverter x:Key="translationok" />
            </ResourceDictionary>
        </ContentPage.Resources>
    
    

    Then this work
    <Label Text="{Binding TypeObstacle, Converter= { StaticResource translationok}}" TextColor="Blue" FontSize="18" Grid.Column="2"/>

Sign In or Register to comment.