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");
}
new TestPage()?