Browse Source

Keep the culture context reachable for a case insensitive global context

pull/25974/head
maliming 2 months ago
parent
commit
d6d8e3b043
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 19
      framework/src/Volo.Abp.TextTemplating.Core/Volo/Abp/TextTemplating/TemplateRenderingEngineBase.cs
  2. 7
      framework/test/Volo.Abp.TextTemplating.Scriban.Tests/Volo/Abp/TextTemplating/Scriban/ScribanTemplateRenderingEngine_CultureContext_Tests.cs

19
framework/src/Volo.Abp.TextTemplating.Core/Volo/Abp/TextTemplating/TemplateRenderingEngineBase.cs

@ -41,15 +41,22 @@ public abstract class TemplateRenderingEngineBase : ITemplateRenderingEngine
// The invariant culture has an empty name, which is not a valid value for a lang attribute. // The invariant culture has an empty name, which is not a valid value for a lang attribute.
var cultureName = CultureInfo.CurrentUICulture.Name; var cultureName = CultureInfo.CurrentUICulture.Name;
if (!globalContext.ContainsKey(CultureContextKey)) SetCultureContextValue(globalContext, CultureContextKey, cultureName.IsNullOrWhiteSpace() ? "en" : cultureName);
{ SetCultureContextValue(globalContext, TextDirectionContextKey, CultureHelper.IsRtl ? "rtl" : "ltr");
globalContext[CultureContextKey] = cultureName.IsNullOrWhiteSpace() ? "en" : cultureName; }
}
if (!globalContext.ContainsKey(TextDirectionContextKey)) protected virtual void SetCultureContextValue(Dictionary<string, object> globalContext, string key, string value)
{
if (!globalContext.TryGetValue(key, out var callerValue))
{ {
globalContext[TextDirectionContextKey] = CultureHelper.IsRtl ? "rtl" : "ltr"; globalContext.Add(key, value);
return;
} }
// A case insensitive context can hold the value under another casing, while templates look the key
// up as it is written here, so it is re-inserted with the canonical one.
globalContext.Remove(key);
globalContext.Add(key, callerValue);
} }
protected virtual async Task<string?> GetContentOrNullAsync(TemplateDefinition templateDefinition) protected virtual async Task<string?> GetContentOrNullAsync(TemplateDefinition templateDefinition)

7
framework/test/Volo.Abp.TextTemplating.Scriban.Tests/Volo/Abp/TextTemplating/Scriban/ScribanTemplateRenderingEngine_CultureContext_Tests.cs

@ -80,9 +80,8 @@ public class ScribanTemplateRenderingEngine_CultureContext_Tests : AbpTextTempla
[Fact] [Fact]
public async Task Should_Keep_The_Comparer_Of_The_Context_Of_The_Caller() public async Task Should_Keep_The_Comparer_Of_The_Context_Of_The_Caller()
{ {
// ABP_CULTURE already covers the key here, so nothing is added; losing the comparer on the copy // Scriban looks the key up as the template writes it, so the value of a case insensitive context
// would add a second, lowercase entry and render "ar". Scriban itself looks names up case // has to be re-inserted under the canonical casing to stay reachable.
// sensitively, hence the empty culture in the output.
var globalContext = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase) var globalContext = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
{ {
["ABP_CULTURE"] = "custom" ["ABP_CULTURE"] = "custom"
@ -92,7 +91,7 @@ public class ScribanTemplateRenderingEngine_CultureContext_Tests : AbpTextTempla
ScribanTestTemplateDefinitionProvider.CultureContext, ScribanTestTemplateDefinitionProvider.CultureContext,
cultureName: "ar", cultureName: "ar",
globalContext: globalContext globalContext: globalContext
)).ShouldBe("<html lang=\"\" dir=\"rtl\">"); )).ShouldBe("<html lang=\"custom\" dir=\"rtl\">");
} }
[Fact] [Fact]

Loading…
Cancel
Save