0

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?

rana hd
  • 355
  • 3
  • 18
  • the app language won't change until the app restarts. You should be able to kill the app on the device and restart it (without using VS) to get the new language – Jason May 06 '22 at 14:03
  • thanks for replying sir. i've been searching for a way to do what you suggested but i couldn't find one. would you please give me a hint – rana hd May 07 '22 at 07:31

2 Answers2

0

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); }

Nick Kovalsky
  • 5,378
  • 2
  • 23
  • 50
0

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://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/localization/text?pivots=windows

Adrain
  • 1,946
  • 1
  • 3
  • 7