Skip to content

Commit

Permalink
Introduce DataLocalizedString
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed Oct 5, 2019
1 parent ff3624b commit b39500a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ public class ContentFieldDataLocalizationProvider : ILocalizationDataProvider
{
private readonly IContentDefinitionService _contentDefinitionService;

private static readonly string ContentFieldsContext = "Content Fields";

public ContentFieldDataLocalizationProvider(IContentDefinitionService contentDefinitionService)
{
_contentDefinitionService = contentDefinitionService;
}

// TODO: Check if there's a better way to get the fields
public IEnumerable<string> GetAllStrings()
public IEnumerable<DataLocalizedString> GetAllStrings()
=> _contentDefinitionService.GetTypes()
.SelectMany(t => t.TypeDefinition.Parts)
.Where(p => p.PartDefinition.Fields.Count() > 0)
.SelectMany(p => p.PartDefinition.Fields.Select(f => f.Name));
.SelectMany(p => p.PartDefinition.Fields.Select(f => new DataLocalizedString(ContentFieldsContext, f.Name)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ public class ContentTypeDataLocalizationProvider : ILocalizationDataProvider
{
private readonly IContentDefinitionService _contentDefinitionService;

private static readonly string ContentTypesContext = "Content Types";

public ContentTypeDataLocalizationProvider(IContentDefinitionService contentDefinitionService)
{
_contentDefinitionService = contentDefinitionService;
}

public IEnumerable<string> GetAllStrings()
=> _contentDefinitionService.GetTypes().Select(t => t.DisplayName);
public IEnumerable<DataLocalizedString> GetAllStrings()
=> _contentDefinitionService.GetTypes().Select(t => new DataLocalizedString(ContentTypesContext, t.DisplayName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace OrchardCore.Localization
{
public class DataLocalizedString
{
public DataLocalizedString(string context, string name)
{
Context = context;
Name = name;
}

public string Context { get; }

public string Name { get; }

public override string ToString() => $"{Context}-{Name}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace OrchardCore.Localization
{
public interface ILocalizationDataProvider
{
IEnumerable<string> GetAllStrings();
IEnumerable<DataLocalizedString> GetAllStrings();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Linq;
using Moq;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Services;
using OrchardCore.ContentTypes.ViewModels;
using Xunit;

namespace OrchardCore.Tests.Modules.OrchardCore.Localization
{
public class LocalizationDataProviderTests
{
[Fact]
public void ContentTypeDataLocalizationProvider_GetLocalizedStrings()
{
var contentDefinitionService = new Mock<IContentDefinitionService>();
contentDefinitionService.Setup(cds => cds.GetTypes())
.Returns(() => new List<EditTypeViewModel> {
new EditTypeViewModel { DisplayName = "Article" },
new EditTypeViewModel { DisplayName = "BlogPost" },
new EditTypeViewModel { DisplayName = "News" }
});
var localizationDataProvider = new ContentTypeDataLocalizationProvider(contentDefinitionService.Object);
var localizedStrings = localizationDataProvider.GetAllStrings();

Assert.Equal(3, localizedStrings.Count());
Assert.True(localizedStrings.All(s => s.Context == "Content Types"));
}

[Fact]
public void ContentFieldDataLocalizationProvider_GetLocalizedStrings()
{
var contentDefinitionService = new Mock<IContentDefinitionService>();
contentDefinitionService.Setup(cds => cds.GetTypes())
.Returns(() => new List<EditTypeViewModel>
{
new EditTypeViewModel { DisplayName = "BlogPost", TypeDefinition = CreateContentTypeDefinition("BlogPost", "Blog Post", new [] { "Title", "Body", "Author" }) },
new EditTypeViewModel { DisplayName = "Person", TypeDefinition = CreateContentTypeDefinition("Person", "Person", new [] { "FirstName", "LastName" }) },
});
var localizationDataProvider = new ContentFieldDataLocalizationProvider(contentDefinitionService.Object);
var localizedStrings = localizationDataProvider.GetAllStrings();

Assert.Equal(5, localizedStrings.Count());
Assert.True(localizedStrings.All(s => s.Context == "Content Fields"));
}

private ContentTypeDefinition CreateContentTypeDefinition(string name, string displayName, string[] fields)
{
var contentPartFieldDefinitions = new List<ContentPartFieldDefinition>();

foreach (var field in fields)
{
contentPartFieldDefinitions.Add(new ContentPartFieldDefinition(new ContentFieldDefinition("TextField"), field, null));
}

return new ContentTypeDefinition(
name,
displayName,
new List<ContentTypePartDefinition> { new ContentTypePartDefinition("Part", new ContentPartDefinition("Part", contentPartFieldDefinitions, null), null) },
null);
}
}
}

0 comments on commit b39500a

Please sign in to comment.