mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-12-02 19:44:02 +08:00
Adding CurrentCultureFilter implementation
--HG-- branch : 1.x
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
<RootNamespace>Orchard.Localization</RootNamespace>
|
<RootNamespace>Orchard.Localization</RootNamespace>
|
||||||
<AssemblyName>Orchard.Localization</AssemblyName>
|
<AssemblyName>Orchard.Localization</AssemblyName>
|
||||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||||
|
<UseIISExpress>false</UseIISExpress>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
@@ -53,6 +54,7 @@
|
|||||||
<Compile Include="Migrations.cs" />
|
<Compile Include="Migrations.cs" />
|
||||||
<Compile Include="Models\LocalizationPart.cs" />
|
<Compile Include="Models\LocalizationPart.cs" />
|
||||||
<Compile Include="Models\LocalizationPartRecord.cs" />
|
<Compile Include="Models\LocalizationPartRecord.cs" />
|
||||||
|
<Compile Include="Projections\CurrentCultureFilter.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="ResourceManifest.cs" />
|
<Compile Include="ResourceManifest.cs" />
|
||||||
<Compile Include="Services\ILocalizationService.cs" />
|
<Compile Include="Services\ILocalizationService.cs" />
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
using System;
|
||||||
|
using Orchard.ContentManagement;
|
||||||
|
using Orchard.Events;
|
||||||
|
using Orchard.Localization.Models;
|
||||||
|
using Orchard.Localization.Services;
|
||||||
|
|
||||||
|
namespace Orchard.Localization.Projections {
|
||||||
|
public interface IFilterProvider : IEventHandler {
|
||||||
|
void Describe(dynamic describe);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CurrentCultureFilter : IFilterProvider {
|
||||||
|
private readonly IWorkContextAccessor _workContextAccessor;
|
||||||
|
private readonly ICultureManager _cultureManager;
|
||||||
|
|
||||||
|
public CurrentCultureFilter(IWorkContextAccessor workContextAccessor, ICultureManager cultureManager) {
|
||||||
|
_workContextAccessor = workContextAccessor;
|
||||||
|
_cultureManager = cultureManager;
|
||||||
|
T = NullLocalizer.Instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
|
public void Describe(dynamic describe) {
|
||||||
|
describe.For("Localization", T("Localization"), T("Localization"))
|
||||||
|
.Element("ForCurrentCulture", T("For current culture"), T("Localized content items for current culture"),
|
||||||
|
(Action<dynamic>)ApplyFilter,
|
||||||
|
(Func<dynamic, LocalizedString>)DisplayFilter,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ApplyFilter(dynamic context) {
|
||||||
|
string currentCulture = _workContextAccessor.GetContext().CurrentCulture;
|
||||||
|
var currentCultureId = _cultureManager.GetCultureByName(currentCulture).Id;
|
||||||
|
|
||||||
|
var query = (IHqlQuery)context.Query;
|
||||||
|
context.Query = query.Where(x => x.ContentPartRecord<LocalizationPartRecord>(), x => x.Eq("CultureId", currentCultureId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalizedString DisplayFilter(dynamic context) {
|
||||||
|
return T("For current culture");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user