Do not translate checkout with the backend language

This commit is contained in:
nicolas.dorier 2024-10-20 11:49:36 +09:00
parent 3dfdbf544a
commit 74011e50e3
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
3 changed files with 24 additions and 10 deletions

View File

@ -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,

View File

@ -299,6 +299,7 @@ namespace BTCPayServer
}
public static IServiceCollection AddDefaultPrettyName(this IServiceCollection services, PaymentMethodId paymentMethodId, string defaultPrettyName)
{
services.AddSingleton<PrettyNameProvider.UntranslatedPrettyName>(new PrettyNameProvider.UntranslatedPrettyName(paymentMethodId, defaultPrettyName));
return services.AddDefaultTranslations(KeyValuePair.Create<string, string?>(PrettyNameProvider.GetTranslationKey(paymentMethodId), defaultPrettyName));
}
public static IServiceCollection AddDefaultTranslations(this IServiceCollection services, params KeyValuePair<string, string?>[] keyValues)

View File

@ -7,17 +7,28 @@ 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<PaymentMethodId, string> _untranslated = new Dictionary<PaymentMethodId, string>();
public PrettyNameProvider(IEnumerable<UntranslatedPrettyName> 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 "<NULL>";
if (untranslated)
{
if (_untranslated.TryGetValue(paymentMethodId, out var v))
return v;
return paymentMethodId.ToString();
}
else
{
var key = GetTranslationKey(paymentMethodId);
var result = _stringLocalizer[key]?.Value;
if (string.IsNullOrEmpty(result) || result == key)
@ -25,4 +36,6 @@ namespace BTCPayServer.Services
return result;
}
}
public string PrettyName(PaymentMethodId paymentMethodId) => PrettyName(paymentMethodId, false);
}
}