localenames

This commit is contained in:
Lightczx
2023-11-13 15:37:06 +08:00
parent 800c948c38
commit d4e9c2aa9c
6 changed files with 66 additions and 51 deletions

View File

@@ -7,17 +7,9 @@
<Nullable>enable</Nullable>
<Platforms>x64</Platforms>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<Configurations>Debug;Release;Debug As Fake Elevated</Configurations>
<Configurations>Debug;Release</Configurations>
</PropertyGroup>
<ItemGroup>
<None Remove="stylecop.json" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all" Version="3.3.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />

View File

@@ -7,7 +7,7 @@
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Configurations>Debug;Release;Debug As Fake Elevated</Configurations>
<Configurations>Debug;Release</Configurations>
</PropertyGroup>
<ItemGroup>

View File

@@ -932,6 +932,9 @@
<data name="ServiceMetadataFileNotFound" xml:space="preserve">
<value>无法找到缓存的元数据文件</value>
</data>
<data name="ServiceMetadataHttpRequestFailed" xml:space="preserve">
<value>HTTP {0}:元数据校验文件下载失败</value>
</data>
<data name="ServiceMetadataNotInitialized" xml:space="preserve">
<value>元数据服务尚未初始化,或初始化失败</value>
</data>

View File

@@ -1,8 +1,6 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using System.Collections.Immutable;
namespace Snap.Hutao.Service.Metadata;
/// <summary>
@@ -26,42 +24,54 @@ internal static class LocaleNames
public const string CHS = "CHS"; // Chinese (Simplified)
public const string CHT = "CHT"; // Chinese (Traditional)
public static readonly ImmutableDictionary<string, string> LanguageNameLocaleNameMap = new Dictionary<string, string>()
public static bool TryGetLocaleNameFromLanguageName(string languageName, [NotNullWhen(true)] out string? localeName)
{
["de"] = DE,
["en"] = EN,
["es"] = ES,
["fr"] = FR,
["id"] = ID,
["it"] = IT,
["ja"] = JP,
["ko"] = KR,
["pt"] = PT,
["ru"] = RU,
["th"] = TH,
["tr"] = TR,
["vi"] = VI,
["zh-Hans"] = CHS,
["zh-Hant"] = CHT,
[string.Empty] = CHS, // Fallback to Chinese.
}.ToImmutableDictionary();
localeName = languageName switch
{
"de" => DE,
"en" => EN,
"es" => ES,
"fr" => FR,
"id" => ID,
"it" => IT,
"ja" => JP,
"ko" => KR,
"pt" => PT,
"ru" => RU,
"th" => TH,
"tr" => TR,
"vi" => VI,
"zh-Hans" => CHS,
"zh-Hant" => CHT,
"" => CHS, // Fallback to Chinese.
_ => string.Empty,
};
public static readonly ImmutableDictionary<string, string> LocaleNameLanguageCodeMap = new Dictionary<string, string>()
return !string.IsNullOrEmpty(localeName);
}
public static bool TryGetLanguageCodeFromLocaleName(string localeName, [NotNullWhen(true)] out string? languageCode)
{
[DE] = "de-de",
[EN] = "en-us",
[ES] = "es-es",
[FR] = "fr-fr",
[ID] = "id-id",
[IT] = "it-it",
[JP] = "ja-jp",
[KR] = "ko-kr",
[PT] = "pt-pt",
[RU] = "ru-ru",
[TH] = "th-th",
[TR] = "tr-tr",
[VI] = "vi-vn",
[CHS] = "zh-cn",
[CHT] = "zh-tw",
}.ToImmutableDictionary();
languageCode = localeName switch
{
DE => "de-de",
EN => "en-us",
ES => "es-es",
FR => "fr-fr",
ID => "id-id",
IT => "it-it",
JP => "ja-jp",
KR => "ko-kr",
PT => "pt-pt",
RU => "ru-ru",
TH => "th-th",
TR => "tr-tr",
VI => "vi-vn",
CHS => "zh-cn",
CHT => "zh-tw",
_ => string.Empty,
};
return !string.IsNullOrEmpty(languageCode);
}
}

View File

@@ -69,7 +69,15 @@ internal sealed partial class MetadataOptions : IOptions<MetadataOptions>
/// </summary>
public string LanguageCode
{
get => LocaleNames.LocaleNameLanguageCodeMap[LocaleName];
get
{
if (LocaleNames.TryGetLanguageCodeFromLocaleName(LocaleName, out string? languageCode))
{
return languageCode;
}
throw new KeyNotFoundException($"Invalid localeName: '{LocaleName}'");
}
}
/// <inheritdoc/>
@@ -84,7 +92,7 @@ internal sealed partial class MetadataOptions : IOptions<MetadataOptions>
{
while (true)
{
if (LocaleNames.LanguageNameLocaleNameMap.TryGetValue(cultureInfo.Name, out string? localeName))
if (LocaleNames.TryGetLocaleNameFromLanguageName(cultureInfo.Name, out string? localeName))
{
return localeName;
}

View File

@@ -9,6 +9,7 @@ using Snap.Hutao.Core.ExceptionService;
using Snap.Hutao.Core.IO.Hashing;
using Snap.Hutao.Core.Setting;
using Snap.Hutao.Service.Notification;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
@@ -90,13 +91,14 @@ internal sealed partial class MetadataService : IMetadataService, IMetadataServi
}
catch (HttpRequestException ex)
{
if (ex.StatusCode is HttpStatusCode.Forbidden or HttpStatusCode.NotFound)
if (ex.StatusCode is (HttpStatusCode)418)
{
infoBarService.Error(SH.ServiceMetadataVersionNotSupported);
}
else
{
infoBarService.Error(ex, SH.ServiceMetadataRequestFailed);
int code = (int)(ex.StatusCode ?? 0);
infoBarService.Error(SH.FormatServiceMetadataHttpRequestFailed(CultureInfo.CurrentCulture, code));
}
return false;