'application doesn't update language after changing the phone's language

i have created a simple xamarin.forms app in order to learn how to make my app multilingual. i found this tutorial https://www.c-sharpcorner.com/article/support-multiple-languages-in-xamarin-forms-application/ and applied it. the thing is, when i debug the app when my phone's language is english, the button's text is "click me". but when i change the phone's language to arabic, the text's language doesn't change unless i rerun the app all over again from my visual studio. so if i open the app normally from my phone the language doesn't change even if i change my phone's language. this is my code:

mainpage.xaml.cs

  public MainPage()
        {
            InitializeComponent();
            
            btn.Text = ApplicationResource.btntxt;
        }

mainpage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App1.MainPage">

    <StackLayout>
        <Button Clicked="Button_Clicked" x:Name="btn"/>
    </StackLayout>

</ContentPage>

these are my app resources: enter image description here what should i do?



Solution 1:[1]

Not a solution but rather a hint:

1 Letting the user to change the language himself inside app. after he selects language i set the language (see below) then recreating the mainpage and the new language is used. At the same time saving the selected language code to local storage (look for "xamarin essentials preferences") and at app startup we call the method below to set the preferred language:

 public bool SetLanguage(string twoLettersCode)

        {
            try
            {
                ResStrings.Culture = CultureInfo.CreateSpecificCulture(twoLettersCode);

                //current thread only
                CurrentCulture = CultureInfo.CreateSpecificCulture(twoLettersCode);

                DependencyService.Get<ILocalize>().SetLocale(ResStrings.Culture);

                SelectedLang = twoLettersCode;

                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return false;
        }

You'd bet ResStrings is the name of my localized resources, i guess it's ApplicationResource in your case and the ILocalize can be peeked here: https://www.mfractor.com/blogs/news/localising-your-xamarin-forms-apps.

  1. if you opt to set language automatically when it's changed externally i would just check the system language (CultureInfo.CurrentUICulture) inside the App OnResume then set the app language if it changed from the saved one:

public string SelectedLang { get => Preferences.Get("SelectedLang", "en"); set => Preferences.Set("SelectedLang", value); }

Solution 2:[2]

Using resource files to localize Xamarin.Forms applications requires you to perform the following steps:

Create Resx files containing translated text.

  • Specify the default culture in the shared project.

  • Localize text in Xamarin.Forms.

  • Localize images based on culture settings for each platform.

  • Localize the application name on each platform.

  • Test localization on each platform.

Here is a microsoft tutorial you can refer to : https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/localization/text?pivots=windows

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nick Kovalsky
Solution 2 Adrain Zhu -MSFT