Revisit getting template content

pull/3792/head
Halil İbrahim Kalkan 5 years ago
parent 720b5d7e2f
commit 12bde17d66

@ -11,17 +11,17 @@ namespace Volo.Abp.TextTemplating
[NotNull]
public IServiceProvider ServiceProvider { get; }
[NotNull]
[CanBeNull]
public string Culture { get; }
public TemplateContentContributorContext(
[NotNull] TemplateDefinition templateDefinition,
[NotNull] IServiceProvider serviceProvider,
[NotNull] string culture)
[CanBeNull] string culture)
{
TemplateDefinition = Check.NotNull(templateDefinition, nameof(templateDefinition));
ServiceProvider = Check.NotNull(serviceProvider, nameof(serviceProvider));
Culture = Check.NotNull(culture, nameof(culture));
Culture = culture;
}
}
}

@ -47,14 +47,12 @@ namespace Volo.Abp.TextTemplating
$"No template content contributor was registered. Use {nameof(AbpTextTemplatingOptions)} to register contributors!"
);
}
if (cultureName == null)
{
cultureName = CultureInfo.CurrentUICulture.Name;
}
using (var scope = ServiceScopeFactory.CreateScope())
{
var searchCultureName = cultureName ??
CultureInfo.CurrentUICulture.Name;
var contributors = Options.ContentContributors
.Select(type => (ITemplateContentContributor) scope.ServiceProvider.GetRequiredService(type))
.Reverse()
@ -66,7 +64,7 @@ namespace Volo.Abp.TextTemplating
new TemplateContentContributorContext(
templateDefinition,
scope.ServiceProvider,
cultureName
searchCultureName
)
);
@ -77,18 +75,36 @@ namespace Volo.Abp.TextTemplating
if (!tryDefaults)
{
if (templateDefinition.IsInlineLocalized && cultureName == null)
{
//Try to get culture independent content
templateString = await GetContentOrNullAsync(
contributors,
new TemplateContentContributorContext(
templateDefinition,
scope.ServiceProvider,
null
)
);
if (templateString != null)
{
return templateString;
}
}
return null;
}
//Try to get from same culture without country code
if (cultureName.Contains("-")) //Example: "tr-TR"
if (searchCultureName.Contains("-")) //Example: "tr-TR"
{
templateString = await GetContentOrNullAsync(
contributors,
new TemplateContentContributorContext(
templateDefinition,
scope.ServiceProvider,
CultureHelper.GetBaseCultureName(cultureName)
CultureHelper.GetBaseCultureName(searchCultureName)
)
);
@ -97,16 +113,16 @@ namespace Volo.Abp.TextTemplating
return templateString;
}
}
//Try to get from default culture
if (templateDefinition.DefaultCultureName != null)
if (templateDefinition.IsInlineLocalized)
{
//Try to get culture independent content
templateString = await GetContentOrNullAsync(
contributors,
new TemplateContentContributorContext(
templateDefinition,
scope.ServiceProvider,
templateDefinition.DefaultCultureName
null
)
);
@ -115,6 +131,26 @@ namespace Volo.Abp.TextTemplating
return templateString;
}
}
else
{
//Try to get from default culture
if (templateDefinition.DefaultCultureName != null)
{
templateString = await GetContentOrNullAsync(
contributors,
new TemplateContentContributorContext(
templateDefinition,
scope.ServiceProvider,
templateDefinition.DefaultCultureName
)
);
if (templateString != null)
{
return templateString;
}
}
}
}
//Not found

@ -17,7 +17,7 @@ namespace Volo.Abp.TextTemplating
[CanBeNull]
public Type LocalizationResource { get; set; }
public bool IsInlineLocalized { get; internal set; }
public bool IsInlineLocalized { get; set; }
[CanBeNull]
public string DefaultCultureName { get; }

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.Localization;
@ -35,16 +34,30 @@ namespace Volo.Abp.TextTemplating
{
Check.NotNullOrWhiteSpace(templateName, nameof(templateName));
cultureName ??= CultureInfo.CurrentUICulture.Name;
if (globalContext == null)
{
globalContext = new Dictionary<string, object>();
}
using (CultureHelper.Use(cultureName))
if (cultureName == null)
{
return await RenderInternalAsync(
templateName,
globalContext ?? new Dictionary<string, object>(),
globalContext,
model
);
}
else
{
using (CultureHelper.Use(cultureName))
{
return await RenderInternalAsync(
templateName,
globalContext,
model
);
}
}
}
protected virtual async Task<string> RenderInternalAsync(

@ -14,7 +14,12 @@ namespace Volo.Abp.TextTemplating.VirtualFiles
public string GetContentOrNull(string culture)
{
return _content;
if (culture == null)
{
return _content;
}
return null;
}
}
}

@ -1,7 +1,9 @@
namespace Volo.Abp.TextTemplating.VirtualFiles
using JetBrains.Annotations;
namespace Volo.Abp.TextTemplating.VirtualFiles
{
public interface ILocalizedTemplateContentReader
{
public string GetContentOrNull(string culture);
public string GetContentOrNull([CanBeNull] string culture);
}
}

@ -1,5 +1,4 @@
using System.Globalization;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.TextTemplating.VirtualFiles

@ -35,6 +35,11 @@ namespace Volo.Abp.TextTemplating.VirtualFiles
public string GetContentOrNull(string cultureName)
{
if (cultureName == null)
{
return null;
}
return _dictionary.GetOrDefault(cultureName);
}
}

Loading…
Cancel
Save