mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 19:04:51 +08:00
Changing the default IText implementation to support "shell" lifetime scope
Text depends on IWorkContextAccessor instead of ICultureManager CurrentCulture string added to WorkContext Enables Localizer T properties to be used by singleton per-shell components --HG-- branch : perf extra : rebase_source : cf262af29af17af8059ee7d0571e8c643a71d3ef
This commit is contained in:
@@ -113,10 +113,10 @@ namespace Orchard.Setup {
|
||||
|
||||
[UsedImplicitly]
|
||||
class SafeModeSiteWorkContextProvider : IWorkContextStateProvider {
|
||||
public Func<T> Get<T>(string name) {
|
||||
public Func<WorkContext, T> Get<T>(string name) {
|
||||
if (name == "CurrentSite") {
|
||||
ISite safeModeSite = new SafeModeSite();
|
||||
return () => (T)safeModeSite;
|
||||
return ctx => (T)safeModeSite;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@@ -111,7 +111,7 @@ namespace Orchard.Environment {
|
||||
if (resolver == null) {
|
||||
return () => default(T);
|
||||
}
|
||||
return () => resolver();
|
||||
return () => resolver(this);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -9,9 +9,9 @@ namespace Orchard {
|
||||
WorkContext GetContext();
|
||||
IWorkContextScope CreateWorkContextScope();
|
||||
}
|
||||
|
||||
|
||||
public interface IWorkContextStateProvider : IDependency {
|
||||
Func<T> Get<T>(string name);
|
||||
Func<WorkContext, T> Get<T>(string name);
|
||||
}
|
||||
|
||||
public interface IWorkContextScope : IDisposable {
|
||||
|
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Orchard.Settings;
|
||||
|
||||
namespace Orchard.Localization.Services {
|
||||
public class CurrentCultureWorkContext : IWorkContextStateProvider {
|
||||
private readonly ICultureManager _cultureManager;
|
||||
|
||||
public CurrentCultureWorkContext(ICultureManager cultureManager) {
|
||||
_cultureManager = cultureManager;
|
||||
}
|
||||
|
||||
public Func<WorkContext, T> Get<T>(string name) {
|
||||
if (name == "CurrentCulture") {
|
||||
return ctx => (T)(object)_cultureManager.GetCurrentCulture(ctx.HttpContext);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,12 +8,13 @@ using Orchard.Logging;
|
||||
namespace Orchard.Localization {
|
||||
public class Text : IText {
|
||||
private readonly string _scope;
|
||||
private readonly ICultureManager _cultureManager;
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
private readonly ILocalizedStringManager _localizedStringManager;
|
||||
|
||||
public Text(string scope, ICultureManager cultureManager, ILocalizedStringManager localizedStringManager) {
|
||||
|
||||
public Text(string scope, IWorkContextAccessor workContextAccessor, ILocalizedStringManager localizedStringManager) {
|
||||
_scope = scope;
|
||||
_cultureManager = cultureManager;
|
||||
_workContextAccessor = workContextAccessor;
|
||||
_localizedStringManager = localizedStringManager;
|
||||
Logger = NullLogger.Instance;
|
||||
}
|
||||
@@ -23,7 +24,8 @@ namespace Orchard.Localization {
|
||||
public LocalizedString Get(string textHint, params object[] args) {
|
||||
Logger.Debug("{0} localizing '{1}'", _scope, textHint);
|
||||
|
||||
string currentCulture = HttpContext.Current == null ? _cultureManager.GetSiteCulture() : _cultureManager.GetCurrentCulture(new HttpContextWrapper(HttpContext.Current));
|
||||
var workContext = _workContextAccessor.GetContext();
|
||||
var currentCulture = workContext.CurrentCulture;
|
||||
var localizedFormat = _localizedStringManager.GetLocalizedString(_scope, textHint, currentCulture);
|
||||
|
||||
return args.Length == 0
|
||||
|
@@ -177,6 +177,7 @@
|
||||
<Compile Include="Environment\Extensions\Loaders\RawThemeExtensionLoader.cs" />
|
||||
<Compile Include="Environment\Features\FeatureManager.cs" />
|
||||
<Compile Include="Environment\IAssemblyLoader.cs" />
|
||||
<Compile Include="Localization\Services\CurrentCultureWorkContext.cs" />
|
||||
<Compile Include="Localization\Services\DefaultLocalizedStringManager.cs" />
|
||||
<Compile Include="Localization\Services\ILocalizedStringManager.cs" />
|
||||
<Compile Include="Mvc\IOrchardViewPage.cs" />
|
||||
|
@@ -8,9 +8,9 @@ namespace Orchard.Security {
|
||||
_authenticationService = authenticationService;
|
||||
}
|
||||
|
||||
public Func<T> Get<T>(string name) {
|
||||
public Func<WorkContext, T> Get<T>(string name) {
|
||||
if (name == "CurrentUser")
|
||||
return () => (T)_authenticationService.GetAuthenticatedUser();
|
||||
return ctx => (T)_authenticationService.GetAuthenticatedUser();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -8,10 +8,10 @@ namespace Orchard.Settings {
|
||||
_siteService = siteService;
|
||||
}
|
||||
|
||||
public Func<T> Get<T>(string name) {
|
||||
public Func<WorkContext, T> Get<T>(string name) {
|
||||
if (name == "CurrentSite") {
|
||||
var siteSettings = _siteService.GetSiteSettings();
|
||||
return () => (T)siteSettings;
|
||||
return ctx => (T)siteSettings;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@@ -10,10 +10,10 @@ namespace Orchard.UI.Zones {
|
||||
_shapeFactory = shapeFactory;
|
||||
}
|
||||
|
||||
public Func<T> Get<T>(string name) {
|
||||
public Func<WorkContext, T> Get<T>(string name) {
|
||||
if (name == "Layout") {
|
||||
var layout = _shapeFactory.Create("Layout", Arguments.Empty());
|
||||
return () => (T)layout;
|
||||
return ctx => (T)layout;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using System.Web;
|
||||
using System;
|
||||
using System.Web;
|
||||
using Orchard.Environment.Extensions.Models;
|
||||
using Orchard.Security;
|
||||
using Orchard.Settings;
|
||||
@@ -36,5 +37,10 @@ namespace Orchard {
|
||||
get { return GetState<ExtensionDescriptor>("CurrentTheme"); }
|
||||
set { SetState("CurrentTheme", value); }
|
||||
}
|
||||
|
||||
public string CurrentCulture {
|
||||
get { return GetState<string>("CurrentCulture"); }
|
||||
set { SetState("CurrentCulture", value); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user