mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-19 10:07:55 +08:00
- A handler for localized part.
- Finishing up the infrastructure support for content item localization. --HG-- branch : dev
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.Core.Localization.Models;
|
||||
using Orchard.Data;
|
||||
using Orchard.Localization;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.Localization.Services;
|
||||
|
||||
namespace Orchard.Core.Localization.Handlers {
|
||||
[UsedImplicitly]
|
||||
public class LocalizedHandler : ContentHandler {
|
||||
private readonly ICultureManager _cultureManager;
|
||||
private readonly IContentManager _contentManager;
|
||||
|
||||
public LocalizedHandler(IRepository<LocalizedRecord> localizedRepository, ICultureManager cultureManager, IContentManager contentManager) {
|
||||
_cultureManager = cultureManager;
|
||||
_contentManager = contentManager;
|
||||
T = NullLocalizer.Instance;
|
||||
|
||||
Filters.Add(StorageFilter.For(localizedRepository));
|
||||
|
||||
OnActivated<Localized>(InitializePart);
|
||||
|
||||
OnLoaded<Localized>(LazyLoadHandlers);
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
void LazyLoadHandlers(LoadContentContext context, Localized localized) {
|
||||
localized.CultureField.Loader(ctx => _cultureManager.GetCultureById(localized.Record.CultureId));
|
||||
localized.MasterContentItemField.Loader(ctx => _contentManager.Get(localized.Record.MasterContentItemId));
|
||||
}
|
||||
|
||||
void InitializePart(ActivatedContentContext context, Localized localized) {
|
||||
localized.CultureField.Setter(cultureRecord => {
|
||||
localized.Record.CultureId = cultureRecord.Id;
|
||||
return cultureRecord;
|
||||
});
|
||||
localized.MasterContentItemField.Setter(masterContentItem => {
|
||||
localized.Record.MasterContentItemId = masterContentItem.ContentItem.Id;
|
||||
return masterContentItem;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,20 +1,33 @@
|
||||
using System.Web.Mvc;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Utilities;
|
||||
using Orchard.Localization.Records;
|
||||
|
||||
namespace Orchard.Core.Localization.Models {
|
||||
public sealed class Localized : ContentPart<LocalizedRecord> {
|
||||
private readonly LazyField<CultureRecord> _culture = new LazyField<CultureRecord>();
|
||||
private readonly LazyField<IContent> _masterContentItem = new LazyField<IContent>();
|
||||
|
||||
public LazyField<CultureRecord> CultureField { get { return _culture; } }
|
||||
public LazyField<IContent> MasterContentItemField { get { return _masterContentItem; } }
|
||||
|
||||
[HiddenInput(DisplayValue = false)]
|
||||
public int Id { get { return ContentItem.Id; } }
|
||||
|
||||
public int CultureId {
|
||||
get { return Record.CultureId; }
|
||||
set { Record.CultureId = value; }
|
||||
public CultureRecord Culture {
|
||||
get { return _culture.Value; }
|
||||
set { _culture.Value = value; }
|
||||
}
|
||||
|
||||
public int MasterContentItemId {
|
||||
get { return Record.MasterContentItemId; }
|
||||
set { Record.MasterContentItemId = value; }
|
||||
public IContent MasterContentItem {
|
||||
get { return _masterContentItem.Value; }
|
||||
set { _masterContentItem.Value = value; }
|
||||
}
|
||||
|
||||
public bool HasTranslationGroup {
|
||||
get {
|
||||
return Record.MasterContentItemId != 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,40 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Core.Localization.Models;
|
||||
using Orchard.Localization.Services;
|
||||
using Localized = Orchard.Core.Localization.Models.Localized;
|
||||
|
||||
namespace Orchard.Core.Localization.Services {
|
||||
[UsedImplicitly]
|
||||
public class ContentItemLocalizationService : IContentItemLocalizationService {
|
||||
private readonly IContentManager _contentManager;
|
||||
private readonly ICultureManager _cultureManager;
|
||||
|
||||
public ContentItemLocalizationService(IContentManager contentManager, ICultureManager cultureManager) {
|
||||
_contentManager = contentManager;
|
||||
_cultureManager = cultureManager;
|
||||
}
|
||||
|
||||
public IEnumerable<Localized> Get() {
|
||||
return _contentManager.Query<Localized, LocalizedRecord>().List();
|
||||
}
|
||||
|
||||
public Localized Get(int localizedId) {
|
||||
return _contentManager.Get<Localized>(localizedId);
|
||||
}
|
||||
|
||||
public Localized GetLocalizationForCulture(int masterId, string cultureName) {
|
||||
var cultures = _cultureManager.ListCultures();
|
||||
if (cultures.Contains(cultureName)) {
|
||||
int cultureId = _cultureManager.GetCultureIdByName(cultureName);
|
||||
if (cultureId != 0) {
|
||||
return _contentManager.Query<Localized, LocalizedRecord>()
|
||||
.Where(x => x.MasterContentItemId == masterId && x.CultureId == cultureId).List().FirstOrDefault();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,10 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Core.Localization.Models;
|
||||
|
||||
namespace Orchard.Core.Localization.Services {
|
||||
public interface IContentItemLocalizationService : IDependency {
|
||||
IEnumerable<Localized> Get();
|
||||
Localized Get(int localizedId);
|
||||
Localized GetLocalizationForCulture(int masterId, string cultureName);
|
||||
}
|
||||
}
|
@@ -119,10 +119,9 @@
|
||||
<Compile Include="Indexing\Services\CreateIndexingTaskHandler.cs" />
|
||||
<Compile Include="Indexing\Services\IndexingTaskExecutor.cs" />
|
||||
<Compile Include="Indexing\Services\IndexingTaskManager.cs" />
|
||||
<Compile Include="Localization\Handlers\LocalizedHandler.cs" />
|
||||
<Compile Include="Localization\Models\Localized.cs" />
|
||||
<Compile Include="Localization\Models\LocalizedRecord.cs" />
|
||||
<Compile Include="Localization\Services\ContentItemLocalizationService.cs" />
|
||||
<Compile Include="Localization\Services\IContentItemLocalizationService.cs" />
|
||||
<Compile Include="Navigation\AdminMenu.cs" />
|
||||
<Compile Include="Navigation\Controllers\AdminController.cs" />
|
||||
<Compile Include="Navigation\Models\MenuItem.cs" />
|
||||
|
@@ -173,9 +173,9 @@ namespace Orchard.Setup.Services {
|
||||
}
|
||||
|
||||
var contentDefinitionManager = environment.Resolve<IContentDefinitionManager>();
|
||||
contentDefinitionManager.AlterTypeDefinition("blogpost", cfg => cfg.WithPart("HasComments").WithPart("HasTags"));
|
||||
contentDefinitionManager.AlterTypeDefinition("page", cfg => cfg.WithPart("HasComments").WithPart("HasTags"));
|
||||
contentDefinitionManager.AlterTypeDefinition("sandboxpage", cfg => cfg.WithPart("HasComments").WithPart("HasTags"));
|
||||
contentDefinitionManager.AlterTypeDefinition("blogpost", cfg => cfg.WithPart("HasComments").WithPart("HasTags").WithPart("Localized"));
|
||||
contentDefinitionManager.AlterTypeDefinition("page", cfg => cfg.WithPart("HasComments").WithPart("HasTags").WithPart("Localized"));
|
||||
contentDefinitionManager.AlterTypeDefinition("sandboxpage", cfg => cfg.WithPart("HasComments").WithPart("HasTags").WithPart("Localized"));
|
||||
}
|
||||
catch {
|
||||
environment.Resolve<ITransactionManager>().Cancel();
|
||||
|
@@ -47,11 +47,8 @@ namespace Orchard.Localization.Services {
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
public int GetCultureIdByName(string cultureName) {
|
||||
if (!IsValidCulture(cultureName)) {
|
||||
throw new ArgumentException("cultureName");
|
||||
}
|
||||
return _cultureRepository.Get(x => x.Culture == cultureName).Id;
|
||||
public CultureRecord GetCultureById(int id) {
|
||||
return _cultureRepository.Get(id);
|
||||
}
|
||||
|
||||
// "<languagecode2>" or
|
||||
|
@@ -1,11 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Web;
|
||||
using Orchard.Localization.Records;
|
||||
|
||||
namespace Orchard.Localization.Services {
|
||||
public interface ICultureManager : IDependency {
|
||||
IEnumerable<string> ListCultures();
|
||||
void AddCulture(string cultureName);
|
||||
string GetCurrentCulture(HttpContext requestContext);
|
||||
int GetCultureIdByName(string cultureName);
|
||||
CultureRecord GetCultureById(int id);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user