From 74011e50e3f334b62088669f62b6607e99eaceb4 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Sun, 20 Oct 2024 11:49:36 +0900 Subject: [PATCH] Do not translate checkout with the backend language --- .../Controllers/UIInvoiceController.UI.cs | 4 +-- BTCPayServer/Extensions.cs | 3 ++- BTCPayServer/Services/PrettyNameProvider.cs | 27 ++++++++++++++----- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/BTCPayServer/Controllers/UIInvoiceController.UI.cs b/BTCPayServer/Controllers/UIInvoiceController.UI.cs index d40475cb9..d1a369ffc 100644 --- a/BTCPayServer/Controllers/UIInvoiceController.UI.cs +++ b/BTCPayServer/Controllers/UIInvoiceController.UI.cs @@ -856,7 +856,7 @@ namespace BTCPayServer.Controllers var model = new CheckoutModel { Activated = prompt.Activated, - PaymentMethodName = _prettyName.PrettyName(paymentMethodId), + PaymentMethodName = _prettyName.PrettyName(paymentMethodId, true), PaymentMethodCurrency = prompt.Currency, RootPath = Request.PathBase.Value.WithTrailingSlash(), OrderId = orderId, @@ -908,7 +908,7 @@ namespace BTCPayServer.Controllers { Displayed = displayedPaymentMethods.Contains(kv.PaymentMethodId), PaymentMethodId = kv.PaymentMethodId, - PaymentMethodName = _prettyName.PrettyName(kv.PaymentMethodId), + PaymentMethodName = _prettyName.PrettyName(kv.PaymentMethodId, true), Order = kv.PaymentMethodId switch { _ when PaymentTypes.CHAIN.GetPaymentMethodId(_NetworkProvider.DefaultNetwork.CryptoCode) == kv.PaymentMethodId => 0, diff --git a/BTCPayServer/Extensions.cs b/BTCPayServer/Extensions.cs index 8d62c33dd..be72e42bb 100644 --- a/BTCPayServer/Extensions.cs +++ b/BTCPayServer/Extensions.cs @@ -299,7 +299,8 @@ namespace BTCPayServer } public static IServiceCollection AddDefaultPrettyName(this IServiceCollection services, PaymentMethodId paymentMethodId, string defaultPrettyName) { - return services.AddDefaultTranslations(KeyValuePair.Create(PrettyNameProvider.GetTranslationKey(paymentMethodId), defaultPrettyName)); + services.AddSingleton(new PrettyNameProvider.UntranslatedPrettyName(paymentMethodId, defaultPrettyName)); + return services.AddDefaultTranslations(KeyValuePair.Create(PrettyNameProvider.GetTranslationKey(paymentMethodId), defaultPrettyName)); } public static IServiceCollection AddDefaultTranslations(this IServiceCollection services, params KeyValuePair[] keyValues) { diff --git a/BTCPayServer/Services/PrettyNameProvider.cs b/BTCPayServer/Services/PrettyNameProvider.cs index 30c5409f8..450476378 100644 --- a/BTCPayServer/Services/PrettyNameProvider.cs +++ b/BTCPayServer/Services/PrettyNameProvider.cs @@ -7,22 +7,35 @@ namespace BTCPayServer.Services { public class PrettyNameProvider { + public record UntranslatedPrettyName(PaymentMethodId PaymentMethodId, string Text); public static string GetTranslationKey(PaymentMethodId paymentMethodId) => $"PrettyName({paymentMethodId})"; private readonly IStringLocalizer _stringLocalizer; - - public PrettyNameProvider(IStringLocalizer stringLocalizer) + Dictionary _untranslated = new Dictionary(); + public PrettyNameProvider(IEnumerable untranslatedPrettyNames, IStringLocalizer stringLocalizer) { _stringLocalizer = stringLocalizer; + foreach (var e in untranslatedPrettyNames) + _untranslated.TryAdd(e.PaymentMethodId, e.Text); } - public string PrettyName(PaymentMethodId paymentMethodId) + public string PrettyName(PaymentMethodId paymentMethodId, bool untranslated) { if (paymentMethodId is null) return ""; - var key = GetTranslationKey(paymentMethodId); - var result = _stringLocalizer[key]?.Value; - if (string.IsNullOrEmpty(result) || result == key) + if (untranslated) + { + if (_untranslated.TryGetValue(paymentMethodId, out var v)) + return v; return paymentMethodId.ToString(); - return result; + } + else + { + var key = GetTranslationKey(paymentMethodId); + var result = _stringLocalizer[key]?.Value; + if (string.IsNullOrEmpty(result) || result == key) + return paymentMethodId.ToString(); + return result; + } } + public string PrettyName(PaymentMethodId paymentMethodId) => PrettyName(paymentMethodId, false); } }