This is a small plugin to allow Xamarin Forms apply different language on the fly
- Android
- iOS
- Install the plugin to your Xamarin Form project (.net standard project only)
Install-Package Plugin.XF.MultiLanguage
- Create your string resources file and define key and value
StringRes.resx //Default
StringRes.es.resx //Spanish
StringRes.zh.resx //Chinese
- Init the library in App.cs with your ResourceManager
public App()
{
InitializeComponent();
Plugin.XF.MultiLanguage.StringLoader.Instance.Init(StringRes.ResourceManager);
MainPage = new MainPage();
}
- Use in XAML
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:multiLang="clr-namespace:Plugin.XF.MultiLanguage;assembly=Plugin.XF.MultiLanguage"
mc:Ignorable="d"
x:Class="SampleApp.MultiLangage.MainPage">
<!-- Replace {ResourceKey} to the key, for example, [Hello].Value-->
<Label Text="{Binding [{ResourceKey}].Value, Source={x:Static multiLang:StringLoader.Instance}}"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"/>
</ContentPage>
- Use in C#
//Replace ResourceKey to to key, for example, Hello
StringLoader.Instance.GetString("ResourceKey").Value;
- Call this below to change the app language in runtime
Plugin.XF.MultiLanguage.StringLoader.Instance.SetCultureInfo(new CultureInfo(languageCode));
As the plugin does not save the language preference, the app should set the language again while launching. Otherwise, it will take DeviceUICultureInfo as the culture info.