Reputation: 49
I am trying to localize text in my Xamarin Forms project for Android.
I set up a new project as a testing environment, following this tutorial, however the language of the text doesn´t change when I press the button. The Lang.resx (standard is german) file is set public, the Lang.en.resx file is set to no code generated. Both files have the same keys (switch-de, switch-en, welcome).
I tried to close and reopen the App after changing the language and I disabled fast deployment, neither fixed the problem. The Text stays german.
This is the SwitchPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace I18nTest
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SwitchPage : ContentPage
{
public SwitchPage()
{
InitializeComponent();
Content = new StackLayout
{
Margin = new Thickness(100),
Children =
{
welcomeLabel,
switchENButton,
switchDEButton
}
};
}
Label welcomeLabel = new Label
{
Text = Language.Lang.welcome,
Margin = new Thickness(100, 100, 100, 50),
FontSize = 80,
HorizontalTextAlignment = TextAlignment.Center,
};
Button switchENButton = new Button
{
Text = Language.Lang.switch_en,
};
Button switchDEButton = new Button
{
Text = Language.Lang.switch_de,
};
private void Button_de_Clicked(object sender, EventArgs e)
{
CultureInfo.CurrentUICulture = new CultureInfo("de", false);
}
private async void Button_en_Clicked(object sender, EventArgs e)
{
CultureInfo.CurrentUICulture = new CultureInfo("en", false);
}
}
}
This is the SwitchPage.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="I18nTest.SwitchPage"
xmlns:resources="clr-namespace:i18nMalWieder.Language">
<StackLayout>
<Label Text="{x:Static resources:Lang.welcome}" FontSize="Header" Margin="100,100,100,50" HorizontalTextAlignment="Center"/>
<Button Text="{x:Static resources:Lang.switch_de}" Clicked="Button_de_Clicked" HeightRequest="100"/>
<Button Text="{x:Static resources:Lang.switch_en}" Clicked="Button_en_Clicked" HeightRequest="100"/>
</StackLayout>
</ContentPage>
Upvotes: 0
Views: 387
Reputation: 496
Xamarin's documentation on localization says that using CurrentUICulture might not be ideal for that:
Testing localization is best accomplished by changing your device language. It is possible to set the value of System.Globalization.CultureInfo.CurrentUICulture in code but behavior is inconsistent across platforms so this is not recommended for testing.
Your implementation might be working just fine; however changing the language pragmatically might not behave the same way as changing the device language from the phone's settings.
Upvotes: 1