mirror of https://github.com/abpframework/abp.git
12 changed files with 179 additions and 19 deletions
@ -0,0 +1,84 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.FileProviders; |
|||
using Volo.Abp.VirtualFileSystem; |
|||
|
|||
namespace Volo.Abp.TextTemplating.VirtualFiles |
|||
{ |
|||
public class VirtualFileTemplateContributor : ITemplateContributor |
|||
{ |
|||
private readonly string _virtualPath; |
|||
private IVirtualFileProvider _virtualFileProvider; |
|||
private volatile Dictionary<string, string> _templateDictionary; |
|||
private readonly object _syncObj = new object(); |
|||
|
|||
public VirtualFileTemplateContributor(string virtualPath) |
|||
{ |
|||
_virtualPath = virtualPath; |
|||
} |
|||
|
|||
public void Initialize(TemplateContributorInitializationContext context) |
|||
{ |
|||
_virtualFileProvider = context.ServiceProvider.GetRequiredService<IVirtualFileProvider>(); |
|||
} |
|||
|
|||
public string GetOrNull([CanBeNull] string cultureName = null) |
|||
{ |
|||
var dictionary = GetTemplateDictionary(); |
|||
|
|||
if (cultureName == null) |
|||
{ |
|||
return dictionary.GetOrDefault("__default"); |
|||
} |
|||
else |
|||
{ |
|||
return dictionary.GetOrDefault(cultureName) ?? |
|||
dictionary.GetOrDefault("__default"); |
|||
} |
|||
} |
|||
|
|||
private Dictionary<string, string> GetTemplateDictionary() |
|||
{ |
|||
if (_templateDictionary != null) |
|||
{ |
|||
return _templateDictionary; |
|||
} |
|||
|
|||
lock (_syncObj) |
|||
{ |
|||
if (_templateDictionary != null) |
|||
{ |
|||
return _templateDictionary; |
|||
} |
|||
|
|||
var dictionary = new Dictionary<string, string>(); |
|||
|
|||
var fileInfo = _virtualFileProvider.GetFileInfo(_virtualPath); |
|||
if (!fileInfo.IsDirectory) |
|||
{ |
|||
//TODO: __default to consts
|
|||
dictionary.Add("__default", fileInfo.ReadAsString()); |
|||
} |
|||
else |
|||
{ |
|||
foreach (var file in _virtualFileProvider.GetDirectoryContents(_virtualPath)) |
|||
{ |
|||
if (file.IsDirectory) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
// TODO: How to normalize file names?
|
|||
dictionary.Add(file.Name.RemovePostFix(".tpl"), file.ReadAsString()); |
|||
} |
|||
} |
|||
|
|||
_templateDictionary = dictionary; |
|||
return dictionary; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1 @@ |
|||
Please click to the following link to get an email to reset your password! |
|||
@ -0,0 +1 @@ |
|||
Welcome to the abp.io! |
|||
@ -0,0 +1 @@ |
|||
abp.io'ya hoşgeldiniz! |
|||
@ -1,17 +1,22 @@ |
|||
namespace Volo.Abp.TextTemplating |
|||
using Volo.Abp.TextTemplating.VirtualFiles; |
|||
|
|||
namespace Volo.Abp.TextTemplating |
|||
{ |
|||
public class TestTemplateDefinitionProvider : TemplateDefinitionProvider |
|||
{ |
|||
public override void Define(ITemplateDefinitionContext context) |
|||
{ |
|||
context |
|||
.Add( |
|||
new TemplateDefinition( |
|||
TestTemplates.TestTemplate1 |
|||
), new TemplateDefinition( |
|||
TestTemplates.TestTemplateLayout1 |
|||
) |
|||
); |
|||
context.Add( |
|||
new TemplateDefinition( |
|||
TestTemplates.TestTemplate1 |
|||
).AddContributor( |
|||
new VirtualFileTemplateContributor("/SampleTemplates/WelcomeEmail") |
|||
) |
|||
); |
|||
|
|||
context.Add(new TemplateDefinition( |
|||
TestTemplates.TestTemplateLayout1 |
|||
)); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,50 @@ |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.TextTemplating.VirtualFiles |
|||
{ |
|||
public class VirtualFileTemplateContributor_Tests : AbpTextTemplatingTestBase |
|||
{ |
|||
[Fact] |
|||
public void Should_Get_Localized_Content_By_Culture() |
|||
{ |
|||
var contributor = new VirtualFileTemplateContributor( |
|||
"/SampleTemplates/WelcomeEmail" |
|||
); |
|||
|
|||
contributor.Initialize( |
|||
new TemplateContributorInitializationContext( |
|||
new TemplateDefinition("Test"), |
|||
ServiceProvider |
|||
) |
|||
); |
|||
|
|||
contributor |
|||
.GetOrNull("en") |
|||
.ShouldBe("Welcome to the abp.io!"); |
|||
|
|||
contributor |
|||
.GetOrNull("tr") |
|||
.ShouldBe("abp.io'ya hoşgeldiniz!"); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Get_Non_Localized_Template_Content() |
|||
{ |
|||
var contributor = new VirtualFileTemplateContributor( |
|||
"/SampleTemplates/ForgotPasswordEmail.tpl" |
|||
); |
|||
|
|||
contributor.Initialize( |
|||
new TemplateContributorInitializationContext( |
|||
new TemplateDefinition("Test"), |
|||
ServiceProvider |
|||
) |
|||
); |
|||
|
|||
contributor |
|||
.GetOrNull() |
|||
.ShouldBe("Please click to the following link to get an email to reset your password!"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue