using System;
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;
using Umbraco.Core.Models.Entities;
namespace Umbraco.Core.Models
{
///
/// Represents a Language.
///
[Serializable]
[DataContract(IsReference = true)]
public class Language : EntityBase, ILanguage
{
private static readonly Lazy Ps = new Lazy();
private string _isoCode;
private string _cultureName;
private bool _isDefaultVariantLanguage;
private bool _mandatory;
public Language(string isoCode)
{
IsoCode = isoCode;
}
// ReSharper disable once ClassNeverInstantiated.Local
private class PropertySelectors
{
public readonly PropertyInfo IsoCodeSelector = ExpressionHelper.GetPropertyInfo(x => x.IsoCode);
public readonly PropertyInfo CultureNameSelector = ExpressionHelper.GetPropertyInfo(x => x.CultureName);
public readonly PropertyInfo IsDefaultVariantLanguageSelector = ExpressionHelper.GetPropertyInfo(x => x.IsDefaultVariantLanguage);
public readonly PropertyInfo MandatorySelector = ExpressionHelper.GetPropertyInfo(x => x.Mandatory);
}
///
/// Gets or sets the Iso Code for the Language
///
[DataMember]
public string IsoCode
{
get => _isoCode;
set => SetPropertyValueAndDetectChanges(value, ref _isoCode, Ps.Value.IsoCodeSelector);
}
///
/// Gets or sets the Culture Name for the Language
///
[DataMember]
public string CultureName
{
get => _cultureName ?? CultureInfo.GetCultureInfo(IsoCode).DisplayName;
set => SetPropertyValueAndDetectChanges(value, ref _cultureName, Ps.Value.CultureNameSelector);
}
///
/// Returns a object for the current Language
///
[IgnoreDataMember]
public CultureInfo CultureInfo => CultureInfo.GetCultureInfo(IsoCode);
public bool IsDefaultVariantLanguage
{
get => _isDefaultVariantLanguage;
set => SetPropertyValueAndDetectChanges(value, ref _isDefaultVariantLanguage, Ps.Value.IsDefaultVariantLanguageSelector);
}
public bool Mandatory
{
get => _mandatory;
set => SetPropertyValueAndDetectChanges(value, ref _mandatory, Ps.Value.MandatorySelector);
}
}
}