mirror of https://github.com/abpframework/abp.git
6 changed files with 164 additions and 56 deletions
@ -0,0 +1,65 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.FileProviders; |
||||
|
using Volo.Abp.Localization; |
||||
|
using Volo.Abp.VirtualFileSystem; |
||||
|
|
||||
|
namespace Volo.Abp.TextTemplating.VirtualFiles |
||||
|
{ |
||||
|
public class FolderLocalizedTemplateContentReader : ILocalizedTemplateContentReader |
||||
|
{ |
||||
|
private Dictionary<string, string> _dictionary; |
||||
|
|
||||
|
public async Task ReadContentsAsync(IVirtualFileProvider virtualFileProvider, string virtualPath) |
||||
|
{ |
||||
|
_dictionary = new Dictionary<string, string>(); |
||||
|
|
||||
|
var directoryInfo = virtualFileProvider.GetFileInfo(virtualPath); |
||||
|
if (!directoryInfo.IsDirectory) |
||||
|
{ |
||||
|
throw new AbpException("Given virtual path is not a folder: " + virtualPath); |
||||
|
} |
||||
|
|
||||
|
foreach (var file in virtualFileProvider.GetDirectoryContents(virtualPath)) |
||||
|
{ |
||||
|
if (file.IsDirectory) |
||||
|
{ |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
_dictionary.Add(file.Name.RemovePostFix(".tpl"), await file.ReadAsStringAsync()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public string GetContent(string cultureName, string defaultCultureName) |
||||
|
{ |
||||
|
var content = _dictionary.GetOrDefault(cultureName); |
||||
|
if (content != null) |
||||
|
{ |
||||
|
return content; |
||||
|
} |
||||
|
|
||||
|
if (cultureName.Contains("-")) |
||||
|
{ |
||||
|
var baseCultureName = CultureHelper.GetBaseCultureName(cultureName); |
||||
|
content = _dictionary.GetOrDefault(baseCultureName); |
||||
|
if (content != null) |
||||
|
{ |
||||
|
return content; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (defaultCultureName != null) |
||||
|
{ |
||||
|
content = _dictionary.GetOrDefault(defaultCultureName); |
||||
|
if (content != null) |
||||
|
{ |
||||
|
return content; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
namespace Volo.Abp.TextTemplating.VirtualFiles |
||||
|
{ |
||||
|
public interface ILocalizedTemplateContentReader |
||||
|
{ |
||||
|
public string GetContent(string culture, string defaultCultureName = null); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
namespace Volo.Abp.TextTemplating.VirtualFiles |
||||
|
{ |
||||
|
public class NullLocalizedTemplateContentReader : ILocalizedTemplateContentReader |
||||
|
{ |
||||
|
public static NullLocalizedTemplateContentReader Instance { get; } = new NullLocalizedTemplateContentReader(); |
||||
|
|
||||
|
private NullLocalizedTemplateContentReader() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public string GetContent( |
||||
|
string culture, |
||||
|
string defaultCultureName = null) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.FileProviders; |
||||
|
|
||||
|
namespace Volo.Abp.TextTemplating.VirtualFiles |
||||
|
{ |
||||
|
public class SingleFileLocalizedTemplateContentReader : ILocalizedTemplateContentReader |
||||
|
{ |
||||
|
private string _content; |
||||
|
|
||||
|
public async Task ReadContentsAsync(IFileInfo fileInfo) |
||||
|
{ |
||||
|
_content = await fileInfo.ReadAsStringAsync(); |
||||
|
} |
||||
|
|
||||
|
public string GetContent(string culture, string defaultCultureName) |
||||
|
{ |
||||
|
return _content; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue