Update user interface language on the fly

I follow this guide to localize my application. I don’t want to get the system language, so I don’t use ILocalizethe dependecy interface and services. I have these 3 resx files for English, Spanish and French:

  • AppResources.es.resx
  • AppResources.fr.resx
  • AppResources.resx (in English)

This is my code TestPage.xamlusing the class TranslateExtensionfrom the manual:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:t="clr-namespace:LanguageTest.Resources;assembly=LanguageTest.Resources"
             x:Class="LanguageTest.TestPage">
    <Label Text="{t:Translate TestText}" VerticalOptions="Center" HorizontalOptions="Center" />
</ContentPage>

With this code, I initialize the language settings:

public App ()
{
    // I can change among "en", "es" and "fr"
    AppResources.Culture = new CultureInfo("es"); 
    this.MainPage = new NavigationPage(new TestPage());
}

In this case, the texts are displayed in Spanish. But I want to change the language after the pages are displayed (for example, with the help of the LanguageSettingsPageuser can change the language while the application is running).

public App ()
{
    AppResources.Culture = new CultureInfo("es"); 
    this.MainPage = new NavigationPage(new TestPage());
    AppResources.Culture = new CultureInfo("fr");
    // At this point the texts are diplayed in Spanish, not in French
}

new TestPage()?

+4
3

Resx, . , . I18NPortable . .

:

<Button Text="{Binding Strings[key]}" />

" " ( ):

I18N.Current.Locale = "fr";
+1

Setters OnAppearing(), , , resx. OnAppearing() , . , , Text .

0

If you simply pull out the page, resources will not be evaluated again. You simply show the previous page in your navigation stack (still in memory).

You need to click a new page for new downloadable resource texts.

0
source

Source: https://habr.com/ru/post/1656320/


All Articles