UI Text Translation with CSharp UI

paramjit 276 Reputation points
2022-02-10T09:49:34.397+00:00

I am translating UI from one language to another. For that I added Resource files for all languages with appropriate string and values. Than I added the two classes as follows:

  public class LocalizationResourceManager : INotifyPropertyChanged
           {
            public static LocalizationResourceManager Instance { get; } = new LocalizationResourceManager();

                public string this[string text]
                {
                    get
                    {
                        return AppResources.ResourceManager.GetString(text, AppResources.Culture);
                    }
                }

                public void SetCulture(CultureInfo language)
                {
                    Thread.CurrentThread.CurrentUICulture = language;
                    AppResources.Culture = language;

                    Invalidate();
                }

                public event PropertyChangedEventHandler PropertyChanged;

                public void Invalidate()
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(null));
                }
            }


   [ContentProperty("Text")]
        public class TranslateExtension : IMarkupExtension<BindingBase>
        {
            public string Text { get; set; }
            object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
            {
                return ProvideValue(serviceProvider);
            }

            public BindingBase ProvideValue(IServiceProvider serviceProvider)
            {
                var binding = new Binding
                {
                    Mode = BindingMode.OneWay,
                    Path = $"[{Text}]",
                    Source = LocalizationResourceManager.Instance,
                };
                return binding;
            }
        }

Now I able to translate the text in Xaml with <Label Text="{helper:Translate Label_Text}" />. But unable to do so in C# with ` new Label(Text=translate.Text=LocalizationResourceManager.Instance["Label_Text"]}. Csharp version shows text once and never changes

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Rob Caplan - MSFT 5,427 Reputation points Microsoft Employee
    2022-02-10T19:03:07.57+00:00

    The LocationResourceManager code you provided isn't the issue: the difference in behavior is based on how you call that code. It will work the same from C# as in Xaml if you call it in the same way. It's a bit easier in Xaml because Xaml has direct support for data binding, while C# requires you wire up the Binding explicitly.

    The XAML version works because it binds to the data, and the data binding updates when the underlying data updates:

    <Label Text="{helper:Translate Label_Text}" />.

    The C# code loads the Label.Text directly without binding the data. Since it's not bound to underlying data it doesn't listen for changes to the underlying data.

    new Label(Text=translate.Text=LocalizationResourceManager.Instance["Label_Text"]}.

    You can set up data binding from code by creating a Binding object as you do in your MarkupExtension and then assigning that to your Label. See https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.binding.-ctor?view=xamarin-forms for documentation and example code.

    label.SetBinding (Label.TextProperty, new Binding ( ... );

    --Rob


  2. 현명 지 1 Reputation point
    2022-02-15T15:04:59.913+00:00
                TranslateExtension teIR = new TranslateExtension();
                teIR.Text = AppResources.IR;
                this.RadioName3 = teIR.Text;
    
    0 comments No comments