Stapyman
Stapyman

Reputation: 73

Xamarin Forms switch language at runtime on each page

I'm developing a cross-platform app (android, IOS) which has to be able to switch languages on each page at the press of a button. I'm using an AppShell for navigation and have the button for the language switch in the toolbar of the AppShell. I made resource files: AppResources.resx and AppResources.fr.resx. I reload both the current page I am on and the AppShell when switching, which seems to have the side effect of going back to the first page I have set on my navigation bar. The reload of the AppShell seems to be necessary as when I don't do it the page seems to go on top and there is no more navigation as well as the color resources I have set in the AppShell get removed. I use the below code to switch the language of my app:

private void Language_switch(object sender, EventArgs e)
        {
            var lang_switch = Lang.Text;
            if (lang_switch == "FR")
            {
                CultureInfo language = new CultureInfo("fr");
                Thread.CurrentThread.CurrentUICulture = language;
                AppResources.Culture = language;
                Application.Current.Properties[key: "LanguageCode"] = "fr_FR";
            }
            else
            {
                CultureInfo language = new CultureInfo("");
                Thread.CurrentThread.CurrentUICulture = language;
                AppResources.Culture = language;
                Application.Current.Properties[key: "LanguageCode"] = "nl_NL";
            }
            Application.Current.MainPage = new Surveys();
            Application.Current.MainPage = new AppShell();
        }

This code used to work but is not working anymore. I do remember updating Xamarin Forms a bit ago so this might have something to do with the code not working anymore. In XAML I read from the resource files as below:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyOpinion.Views.Surveys"
             xmlns:vm="clr-namespace:MyOpinion.ViewModels"
             Title="{Binding Title}"
             xmlns:resource="clr-namespace:MyOpinion.Resx">

<Label Text="{x:Static resource:AppResources.Openstaande}" TextColor="{StaticResource Text}" FontSize="24" Margin="0,0,0,10"/>

</ContentPage>

Upvotes: 1

Views: 846

Answers (2)

Amjad S.
Amjad S.

Reputation: 1241

As @Michael said i dont think either there is a way to change language without poping to Root. Though i dont recommend his idea of copping the navigation stack then pushing the pages. But i will implement the idea for you. Note that navigation stack in readonly property.

 
var navStack = Application.Current.MainPage.Navigation.NavigationStack;
Application.Current.MainPage.Navigation.RemovePage(navStack.First());

Application.Current.MainPage = new AppShell ();
foreach (var item in navStack)
{
   await Application.Current.MainPage.Navigation.PushAsync((Page)Activator.CreateInstance(item.GetType()));
}

Upvotes: 0

benyaala92
benyaala92

Reputation: 78

you can use the TranslateExtension provided from Xamarin Community Toolkit. You don't need to reload your pages.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="MyLittleApp.MainPage">

    <StackLayout>

        <Label Text="{xct:Translate AppResources.ATranslatedMessage}" />

        <Label Text="{xct:Translate AppResources.AnotherTranslatedMessage, StringFormat='#{0}'}" />

    </StackLayout>
</ContentPage>

You should Initiliaze it first:

LocalizationResourceManager.Current.PropertyChanged += (_, _) => AppResources.Culture = LocalizationResourceManager.Current.CurrentCulture;
LocalizationResourceManager.Current.Init(AppResources.ResourceManager);

After Language change you call:

LocalizationResourceManager.Current.CurrentCulture = newCulture;

Here is documentation https://learn.microsoft.com/en-us/xamarin/community-toolkit/extensions/translateextension

https://learn.microsoft.com/en-us/xamarin/community-toolkit/helpers/localizationresourcemanager

Upvotes: 2

Related Questions