move InitializeCulture

This commit is contained in:
Lightczx
2023-05-16 16:14:49 +08:00
parent 168bed4b2c
commit 73f0e356c4
5 changed files with 24 additions and 25 deletions

View File

@@ -2,6 +2,9 @@
// Licensed under the MIT license.
using CommunityToolkit.Mvvm.Messaging;
using Snap.Hutao.Service;
using System.Globalization;
using Windows.Globalization;
namespace Snap.Hutao.Core.DependencyInjection;
@@ -36,6 +39,23 @@ internal static class DependencyInjection
.BuildServiceProvider(true);
Ioc.Default.ConfigureServices(serviceProvider);
serviceProvider.InitializeCulture();
return serviceProvider;
}
private static IServiceProvider InitializeCulture(this IServiceProvider serviceProvider)
{
AppOptions appOptions = serviceProvider.GetRequiredService<AppOptions>();
appOptions.PreviousCulture = CultureInfo.CurrentCulture;
CultureInfo cultureInfo = appOptions.CurrentCulture;
CultureInfo.CurrentCulture = cultureInfo;
CultureInfo.CurrentUICulture = cultureInfo;
ApplicationLanguages.PrimaryLanguageOverride = cultureInfo.Name;
return serviceProvider;
}
}

View File

@@ -4,10 +4,7 @@
using Microsoft.EntityFrameworkCore;
using Snap.Hutao.Core.Json;
using Snap.Hutao.Model.Entity.Database;
using Snap.Hutao.Service;
using System.Diagnostics;
using System.Globalization;
using Windows.Globalization;
namespace Snap.Hutao.Core.DependencyInjection;
@@ -39,26 +36,6 @@ internal static class IocConfiguration
.AddDbContext<AppDbContext>(AddDbContextCore);
}
/// <summary>
/// 初始化语言
/// </summary>
/// <param name="serviceProvider">服务提供器</param>
/// <returns>服务提供器,用于链式调用</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static IServiceProvider InitializeCulture(this IServiceProvider serviceProvider)
{
AppOptions appOptions = serviceProvider.GetRequiredService<AppOptions>();
appOptions.PreviousCulture = CultureInfo.CurrentCulture;
CultureInfo cultureInfo = appOptions.CurrentCulture;
CultureInfo.CurrentCulture = cultureInfo;
CultureInfo.CurrentUICulture = cultureInfo;
ApplicationLanguages.PrimaryLanguageOverride = cultureInfo.Name;
return serviceProvider;
}
private static void AddDbContextCore(IServiceProvider provider, DbContextOptionsBuilder builder)
{
HutaoOptions hutaoOptions = provider.GetRequiredService<HutaoOptions>();

View File

@@ -20,6 +20,8 @@ internal sealed class ExceptionFormat
/// <returns>格式化后的异常</returns>
public static string Format(Exception exception)
{
// TODO: handle WinRT.ExceptionHelpers+__RestrictedErrorObject
StringBuilder builder = new();
builder.AppendLine("Exception Data:");

View File

@@ -29,8 +29,10 @@ internal sealed class ScheduleTaskInterop : IScheduleTaskInterop
TaskDefinition task = TaskService.Instance.NewTask();
task.RegistrationInfo.Description = SH.CoreScheduleTaskHelperDailyNoteRefreshTaskDescription;
task.Triggers.Add(new TimeTrigger() { Repetition = new(TimeSpan.FromSeconds(interval), TimeSpan.Zero), });
string scriptPath = EnsureWScriptCreated("DailyNoteRefresh", "hutao://DailyNote/Refresh");
task.Actions.Add("wscript", $@"/b ""{scriptPath}""");
TaskService.Instance.RootFolder.RegisterTaskDefinition(DailyNoteRefreshTaskName, task);
return true;
}

View File

@@ -33,8 +33,6 @@ public static partial class Program
// By adding the using statement, we can dispose the injected services when we closing
using (ServiceProvider serviceProvider = DependencyInjection.Initialize())
{
serviceProvider.InitializeCulture();
// In a Desktop app this runs a message pump internally,
// and does not return until the application shuts down.
Application.Start(AppInitializationCallback);