diff --git a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateContentContributorContext.cs b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateContentContributorContext.cs index bb304a3344..773bf1a0a4 100644 --- a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateContentContributorContext.cs +++ b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateContentContributorContext.cs @@ -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; } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateContentProvider.cs b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateContentProvider.cs index dcb5d88850..2e9c7af269 100644 --- a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateContentProvider.cs +++ b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateContentProvider.cs @@ -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 diff --git a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateDefinition.cs b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateDefinition.cs index f3b31a769d..769b970733 100644 --- a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateDefinition.cs +++ b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateDefinition.cs @@ -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; } diff --git a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateRenderer.cs b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateRenderer.cs index 8b67a380f0..6a1fff00ef 100644 --- a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateRenderer.cs +++ b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/TemplateRenderer.cs @@ -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(); + } - using (CultureHelper.Use(cultureName)) + if (cultureName == null) { return await RenderInternalAsync( templateName, - globalContext ?? new Dictionary(), + globalContext, model ); } + else + { + using (CultureHelper.Use(cultureName)) + { + return await RenderInternalAsync( + templateName, + globalContext, + model + ); + } + } } protected virtual async Task RenderInternalAsync( diff --git a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/FileInfoLocalizedTemplateContentReader.cs b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/FileInfoLocalizedTemplateContentReader.cs index 3ddfb973f3..cde913f307 100644 --- a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/FileInfoLocalizedTemplateContentReader.cs +++ b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/FileInfoLocalizedTemplateContentReader.cs @@ -14,7 +14,12 @@ namespace Volo.Abp.TextTemplating.VirtualFiles public string GetContentOrNull(string culture) { - return _content; + if (culture == null) + { + return _content; + } + + return null; } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/ILocalizedTemplateContentReader.cs b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/ILocalizedTemplateContentReader.cs index 611e50b005..7d837d731d 100644 --- a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/ILocalizedTemplateContentReader.cs +++ b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/ILocalizedTemplateContentReader.cs @@ -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); } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/VirtualFileTemplateContentContributor.cs b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/VirtualFileTemplateContentContributor.cs index 668d1dc3ec..ede33a3652 100644 --- a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/VirtualFileTemplateContentContributor.cs +++ b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/VirtualFileTemplateContentContributor.cs @@ -1,5 +1,4 @@ -using System.Globalization; -using System.Threading.Tasks; +using System.Threading.Tasks; using Volo.Abp.DependencyInjection; namespace Volo.Abp.TextTemplating.VirtualFiles diff --git a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/VirtualFolderLocalizedTemplateContentReader.cs b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/VirtualFolderLocalizedTemplateContentReader.cs index b43ab722b7..638a7db190 100644 --- a/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/VirtualFolderLocalizedTemplateContentReader.cs +++ b/framework/src/Volo.Abp.TextTemplating/Volo/Abp/TextTemplating/VirtualFiles/VirtualFolderLocalizedTemplateContentReader.cs @@ -35,6 +35,11 @@ namespace Volo.Abp.TextTemplating.VirtualFiles public string GetContentOrNull(string cultureName) { + if (cultureName == null) + { + return null; + } + return _dictionary.GetOrDefault(cultureName); } }