.NET Core AvaloniaUI multi-language internationalization

AvaloniaUI is a cross-platform desktop application UI framework based on .Net Core, if you use multiple languages ​​AvaloniaUI international friends can refer to my article:

This article can help you:

  1. Change the UI display language according to the language set by the system user
  2. You can preview the effect in the designer
  3. Support the TextBox Watermark Properties
  4. Support Content property of ContentControl
  5. Text property of the TextBlock support

step 1:

Create a new folder (example here called Localizations) for storage in the project directory AppResources.resx file:

This file some names, values, comments, in which the name is to append the following value to control property

Here default AppResources.resx is English, if I want to add simplified Chinese; then create a new AppResources.zh-Hans.resx file, if you want to add more languages, please refer to:

https://docs.microsoft.com/zh-cn/xamarin/xamarin-forms/app-fundamentals/localization/text?tabs=windows

Note: The top of the new resource file access modifier can not select "No Code Generation"

 

Step 2:

Dependent properties of the new control principle is to find Uid when displaying text property of the control change and replace the specified text in multiple languages, Uid is the name defined in the AppResources.resx

New LocalizationAttachedPropertyHolder.cs file

 1     public class LocalizationAttachedPropertyHolder
 2     {
 3         public static AvaloniaProperty<string> UidProperty =
 4             AvaloniaProperty.RegisterAttached<LocalizationAttachedPropertyHolder, AvaloniaObject, string>("Uid");
 5 
 6         static LocalizationAttachedPropertyHolder()
 7         {
 8             TextBlock.TextProperty.Changed.Subscribe(next =>
 9             {
10                 var uid = GetUid(next.Sender);
11                 if (uid != null)
12                 {
13                     next.Sender.SetValue(TextBlock.TextProperty, AppResources.ResourceManager.GetString(uid.ToString()));
14                 }
15             });
16 
17             ContentControl.ContentProperty.Changed.Subscribe(next =>
18             {
19                 var uid = GetUid(next.Sender);
20                 if (uid != null)
21                 {
22                     next.Sender.SetValue(ContentControl.ContentProperty, AppResources.ResourceManager.GetString(uid.ToString()));
23                 }
24             });
25 
26             TextBox.WatermarkProperty.Changed.Subscribe(next =>
27             {
28                 var uid = GetUid(next.Sender);
29                 if (uid != null)
30                 {
31                     next.Sender.SetValue(TextBox.WatermarkProperty, AppResources.ResourceManager.GetString(uid.ToString()));
32                 }
33             });
34         }
35 
36         public static void SetUid(AvaloniaObject target, string value)
37         {
38             target.SetValue(UidProperty, value);
39         }
40 
41         public static string GetUid(AvaloniaObject target)
42         {
43             return (string)target.GetValue(UidProperty);
44         }
45     }

I named Uid , where you can customize your own

 Step 3:

Cited in the xaml:

 

xmlns:localizations="clr-namespace:Demo.Localizations;assembly=SpockWallet"

 

Demo.Localizations is located just created LocalizationAttachedPropertyHolder.cs namespace 

then you can use the controls on:
Uid is the name defined in AppResources.resx
1 <TextBlock localizations:LocalizationAttachedPropertyHolder.Uid="CreateWallet" Text="Create Wallet"/>
2 
3 <Button localizations:LocalizationAttachedPropertyHolder.Uid="CreateYourWallet" Content="Create Your Wallet" />
4 
5 <TextBox localizations:LocalizationAttachedPropertyHolder.Uid="DeleteWallet" Watermark="Delete Wallet" />

Rebuild what AvaloniaUI designer will be able to show you AppResources.resx and xaml text of the definition of control

 

Guess you like

Origin www.cnblogs.com/myhalo/p/11417374.html