Skip to content

Small plugin to allow Xamarin Forms apply different language on the fly, supporting iOS and Android

License

Notifications You must be signed in to change notification settings

JimmyPun610/Plugin.XF.MultiLanguage

Repository files navigation

This is a small plugin to allow Xamarin Forms apply different language on the fly

Support

  • Android
  • iOS

How to use

  1. Install the plugin to your Xamarin Form project (.net standard project only)
Install-Package Plugin.XF.MultiLanguage
  1. Create your string resources file and define key and value
StringRes.resx         //Default
StringRes.es.resx      //Spanish
StringRes.zh.resx      //Chinese
  1. Init the library in App.cs with your ResourceManager
    public App()
        {
            InitializeComponent();
            Plugin.XF.MultiLanguage.StringLoader.Instance.Init(StringRes.ResourceManager);
            MainPage = new MainPage();
        }
  1. 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>
  1. Use in C#
//Replace ResourceKey to to key, for example, Hello
StringLoader.Instance.GetString("ResourceKey").Value;

Change App language

  1. Call this below to change the app language in runtime
  Plugin.XF.MultiLanguage.StringLoader.Instance.SetCultureInfo(new CultureInfo(languageCode));

Important to know

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.

Reference

About

Small plugin to allow Xamarin Forms apply different language on the fly, supporting iOS and Android

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages