<commit_before>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LocalizationExtensions.cs" company="Quartz Software SRL">
//   Copyright (c) Quartz Software SRL. All rights reserved.
// </copyright>
// <summary>
//   Implements the localization extensions class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Kephas.Reflection
{
    using System;
    using System.Reflection;

    using Kephas.Diagnostics.Contracts;
    using Kephas.Reflection.Localization;

    /// <summary>
    /// Localization extensions for reflection.
    /// </summary>
    public static class LocalizationExtensions
    {
        /// <summary>
        /// Gets the dynamic localization property name.
        /// </summary>
        /// <value>
        /// The dynamic localization property name.
        /// </value>
        private const string LocalizationPropertyName = "Kephas-Localization";

        /// <summary>
        /// Gets the localization for a <see cref="Type"/>.
        /// </summary>
        /// <param name="type">The type to act on.</param>
        /// <returns>
        /// The type localization.
        /// </returns>
        public static ITypeInfoLocalization GetLocalization(this Type type)
        {
            Requires.NotNull(type, nameof(type));

            return GetLocalization(type.AsRuntimeTypeInfo());
        }

        /// <summary>
        /// Gets the localization for a <see cref="TypeInfo"/>.
        /// </summary>
        /// <param name="typeInfo">The type information to act on.</param>
        /// <returns>
        /// The type localization.
        /// </returns>
        public static ITypeInfoLocalization GetLocalization(this TypeInfo typeInfo)
        {
            Requires.NotNull(typeInfo, nameof(typeInfo));

            return GetLocalization(typeInfo.AsRuntimeTypeInfo());
        }

        /// <summary>
        /// Gets the localization for a <see cref="ITypeInfo"/>.
        /// </summary>
        /// <param name="typeInfo">The type information to act on.</param>
        /// <returns>
        /// The type localization.
        /// </returns>
        public static ITypeInfoLocalization GetLocalization(this ITypeInfo typeInfo)
        {
            Requires.NotNull(typeInfo, nameof(typeInfo));

            var localization = typeInfo[LocalizationPropertyName] as ITypeInfoLocalization;
            if (localization == null && !typeInfo.HasMember(LocalizationPropertyName))
            {
                localization = new TypeInfoLocalization(typeInfo);
                typeInfo[LocalizationPropertyName] = localization;
            }

            return localization;
        }

        /// <summary>
        /// Gets the localization for a <see cref="PropertyInfo"/>.
        /// </summary>
        /// <param name="propertyInfo">The property information to act on.</param>
        /// <returns>
        /// The property localization.
        /// </returns>
        public static IMemberInfoLocalization GetLocalization(this PropertyInfo propertyInfo)<commit_msg>Extended the LocalizationExtensions to be able to provide a constructor for TypeInfoLocalizations.<commit_after>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LocalizationExtensions.cs" company="Quartz Software SRL">
//   Copyright (c) Quartz Software SRL. All rights reserved.
// </copyright>
// <summary>
//   Implements the localization extensions class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Kephas.Reflection
{
    using System;
    using System.Reflection;

    using Kephas.Diagnostics.Contracts;
    using Kephas.Reflection.Localization;

    /// <summary>
    /// Localization extensions for reflection.
    /// </summary>
    public static class LocalizationExtensions
    {
        /// <summary>
        /// Gets the dynamic localization property name.
        /// </summary>
        /// <value>
        /// The dynamic localization property name.
        /// </value>
        private const string LocalizationPropertyName = "Kephas-Localization";

        /// <summary>
        /// The the function for creating the type info localization.
        /// </summary>
        private static Func<ITypeInfo, ITypeInfoLocalization> createTypeInfoLocalizationFunc = ti => new TypeInfoLocalization(ti);

        /// <summary>
        /// Gets or sets the function for creating the type info localization.
        /// </summary>
        /// <value>
        /// The the function for creating the type info localization.
        /// </value>
        public static Func<ITypeInfo, ITypeInfoLocalization> CreateTypeInfoLocalization
        {
            get => createTypeInfoLocalizationFunc;
            set
            {
                Requires.NotNull(value, nameof(value));
                createTypeInfoLocalizationFunc = value;
            }
        }

        /// <summary>
        /// Gets the localization for a <see cref="Type"/>.
        /// </summary>
        /// <param name="type">The type to act on.</param>
        /// <returns>
        /// The type localization.
        /// </returns>
        public static ITypeInfoLocalization GetLocalization(this Type type)
        {
            Requires.NotNull(type, nameof(type));

            return GetLocalization(type.AsRuntimeTypeInfo());
        }

        /// <summary>
        /// Gets the localization for a <see cref="TypeInfo"/>.
        /// </summary>
        /// <param name="typeInfo">The type information to act on.</param>
        /// <returns>
        /// The type localization.
        /// </returns>
        public static ITypeInfoLocalization GetLocalization(this TypeInfo typeInfo)
        {
            Requires.NotNull(typeInfo, nameof(typeInfo));

            return GetLocalization(typeInfo.AsRuntimeTypeInfo());
        }

        /// <summary>
        /// Gets the localization for a <see cref="ITypeInfo"/>.
        /// </summary>
        /// <param name="typeInfo">The type information to act on.</param>
        /// <returns>
        /// The type localization.
        /// </returns>
        public static ITypeInfoLocalization GetLocalization(this ITypeInfo typeInfo)
        {
            Requires.NotNull(typeInfo, nameof(typeInfo));

            var localization = typeInfo[LocalizationPropertyName] as ITypeInfoLocalization;
            if (localization == null && !typeInfo.HasMember(LocalizationPropertyName))
            {
                localization = CreateTypeInfoLocalization(typeInfo) ?? new TypeInfoLocalization(typeInfo);
                typeInfo[LocalizationPropertyName] = localization;
            }

            return localization;
        }

        /// <summary>
        /// Gets the localization for a <see cref="PropertyInfo"/>.
        /// </summary>
        /// <param name="propertyInfo">The property information to act on.</param>
        /// <returns>
        /// The property localization.
        /// </returns>
        public static IMemberInfoLocalization GetLocalization(this PropertyInfo propertyInfo)
{'fraction_non_alphanumeric': 0.1744233656339566, 'fraction_numerical': 0.0, 'mean_word_length': 1.6464427591188155, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2140973', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1661, 'n_words': 456}