|
|
|
@ -1,4 +1,5 @@ |
|
|
|
using System.Globalization; |
|
|
|
using System; |
|
|
|
using System.Globalization; |
|
|
|
using System.Linq; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using JetBrains.Annotations; |
|
|
|
@ -26,9 +27,10 @@ namespace Volo.Abp.TextTemplating |
|
|
|
} |
|
|
|
|
|
|
|
public virtual Task<string> GetContentOrNullAsync( |
|
|
|
[NotNull] string templateName, |
|
|
|
[NotNull] string templateName, |
|
|
|
[CanBeNull] string cultureName = null, |
|
|
|
bool tryDefaults = true) |
|
|
|
bool tryDefaults = true, |
|
|
|
bool useCurrentCultureIfCultureNameIsNull = true) |
|
|
|
{ |
|
|
|
var template = _templateDefinitionManager.Get(templateName); |
|
|
|
return GetContentOrNullAsync(template, cultureName); |
|
|
|
@ -37,7 +39,8 @@ namespace Volo.Abp.TextTemplating |
|
|
|
public virtual async Task<string> GetContentOrNullAsync( |
|
|
|
[NotNull] TemplateDefinition templateDefinition, |
|
|
|
[CanBeNull] string cultureName = null, |
|
|
|
bool tryDefaults = true) |
|
|
|
bool tryDefaults = true, |
|
|
|
bool useCurrentCultureIfCultureNameIsNull = true) |
|
|
|
{ |
|
|
|
Check.NotNull(templateDefinition, nameof(templateDefinition)); |
|
|
|
|
|
|
|
@ -47,64 +50,50 @@ namespace Volo.Abp.TextTemplating |
|
|
|
$"No template content contributor was registered. Use {nameof(AbpTextTemplatingOptions)} to register contributors!" |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
using (var scope = ServiceScopeFactory.CreateScope()) |
|
|
|
{ |
|
|
|
var searchCultureName = cultureName ?? |
|
|
|
CultureInfo.CurrentUICulture.Name; |
|
|
|
|
|
|
|
var contributors = Options.ContentContributors |
|
|
|
.Select(type => (ITemplateContentContributor) scope.ServiceProvider.GetRequiredService(type)) |
|
|
|
.Reverse() |
|
|
|
.ToArray(); |
|
|
|
|
|
|
|
//Try to get from the requested culture
|
|
|
|
var templateString = await GetContentOrNullAsync( |
|
|
|
contributors, |
|
|
|
new TemplateContentContributorContext( |
|
|
|
templateDefinition, |
|
|
|
scope.ServiceProvider, |
|
|
|
searchCultureName |
|
|
|
) |
|
|
|
); |
|
|
|
string templateString = null; |
|
|
|
|
|
|
|
if (templateString != null) |
|
|
|
if (cultureName == null && useCurrentCultureIfCultureNameIsNull) |
|
|
|
{ |
|
|
|
return templateString; |
|
|
|
cultureName = CultureInfo.CurrentUICulture.Name; |
|
|
|
} |
|
|
|
|
|
|
|
if (!tryDefaults) |
|
|
|
var contributors = CreateTemplateContentContributors(scope.ServiceProvider); |
|
|
|
|
|
|
|
//Try to get from the requested culture
|
|
|
|
if (cultureName != null) |
|
|
|
{ |
|
|
|
if (templateDefinition.IsInlineLocalized && cultureName == null) |
|
|
|
{ |
|
|
|
//Try to get culture independent content
|
|
|
|
templateString = await GetContentOrNullAsync( |
|
|
|
contributors, |
|
|
|
new TemplateContentContributorContext( |
|
|
|
templateDefinition, |
|
|
|
scope.ServiceProvider, |
|
|
|
null |
|
|
|
) |
|
|
|
); |
|
|
|
templateString = await GetContentOrNullAsync( |
|
|
|
contributors, |
|
|
|
new TemplateContentContributorContext( |
|
|
|
templateDefinition, |
|
|
|
scope.ServiceProvider, |
|
|
|
cultureName |
|
|
|
) |
|
|
|
); |
|
|
|
|
|
|
|
if (templateString != null) |
|
|
|
{ |
|
|
|
return templateString; |
|
|
|
} |
|
|
|
if (templateString != null) |
|
|
|
{ |
|
|
|
return templateString; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!tryDefaults) |
|
|
|
{ |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
//Try to get from same culture without country code
|
|
|
|
if (searchCultureName.Contains("-")) //Example: "tr-TR"
|
|
|
|
if (cultureName != null && cultureName.Contains("-")) //Example: "tr-TR"
|
|
|
|
{ |
|
|
|
templateString = await GetContentOrNullAsync( |
|
|
|
contributors, |
|
|
|
new TemplateContentContributorContext( |
|
|
|
templateDefinition, |
|
|
|
scope.ServiceProvider, |
|
|
|
CultureHelper.GetBaseCultureName(searchCultureName) |
|
|
|
CultureHelper.GetBaseCultureName(cultureName) |
|
|
|
) |
|
|
|
); |
|
|
|
|
|
|
|
@ -113,7 +102,7 @@ namespace Volo.Abp.TextTemplating |
|
|
|
return templateString; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (templateDefinition.IsInlineLocalized) |
|
|
|
{ |
|
|
|
//Try to get culture independent content
|
|
|
|
@ -157,6 +146,14 @@ namespace Volo.Abp.TextTemplating |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual ITemplateContentContributor[] CreateTemplateContentContributors(IServiceProvider serviceProvider) |
|
|
|
{ |
|
|
|
return Options.ContentContributors |
|
|
|
.Select(type => (ITemplateContentContributor)serviceProvider.GetRequiredService(type)) |
|
|
|
.Reverse() |
|
|
|
.ToArray(); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual async Task<string> GetContentOrNullAsync( |
|
|
|
ITemplateContentContributor[] contributors, |
|
|
|
TemplateContentContributorContext context) |
|
|
|
|