Support lol-US locale for IPC requests (#2435)

* Support lol-US locale for IPC requests

* Support sr-CS as well

* Apply feedback

* Apply feedback and Rider cleanup

* Less allocations make everyone happy

* Apply feedback

* Explain why we're doing this stupidity

* Uppercase Windows/Linux compat fix

* Go back to earlier version
This commit is contained in:
Sebastian Göls
2021-10-27 13:15:56 +02:00
committed by GitHub
parent 148472eda4
commit 785b43781a
4 changed files with 91 additions and 2 deletions

View File

@@ -37,7 +37,7 @@ namespace ArchiSteamFarm.Core {
if ((now.Month == 4) && (now.Day == 1)) {
try {
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("qps-Ploc");
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture(SharedInfo.LolcatCultureName);
} catch (Exception e) {
ASF.ArchiLogger.LogGenericDebuggingException(e);

View File

@@ -0,0 +1,84 @@
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// |
// Copyright 2015-2021 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Headers;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
namespace ArchiSteamFarm.IPC.Integration {
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
internal sealed class LocalizationMiddleware {
internal static readonly ImmutableDictionary<string, string> CultureConversions = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) { { "lol-US", SharedInfo.LolcatCultureName }, { "sr-CS", "sr-Latn" } }.ToImmutableDictionary();
private readonly RequestDelegate Next;
public LocalizationMiddleware(RequestDelegate next) => Next = next ?? throw new ArgumentNullException(nameof(next));
[UsedImplicitly]
public async Task InvokeAsync(HttpContext context) {
if (context == null) {
throw new ArgumentNullException(nameof(context));
}
RequestHeaders headers = context.Request.GetTypedHeaders();
IList<StringWithQualityHeaderValue>? acceptLanguageHeader = headers.AcceptLanguage;
if ((acceptLanguageHeader == null) || (acceptLanguageHeader.Count == 0)) {
await Next(context).ConfigureAwait(false);
return;
}
bool valuesChanged = false;
for (int i = 0; i < acceptLanguageHeader.Count; i++) {
StringSegment language = acceptLanguageHeader[i].Value;
if (!language.HasValue || string.IsNullOrEmpty(language.Value)) {
continue;
}
if (!CultureConversions.TryGetValue(language.Value, out string? replacement) || string.IsNullOrEmpty(replacement)) {
continue;
}
acceptLanguageHeader[i] = StringWithQualityHeaderValue.Parse(replacement);
valuesChanged = true;
}
if (valuesChanged) {
// The getter returns a temporary collection; To make sure our changes are persisted, we need to assign it back
headers.AcceptLanguage = acceptLanguageHeader;
}
await Next(context).ConfigureAwait(false);
}
}
}

View File

@@ -27,6 +27,7 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using ArchiSteamFarm.Core;
@@ -140,6 +141,7 @@ namespace ArchiSteamFarm.IPC {
}
);
app.UseMiddleware<LocalizationMiddleware>();
app.UseRequestLocalization();
// Use routing for our API controllers, this should be called once we're done with all the static files mess
@@ -222,7 +224,9 @@ namespace ArchiSteamFarm.IPC {
#endif
static options => {
// We do not set the DefaultRequestCulture here, because it will default to Thread.CurrentThread.CurrentCulture in this case, which is set when loading GlobalConfig
options.SupportedUICultures = options.SupportedCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
List<CultureInfo> supportedCultures = CultureInfo.GetCultures(CultureTypes.AllCultures).Append(CultureInfo.CreateSpecificCulture(SharedInfo.LolcatCultureName)).ToList();
options.SupportedUICultures = options.SupportedCultures = supportedCultures;
// The default checks the URI and cookies and only then for headers; ASFs IPC does not use either of the higher priority mechanisms anywhere else and we don't want to start here.
options.RequestCultureProviders = new List<IRequestCultureProvider> { new AcceptLanguageHeaderRequestCultureProvider() };

View File

@@ -57,6 +57,7 @@ namespace ArchiSteamFarm {
internal const string LicenseName = "Apache 2.0";
internal const string LicenseURL = "https://www.apache.org/licenses/LICENSE-2.0";
internal const string LogFile = "log.txt";
internal const string LolcatCultureName = "qps-Ploc";
internal const string MobileAuthenticatorExtension = ".maFile";
internal const string PluginsDirectory = "plugins";
internal const string ProjectURL = "https://github.com/" + GithubRepo;