Skip to content

Commit

Permalink
Fix language changed display bug
Browse files Browse the repository at this point in the history
Fix language changed display bug
  • Loading branch information
Gaoyifei1011 committed Jul 31, 2024
1 parent 9394f9d commit 21b0472
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 81 deletions.
5 changes: 3 additions & 2 deletions GetStoreApp/Extensions/Console/CharExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GetStoreApp.Services.Root;
using System.Threading.Tasks;

namespace GetStoreApp.Extensions.Console
{
Expand All @@ -9,9 +10,9 @@ public static class CharExtension
{
private static byte[] lengths;

public static void Initialize()
public static async Task InitializeAsync()
{
lengths = ResourceService.GetEmbeddedData("Files/EmbedAssets/Lengths.bin");
lengths = await ResourceService.GetEmbeddedDataAsync("Files/EmbedAssets/Lengths.bin");
}

/// <summary>
Expand Down
Binary file modified GetStoreApp/GetStoreApp.res
Binary file not shown.
6 changes: 3 additions & 3 deletions GetStoreApp/Helpers/Controls/Store/QueryLinksHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static async Task<string> GetCookieAsync()

try
{
byte[] contentBytes = ResourceService.GetEmbeddedData("Files/EmbedAssets/cookie.xml");
byte[] contentBytes = await ResourceService.GetEmbeddedDataAsync("Files/EmbedAssets/cookie.xml");

HttpStringContent httpStringContent = new(Encoding.UTF8.GetString(contentBytes));
httpStringContent.Headers.Expires = DateTime.Now;
Expand Down Expand Up @@ -234,7 +234,7 @@ public static async Task<string> GetFileListXmlAsync(string cookie, string categ

try
{
byte[] wubytesArray = ResourceService.GetEmbeddedData("Files/EmbedAssets/wu.xml");
byte[] wubytesArray = await ResourceService.GetEmbeddedDataAsync("Files/EmbedAssets/wu.xml");
string fileListXml = Encoding.UTF8.GetString(wubytesArray).Replace("{1}", cookie).Replace("{2}", categoryId).Replace("{3}", ring);
byte[] contentBytes = Encoding.UTF8.GetBytes(fileListXml);

Expand Down Expand Up @@ -406,7 +406,7 @@ private static async Task<string> GetAppxUrlAsync(string updateID, string revisi

try
{
byte[] urlbytesArray = ResourceService.GetEmbeddedData("Files/EmbedAssets/url.xml");
byte[] urlbytesArray = await ResourceService.GetEmbeddedDataAsync("Files/EmbedAssets/url.xml");
string url = Encoding.UTF8.GetString(urlbytesArray).Replace("{1}", updateID).Replace("{2}", revisionNumber).Replace("{3}", ring);
byte[] contentBytes = Encoding.UTF8.GetBytes(url);

Expand Down
6 changes: 3 additions & 3 deletions GetStoreApp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
[assembly: AssemblyCompany("高怡飞")]
[assembly: AssemblyCopyright("Copyright ©2022-2024 高怡飞, All Rights Reserved.")]
[assembly: AssemblyDescription("获取商店应用")]
[assembly: AssemblyFileVersion("4.2.724.0")]
[assembly: AssemblyInformationalVersion("4.2.724.0")]
[assembly: AssemblyFileVersion("4.2.730.0")]
[assembly: AssemblyInformationalVersion("4.2.730.0")]
[assembly: AssemblyProduct("获取商店应用")]
[assembly: AssemblyTitle("获取商店应用")]
[assembly: AssemblyVersion("4.2.724.0")]
[assembly: AssemblyVersion("4.2.730.0")]

// 应用程序默认区域性的资源控制器设置
[assembly: NeutralResourcesLanguage("en-us")]
Expand Down
2 changes: 1 addition & 1 deletion GetStoreApp/Services/Controls/Settings/LanguageService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GetStoreApp.Extensions.DataType.Constant;
using GetStoreApp.Services.Root;
using Microsoft.Windows.Globalization;
using Windows.Globalization;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down
2 changes: 1 addition & 1 deletion GetStoreApp/Services/Root/ConsoleLaunchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static async Task InitializeLaunchAsync(string[] args)

InitializeIntroduction();
InitializeRequestContent();
CharExtension.Initialize();
await CharExtension.InitializeAsync();
await RequestService.GetLinksAsync();

DownloadSchedulerService.CloseDownloadScheduler(false);
Expand Down
86 changes: 24 additions & 62 deletions GetStoreApp/Services/Root/ResourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
using Microsoft.UI.Composition.SystemBackdrops;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.ApplicationModel.Resources;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.ApplicationModel.Resources.Core;
using Windows.Foundation.Diagnostics;
using Windows.Storage.Streams;

namespace GetStoreApp.Services.Root
{
Expand All @@ -23,10 +25,9 @@ public static class ResourceService
private static DictionaryEntry _defaultAppLanguage;
private static DictionaryEntry _currentAppLanguage;

private static readonly ResourceManager resourceManager = new();
private static readonly ResourceContext defaultResourceContext;
private static readonly ResourceContext currentResourceContext;
private static readonly ResourceMap resourceMap = new ResourceManager().MainResourceMap;
private static readonly ResourceContext defaultResourceContext = new();
private static readonly ResourceContext currentResourceContext = new();
private static readonly ResourceMap resourceMap = ResourceManager.Current.MainResourceMap;

public static List<TypeModel> TypeList { get; } = [];

Expand All @@ -52,45 +53,6 @@ public static class ResourceService

public static List<DictionaryEntry> DoEngineModeList { get; } = [];

static ResourceService()
{
defaultResourceContext = resourceManager.CreateResourceContext();
currentResourceContext = resourceManager.CreateResourceContext();
resourceMap = resourceManager.MainResourceMap;
GlobalNotificationService.ApplicationExit += OnApplicationExit;
resourceManager.ResourceNotFound += OnResourceNotFound;
}

/// <summary>
/// 应用程序退出时触发的事件
/// </summary>
private static void OnApplicationExit(object sender, EventArgs args)
{
try
{
GlobalNotificationService.ApplicationExit -= OnApplicationExit;
resourceManager.ResourceNotFound -= OnResourceNotFound;
}
catch (Exception e)
{
LogService.WriteLog(LoggingLevel.Error, "Unregister resource service event failed", e);
}
}

/// <summary>
/// 找不到指定的资源而尝试获取资源失败时发生的事件
/// </summary>
private static void OnResourceNotFound(ResourceManager sender, ResourceNotFoundEventArgs args)
{
Dictionary<string, string> loggingInformationDict = new()
{
{ "ResourceName", args.Name },
{ "ResourceContext", args.Context.QualifierValues["language"] }
};

LogService.WriteLog(LoggingLevel.Warning, string.Format("No value for resource {0} could be found.", args.Name), loggingInformationDict);
}

/// <summary>
/// 初始化应用本地化资源
/// </summary>
Expand Down Expand Up @@ -358,26 +320,20 @@ public static string GetLocalized(string resource)
{
try
{
string currentResourceString = resourceMap.GetValue(resource, currentResourceContext).ValueAsString;

if (!string.IsNullOrEmpty(currentResourceString))
return resourceMap.GetValue(resource, currentResourceContext).ValueAsString;
}
catch (Exception currentResourceException)
{
LogService.WriteLog(LoggingLevel.Warning, string.Format("Get resource context with langauge {0} failed.", _currentAppLanguage.Value), currentResourceException);
try
{
return currentResourceString;
return resourceMap.GetValue(resource, defaultResourceContext).ValueAsString;
}

string defaultResourceString = resourceMap.GetValue(resource, defaultResourceContext).ValueAsString;

if (!string.IsNullOrEmpty(defaultResourceString))
catch (Exception defaultResourceException)
{
return defaultResourceString;
LogService.WriteLog(LoggingLevel.Warning, string.Format("Get resource context string with langauge {0} failed.", _defaultAppLanguage.Value), defaultResourceException);
return resource;
}

return resource;
}
catch (Exception e)
{
LogService.WriteLog(LoggingLevel.Warning, string.Format("Get resource context with resource {0} failed.", resource), e);
return resource;
}
}
else
Expand All @@ -390,11 +346,17 @@ public static string GetLocalized(string resource)
/// <summary>
/// 获取嵌入的数据
/// </summary>
public static byte[] GetEmbeddedData(string resource)
public static async Task<byte[]> GetEmbeddedDataAsync(string resource)
{
try
{
return resourceMap.GetValue(resource).ValueAsBytes;
IRandomAccessStream randomAccessStream = await resourceMap.GetValue(resource).GetValueAsStreamAsync();
DataReader dataReader = new(randomAccessStream);
await dataReader.LoadAsync((uint)randomAccessStream.Size);
byte[] bytesArray = new byte[randomAccessStream.Size];
dataReader.ReadBytes(bytesArray);
dataReader.Dispose();
return bytesArray;
}
catch (Exception e)
{
Expand Down
4 changes: 2 additions & 2 deletions GetStoreApp/UI/Dialogs/About/LicenseDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public LicenseDialog()
/// <summary>
/// 初始化许可证对话框时加载的内容
/// </summary>
private void OnLoaded(object sender, RoutedEventArgs args)
private async void OnLoaded(object sender, RoutedEventArgs args)
{
LicenseText = Encoding.UTF8.GetString(ResourceService.GetEmbeddedData("Files/EmbedAssets/LICENSE"));
LicenseText = Encoding.UTF8.GetString(await ResourceService.GetEmbeddedDataAsync("Files/EmbedAssets/LICENSE"));
}
}
}
2 changes: 1 addition & 1 deletion GetStoreAppPackage/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Identity
Name="Gaoyifei1011.GetStoreApp"
Publisher="CN=高怡飞"
Version="4.2.724.0" />
Version="4.2.730.0" />

<Properties>
<DisplayName>ms-resource:PackageDisplayName</DisplayName>
Expand Down
6 changes: 3 additions & 3 deletions GetStoreAppWebView/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
[assembly: AssemblyCompany("高怡飞")]
[assembly: AssemblyCopyright("Copyright ©2022-2024 高怡飞, All Rights Reserved.")]
[assembly: AssemblyDescription("网页浏览器")]
[assembly: AssemblyFileVersion("4.2.724.0")]
[assembly: AssemblyInformationalVersion("4.2.724.0")]
[assembly: AssemblyFileVersion("4.2.730.0")]
[assembly: AssemblyInformationalVersion("4.2.730.0")]
[assembly: AssemblyProduct("网页浏览器")]
[assembly: AssemblyTitle("网页浏览器")]
[assembly: AssemblyVersion("4.2.724.0")]
[assembly: AssemblyVersion("4.2.730.0")]

// 应用程序默认区域性的资源控制器设置
[assembly: NeutralResourcesLanguage("en-us")]
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
// 程序集信息设置
[assembly: AssemblyCompany("高怡飞")]
[assembly: AssemblyCopyright("©Copyright ©2022-2024 高怡飞, All Rights Reserved.")]
[assembly: AssemblyFileVersion("4.2.724.0")]
[assembly: AssemblyInformationalVersion("4.2.724.0")]
[assembly: AssemblyFileVersion("4.2.730.0")]
[assembly: AssemblyInformationalVersion("4.2.730.0")]
[assembly: AssemblyProduct("获取商店应用 WinGet WinRT 扩展")]
[assembly: AssemblyVersion("4.2.724.0")]
[assembly: AssemblyVersion("4.2.730.0")]

// 设置程序集仅允许在Windows平台上可用
[assembly: SupportedOSPlatform("windows10.0.26100")]

0 comments on commit 21b0472

Please sign in to comment.