Skip to content

Instantly share code, notes, and snippets.

@Harold2017
Created September 2, 2022 09:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Harold2017/bee3b6822d90a695e693ea4d4184162f to your computer and use it in GitHub Desktop.
Save Harold2017/bee3b6822d90a695e693ea4d4184162f to your computer and use it in GitHub Desktop.
Avalonia I18N Setting

Add language resource to App.axml:

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceInclude Source="avares://MyAssembly/Assets/Languages/Lang.ENG.xaml"></ResourceInclude>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
</Application.Resources>

Add language selection command to view model:

public ReactiveCommand<string, Unit> LangSelection { get; }

LangSelection = ReactiveCommand.Create((string lang) =>
{
  Application.Current?.Resources.MergedDictionaries!.Replace(Application.Current.Resources.MergedDictionaries[0], new ResourceInclude
  {
      Source = lang switch
      {
          "TC" => new Uri("avares://MyAssembly/Assets/Languages/Lang.TC.xaml"),
          "SC" => new Uri("avares://MyAssembly/Assets/Languages/Lang.SC.xaml"),
          "ENG" => new Uri("avares://MyAssembly/Assets/Languages/Lang.ENG.xaml"),
          _ => new Uri("avares://MyAssembly/Assets/Languages/Lang.ENG.xaml"),
      }
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment