mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
- ICultureSelector to set current request culture.
- Default implementation uses site setting with a low priority. - Tests. --HG-- branch : dev
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Web.Routing;
|
||||
using Autofac;
|
||||
using NHibernate;
|
||||
using NUnit.Framework;
|
||||
@@ -30,6 +32,7 @@ namespace Orchard.Tests.Localization {
|
||||
[SetUp]
|
||||
public void Init() {
|
||||
var builder = new ContainerBuilder();
|
||||
builder.RegisterType<TestCultureSelector>().As<ICultureSelector>();
|
||||
builder.RegisterType<DefaultCultureManager>().As<ICultureManager>();
|
||||
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));
|
||||
_session = _sessionFactory.OpenSession();
|
||||
@@ -76,6 +79,17 @@ namespace Orchard.Tests.Localization {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CultureManagerReturnsCultureFromSelector() {
|
||||
Assert.That(_cultureManager.GetCurrentCulture(null), Is.EqualTo("en-US"));
|
||||
}
|
||||
}
|
||||
|
||||
public class TestCultureSelector : ICultureSelector {
|
||||
public CultureSelectorResult GetCulture(RequestContext context) {
|
||||
return new CultureSelectorResult { Priority = 1, CultureName = "en-US" };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -16,7 +16,7 @@ namespace Orchard.Core.Settings.Models {
|
||||
public string SiteSalt {
|
||||
get { return Record.SiteSalt; }
|
||||
}
|
||||
public string SuperUser {
|
||||
public string SuperUser {
|
||||
get { return Record.SuperUser; }
|
||||
set { Record.SuperUser = value; }
|
||||
}
|
||||
@@ -24,5 +24,9 @@ namespace Orchard.Core.Settings.Models {
|
||||
get { return Record.HomePage; }
|
||||
set { Record.HomePage = value; }
|
||||
}
|
||||
public string SiteCulture {
|
||||
get { return Record.SiteCulture; }
|
||||
set { Record.SiteCulture = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -7,5 +7,6 @@ namespace Orchard.Core.Settings.Models {
|
||||
public virtual string SuperUser { get; set; }
|
||||
public virtual string PageTitleSeparator { get; set; }
|
||||
public virtual string HomePage { get; set; }
|
||||
public virtual string SiteCulture { get; set; }
|
||||
}
|
||||
}
|
@@ -125,12 +125,13 @@ namespace Orchard.Setup.Services {
|
||||
siteSettings.Record.SiteName = context.SiteName;
|
||||
siteSettings.Record.SuperUser = context.AdminUsername;
|
||||
siteSettings.Record.PageTitleSeparator = " - ";
|
||||
siteSettings.Record.SiteCulture = "en-US";
|
||||
|
||||
// set site theme
|
||||
var themeService = environment.Resolve<IThemeService>();
|
||||
themeService.SetSiteTheme("Classic");
|
||||
|
||||
// set default culture
|
||||
// add default culture
|
||||
var cultureManager = environment.Resolve<ICultureManager>();
|
||||
cultureManager.AddCulture("en-US");
|
||||
|
||||
|
@@ -123,6 +123,11 @@ namespace Orchard.Setup {
|
||||
get { return ""; }
|
||||
set { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public string SiteCulture {
|
||||
get { return ""; }
|
||||
set { throw new NotImplementedException(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,15 +2,18 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web.Routing;
|
||||
using Orchard.Data;
|
||||
using Orchard.Localization.Records;
|
||||
|
||||
namespace Orchard.Localization {
|
||||
public class DefaultCultureManager : ICultureManager {
|
||||
private readonly IRepository<CultureRecord> _cultureRepository;
|
||||
private readonly IEnumerable<ICultureSelector> _cultureSelectors;
|
||||
|
||||
public DefaultCultureManager(IRepository<CultureRecord> cultureRepository) {
|
||||
public DefaultCultureManager(IRepository<CultureRecord> cultureRepository, IEnumerable<ICultureSelector> cultureSelectors) {
|
||||
_cultureRepository = cultureRepository;
|
||||
_cultureSelectors = cultureSelectors;
|
||||
}
|
||||
|
||||
public IEnumerable<string> ListCultures() {
|
||||
@@ -25,6 +28,24 @@ namespace Orchard.Localization {
|
||||
_cultureRepository.Create(new CultureRecord { Culture = cultureName });
|
||||
}
|
||||
|
||||
public string GetCurrentCulture(RequestContext requestContext) {
|
||||
var requestCulture = _cultureSelectors
|
||||
.Select(x => x.GetCulture(requestContext))
|
||||
.Where(x => x != null)
|
||||
.OrderByDescending(x => x.Priority);
|
||||
|
||||
if (requestCulture.Count() < 1)
|
||||
return String.Empty;
|
||||
|
||||
foreach (var culture in requestCulture) {
|
||||
if (!String.IsNullOrEmpty(culture.CultureName)) {
|
||||
return culture.CultureName;
|
||||
}
|
||||
}
|
||||
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
// "<languagecode2>" or
|
||||
// "<languagecode2>-<country/regioncode2>" or
|
||||
// "<languagecode2>-<scripttag>-<country/regioncode2>"
|
||||
|
@@ -1,8 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Web.Routing;
|
||||
|
||||
namespace Orchard.Localization {
|
||||
public interface ICultureManager : IDependency {
|
||||
IEnumerable<string> ListCultures();
|
||||
void AddCulture(string cultureName);
|
||||
string GetCurrentCulture(RequestContext requestContext);
|
||||
}
|
||||
}
|
||||
|
12
src/Orchard/Localization/ICultureSelector.cs
Normal file
12
src/Orchard/Localization/ICultureSelector.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Web.Routing;
|
||||
|
||||
namespace Orchard.Localization {
|
||||
public class CultureSelectorResult {
|
||||
public int Priority { get; set; }
|
||||
public string CultureName { get; set; }
|
||||
}
|
||||
|
||||
public interface ICultureSelector : IDependency {
|
||||
CultureSelectorResult GetCulture(RequestContext context);
|
||||
}
|
||||
}
|
23
src/Orchard/Localization/SiteCultureSelector.cs
Normal file
23
src/Orchard/Localization/SiteCultureSelector.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Web.Routing;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.Settings;
|
||||
|
||||
namespace Orchard.Localization {
|
||||
public class SiteCultureSelector : ICultureSelector {
|
||||
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
|
||||
|
||||
public CultureSelectorResult GetCulture(RequestContext context) {
|
||||
string currentCultureName = CurrentSite.SiteCulture;
|
||||
|
||||
if (String.IsNullOrEmpty(currentCultureName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new CultureSelectorResult { Priority = -5, CultureName = currentCultureName };
|
||||
}
|
||||
}
|
||||
|
||||
public class CultureSettings {
|
||||
}
|
||||
}
|
@@ -145,7 +145,9 @@
|
||||
<Compile Include="IDependency.cs" />
|
||||
<Compile Include="Localization\DefaultCultureManager.cs" />
|
||||
<Compile Include="Localization\ICultureManager.cs" />
|
||||
<Compile Include="Localization\ICultureSelector.cs" />
|
||||
<Compile Include="Localization\Records\CultureRecord.cs" />
|
||||
<Compile Include="Localization\SiteCultureSelector.cs" />
|
||||
<Compile Include="Mvc\Html\ThemeExtensions.cs" />
|
||||
<Compile Include="Mvc\Routes\IRoutePublisher.cs" />
|
||||
<Compile Include="Mvc\Routes\IRouteProvider.cs" />
|
||||
|
@@ -10,5 +10,6 @@ namespace Orchard.Settings {
|
||||
string SiteSalt { get; }
|
||||
string SuperUser { get; }
|
||||
string HomePage { get; set; }
|
||||
string SiteCulture { get; set; }
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user