mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Adding Culture selector to Localization module, Culture selector is a shape
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.Localization.Services;
|
||||
using Orchard.Mvc.Extensions;
|
||||
|
||||
namespace Orchard.Localization.Controllers {
|
||||
[OrchardFeature("Orchard.Localization.CutlureSelector")]
|
||||
public class CutlureSelectorController : Controller {
|
||||
private readonly ICultureService _cultureService;
|
||||
|
||||
public CutlureSelectorController(ICultureService cultureService) {
|
||||
_cultureService = cultureService;
|
||||
}
|
||||
|
||||
public ActionResult SetCulture(string culture, string returnUrl) {
|
||||
|
||||
_cultureService.SetCulture(culture);
|
||||
|
||||
return this.RedirectLocal(returnUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,4 +14,8 @@ Features:
|
||||
Orchard.Localization.DateTimeFormat:
|
||||
Description: Enables localization of date/time formats and names of days and months.
|
||||
Category: Content
|
||||
Name: Date/Time Format Localization
|
||||
Name: Date/Time Format Localization
|
||||
Orchard.Localization.CutlureSelector:
|
||||
Description: Enables a culture picker to localize the UI.
|
||||
Category: Content
|
||||
Name: Culture Picker
|
||||
@@ -66,6 +66,9 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Controllers\AdminController.cs" />
|
||||
<Compile Include="Controllers\CutlureSelectorController.cs" />
|
||||
<Compile Include="Providers\CookieCultureSelector.cs" />
|
||||
<Compile Include="Services\ICultureService.cs" />
|
||||
<Compile Include="Drivers\LocalizationPartDriver.cs" />
|
||||
<Compile Include="Handlers\LocalizationPartHandler.cs" />
|
||||
<Compile Include="Migrations.cs" />
|
||||
@@ -74,6 +77,8 @@
|
||||
<Compile Include="Projections\CurrentCultureFilter.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ResourceManifest.cs" />
|
||||
<Compile Include="Routes.cs" />
|
||||
<Compile Include="Services\ICultureStorage.cs" />
|
||||
<Compile Include="Services\ILocalizationService.cs" />
|
||||
<Compile Include="Services\LocalizationDateTimeFormatProvider.cs" />
|
||||
<Compile Include="Services\LocalizationService.cs" />
|
||||
@@ -125,6 +130,9 @@
|
||||
<ItemGroup>
|
||||
<Content Include="web.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Views\UICultureSelector.cshtml" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Globalization;
|
||||
using System.Web;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.Localization.Services;
|
||||
|
||||
namespace Orchard.Localization.Providers {
|
||||
[OrchardFeature("Orchard.Localization.CutlureSelector")]
|
||||
public class CookieCultureSelector : ICultureSelector {
|
||||
private readonly ICultureService _cultureService;
|
||||
|
||||
public CookieCultureSelector(ICultureService cultureService) {
|
||||
_cultureService = cultureService;
|
||||
}
|
||||
|
||||
public CultureSelectorResult GetCulture(HttpContextBase context) {
|
||||
var cultureName = _cultureService.GetCulture();
|
||||
if (cultureName == null) return null;
|
||||
|
||||
return new CultureSelectorResult { Priority = -4, CultureName = cultureName };
|
||||
}
|
||||
}
|
||||
}
|
||||
34
src/Orchard.Web/Modules/Orchard.Localization/Routes.cs
Normal file
34
src/Orchard.Web/Modules/Orchard.Localization/Routes.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.Mvc.Routes;
|
||||
|
||||
namespace Orchard.Localization {
|
||||
[OrchardFeature("Orchard.Localization.CutlurePicker")]
|
||||
public class Routes : IRouteProvider {
|
||||
public void GetRoutes(ICollection<RouteDescriptor> routes) {
|
||||
foreach (var routeDescriptor in GetRoutes())
|
||||
routes.Add(routeDescriptor);
|
||||
}
|
||||
|
||||
public IEnumerable<RouteDescriptor> GetRoutes() {
|
||||
return new[] {
|
||||
new RouteDescriptor {
|
||||
Route = new Route(
|
||||
"Culture/Change",
|
||||
new RouteValueDictionary {
|
||||
{"area", "Orchard.Localization"},
|
||||
{"controller", "Culture"},
|
||||
{"action", "ChangeCulture"}
|
||||
},
|
||||
new RouteValueDictionary(),
|
||||
new RouteValueDictionary {
|
||||
{"area", "Orchard.Localization"}
|
||||
},
|
||||
new MvcRouteHandler())
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Orchard.Environment.Extensions;
|
||||
|
||||
namespace Orchard.Localization.Services {
|
||||
public interface ICultureService : IDependency {
|
||||
void SetCulture(string culture);
|
||||
string GetCulture();
|
||||
}
|
||||
|
||||
[OrchardFeature("Orchard.Localization.CutlureSelector")]
|
||||
public class CultureService : ICultureService {
|
||||
private readonly ICultureStorage _cultureStorage;
|
||||
|
||||
public CultureService(ICultureStorage cultureStorage) {
|
||||
_cultureStorage = cultureStorage;
|
||||
}
|
||||
public void SetCulture(string culture) {
|
||||
_cultureStorage.SetCulture(culture);
|
||||
}
|
||||
|
||||
public string GetCulture() {
|
||||
return _cultureStorage.GetCulture();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Web;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.Mvc;
|
||||
using Orchard.Services;
|
||||
|
||||
namespace Orchard.Localization.Services {
|
||||
public interface ICultureStorage : IDependency {
|
||||
void SetCulture(string culture);
|
||||
string GetCulture();
|
||||
}
|
||||
|
||||
[OrchardFeature("Orchard.Localization.CutlureSelector")]
|
||||
public class DefaultCultureStorage : ICultureStorage {
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly IClock _clock;
|
||||
private readonly ShellSettings _shellSettings;
|
||||
|
||||
private const string CookieName = "OrchardCurrentCulture";
|
||||
private const int DefaultExpireTimeYear = 1;
|
||||
|
||||
public DefaultCultureStorage(IHttpContextAccessor httpContextAccessor,
|
||||
IClock clock,
|
||||
ShellSettings shellSettings) {
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_clock = clock;
|
||||
_shellSettings = shellSettings;
|
||||
}
|
||||
|
||||
public void SetCulture(string culture) {
|
||||
var httpContext = _httpContextAccessor.Current();
|
||||
|
||||
if (httpContext == null) return;
|
||||
|
||||
var cookie = new HttpCookie(CookieName, culture) {
|
||||
Expires = _clock.UtcNow.AddYears(DefaultExpireTimeYear),
|
||||
};
|
||||
|
||||
if (!String.IsNullOrEmpty(_shellSettings.RequestUrlHost)) {
|
||||
cookie.Domain = _shellSettings.RequestUrlHost;
|
||||
}
|
||||
|
||||
if (!String.IsNullOrEmpty(_shellSettings.RequestUrlPrefix)) {
|
||||
cookie.Path = GetCookiePath(httpContext);
|
||||
}
|
||||
|
||||
httpContext.Request.Cookies.Remove(CookieName);
|
||||
httpContext.Response.Cookies.Remove(CookieName);
|
||||
httpContext.Response.Cookies.Add(cookie);
|
||||
}
|
||||
|
||||
public string GetCulture() {
|
||||
var httpContext = _httpContextAccessor.Current();
|
||||
|
||||
if (httpContext == null) return null;
|
||||
|
||||
var cookie = httpContext.Request.Cookies.Get(CookieName);
|
||||
|
||||
if (cookie == null) return null;
|
||||
|
||||
return cookie.Value;
|
||||
}
|
||||
|
||||
private string GetCookiePath(HttpContextBase httpContext) {
|
||||
var cookiePath = httpContext.Request.ApplicationPath;
|
||||
if (cookiePath != null && cookiePath.Length > 1) {
|
||||
cookiePath += '/';
|
||||
}
|
||||
|
||||
cookiePath += _shellSettings.RequestUrlPrefix;
|
||||
|
||||
return cookiePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
@using Orchard.ContentManagement
|
||||
@using Orchard.Localization.Services
|
||||
@{
|
||||
var currentCulture = WorkContext.CurrentCulture;
|
||||
var supportedCultures = WorkContext.Resolve<ICultureManager>().ListCultures();
|
||||
}
|
||||
|
||||
<div id="culture-selection">
|
||||
<ul>
|
||||
@foreach (var supportedCulure in supportedCultures) {
|
||||
var url = Url.Action("SetCulture", "CutlureSelector", new { area = "Orchard.Localization", culture = supportedCulure, returnUrl = Html.ViewContext.HttpContext.Request.RawUrl });
|
||||
|
||||
<li>
|
||||
@if (supportedCulure.Equals(currentCulture)) {
|
||||
<a href="@url">@T("{0} (current)", supportedCulure)</a>
|
||||
} else {
|
||||
<a href="@url">@supportedCulure</a>
|
||||
}
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
</div>
|
||||
Reference in New Issue
Block a user