Files
Umbraco-CMS/src/Umbraco.Core/Models/DictionaryItem.cs
Elitsa Marinovska ba423a0108 V12: Add ISO codes to make the migration from language IDs easier (#14567)
* Change the obsoletion messages for language IDs to target V14 instead of V13.

* Wrong Language file

* Add ISO codes required to migrate custom code from language IDs

* Population of the new language FallbackIsoCode prop

* Changing obsoletion msgs from v13 to v14

* Fix breaking changes
2023-07-18 10:53:14 +02:00

84 lines
2.4 KiB
C#

using System.Runtime.Serialization;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Models;
/// <summary>
/// Represents a Dictionary Item
/// </summary>
[Serializable]
[DataContract(IsReference = true)]
public class DictionaryItem : EntityBase, IDictionaryItem
{
// Custom comparer for enumerable
private static readonly DelegateEqualityComparer<IEnumerable<IDictionaryTranslation>>
DictionaryTranslationComparer =
new(
(enumerable, translations) => enumerable.UnsortedSequenceEqual(translations),
enumerable => enumerable.GetHashCode());
private string _itemKey;
private Guid? _parentId;
private IEnumerable<IDictionaryTranslation> _translations;
public DictionaryItem(string itemKey)
: this(null, itemKey)
{
}
public DictionaryItem(Guid? parentId, string itemKey)
{
_parentId = parentId;
_itemKey = itemKey;
_translations = new List<IDictionaryTranslation>();
}
[Obsolete("This will be removed in V14.")]
public Func<int, ILanguage?>? GetLanguage { get; set; }
/// <summary>
/// Gets or Sets the Parent Id of the Dictionary Item
/// </summary>
[DataMember]
public Guid? ParentId
{
get => _parentId;
set => SetPropertyValueAndDetectChanges(value, ref _parentId, nameof(ParentId));
}
/// <summary>
/// Gets or sets the Key for the Dictionary Item
/// </summary>
[DataMember]
public string ItemKey
{
get => _itemKey;
set => SetPropertyValueAndDetectChanges(value, ref _itemKey!, nameof(ItemKey));
}
/// <summary>
/// Gets or sets a list of translations for the Dictionary Item
/// </summary>
[DataMember]
public IEnumerable<IDictionaryTranslation> Translations
{
get => _translations;
set
{
IDictionaryTranslation[] asArray = value.ToArray();
// ensure the language callback is set on each translation
if (GetLanguage != null)
{
foreach (DictionaryTranslation translation in asArray.OfType<DictionaryTranslation>())
{
translation.GetLanguage = GetLanguage;
}
}
SetPropertyValueAndDetectChanges(asArray, ref _translations!, nameof(Translations), DictionaryTranslationComparer);
}
}
}