Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CultureDictionary IEnumerable #6202

Merged
merged 2 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace OrchardCore.Localization
{
/// <summary>
/// Represents a dictionary for a certain culture.
/// </summary>
public class CultureDictionary
public class CultureDictionary : IEnumerable<CultureDictionaryRecord>
{
/// <summary>
/// Creates a new instance of <see cref="CultureDictionary"/>.
Expand Down Expand Up @@ -76,5 +77,15 @@ public void MergeTranslations(IEnumerable<CultureDictionaryRecord> records)
Translations[record.Key] = record.Translations;
}
}

public IEnumerator<CultureDictionaryRecord> GetEnumerator()
{
foreach (var item in Translations)
{
yield return new CultureDictionaryRecord(item.Key, null, item.Value);
}
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
26 changes: 26 additions & 0 deletions test/OrchardCore.Tests/Localization/CultureDictionaryTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using OrchardCore.Localization;
using Xunit;

namespace OrchardCore.Tests.Localization
{
public class CultureDictionaryTests
{
private static PluralizationRuleDelegate _arPluralRule = n => (n == 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5);
private static PluralizationRuleDelegate _csPluralRule = n => ((n == 1) ? 0 : (n >= 2 && n <= 4) ? 1 : 2);

[Fact]
Expand Down Expand Up @@ -50,5 +53,28 @@ public void IntexerThrowsPluralFormNotFoundExceptionIfSpecifiedPluralFormDoesntE

Assert.Throws<PluralFormNotFoundException>(() => dictionary["ball", 5]);
}

[Fact]
public void EnumerateCultureDictionary()
{
// Arrange
var dictionary = new CultureDictionary("ar", _arPluralRule);
dictionary.MergeTranslations(new List<CultureDictionaryRecord>
{
new CultureDictionaryRecord("Hello", "مرحبا"),
new CultureDictionaryRecord("Bye", "مع السلامة")
});

// Act & Assert
Assert.NotEmpty(dictionary);

foreach (var record in dictionary)
{
Assert.NotNull(record.Key);
Assert.Single(record.Translations);
}

Assert.Equal(2, dictionary.Count());
}
}
}