Skip to content

Commit

Permalink
Try to translate a native dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
ousiax committed Dec 11, 2023
1 parent 4e16694 commit 5c436f9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/Leo.Wpf.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public partial class App : Application
{
private readonly IHost _host;

#if DEBUG
static App()
{
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
}
#endif

public App()
{
_host = Host.CreateDefaultBuilder()
Expand Down Expand Up @@ -76,9 +83,6 @@ protected override async void OnStartup(StartupEventArgs e)
private void LoadApplicationResources()
{
var cultureInfo = CultureInfo.CurrentUICulture;
#if DEBUG
cultureInfo = CultureInfo.GetCultureInfo("en-US");
#endif
while (!string.IsNullOrEmpty(cultureInfo.Name))
{
var uri = $"Resources/{cultureInfo.Name}/Localization.xaml";
Expand Down
34 changes: 29 additions & 5 deletions src/Leo.Wpf.App/Infrastructure/Constants/Constants.Genders.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
namespace Leo.Wpf.App.Infrastructure
using System.Globalization;

namespace Leo.Wpf.App.Infrastructure
{
public partial class Constants
{
public static Dictionary<string, string> Genders = new()
{
// i18n
{"Unkonw", "未知" },
{"Male", "男" },
{"Female", "女" },
{"Unkonw", GetString("Unkonw") },
{"Male", GetString("Male") },
{"Female", GetString("Female") },
};

// TODO i18n
private static string GetString(string key)
{
var enUS = new Dictionary<string, string>()
{
{"Unkonw", "Unkonw" },
{"Male", "Male" },
{"Female", "Female" },
};
var zhCN = new Dictionary<string, string>()
{
{"Unkonw", "未知" },
{"Male", "男" },
{"Female", "女" },
};

if (CultureInfo.CurrentUICulture.Name.Contains("zh"))
{
return zhCN[key];
}
return enUS[key];
}
}
}
19 changes: 19 additions & 0 deletions src/Leo.Wpf.App/ViewModels/FindCustomerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Leo.Wpf.App.Messages;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Data;

namespace Leo.Wpf.App.ViewModels
Expand Down Expand Up @@ -40,6 +41,24 @@ public FindCustomerViewModel(
AllCustomersView = CollectionViewSource.GetDefaultView(AllCustomers);

_ = LoadAllCustomersAsync();

// TODO i18n
if (CultureInfo.CurrentUICulture.Name.Contains("zh"))
{
SearchFields = new() {
{ "name", "姓名" },
{ "phone", "手机" },
{ "card", "卡号" },
};
}
else
{
SearchFields = new() {
{ "name", "Name" },
{ "phone", "Phone" },
{ "card", "CardNo" },
};
}
}

public ObservableCollection<CustomerViewModel> AllCustomers { get; set; } = [];
Expand Down

0 comments on commit 5c436f9

Please sign in to comment.