コード例 #1
0
        public static MvcHtmlString TranslateByCulture(this HtmlHelper helper, Expression <Func <object> > model, Type customAttribute, CultureInfo culture, params object[] formatArguments)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (customAttribute == null)
            {
                throw new ArgumentNullException(nameof(customAttribute));
            }

            if (culture == null)
            {
                throw new ArgumentNullException(nameof(culture));
            }

            if (!typeof(Attribute).IsAssignableFrom(customAttribute))
            {
                throw new ArgumentException($"Given type `{customAttribute.FullName}` is not of type `System.Attribute`");
            }

            var resourceKey = ResourceKeyBuilder.BuildResourceKey(ExpressionHelper.GetFullMemberName(model), customAttribute);

            return(new MvcHtmlString(LocalizationProvider.Current.GetStringByCulture(resourceKey, culture, formatArguments)));
        }
コード例 #2
0
 /// <summary>
 /// Creates new localization provider with all the required settings and services injected.
 /// </summary>
 /// <param name="keyBuilder">Key builder (with help of <paramref name="expressionHelper"/> this dependency will help to translate from lambda expression to resource key as string).</param>
 /// <param name="expressionHelper">Can walk lambda expressions and return string representation of the expression.</param>
 /// <param name="fallbackCollection">Collection of fallback language settings.</param>
 /// <param name="queryExecutor">Small utility robot to help with queries.</param>
 public LocalizationProvider(
     ResourceKeyBuilder keyBuilder,
     ExpressionHelper expressionHelper,
     FallbackLanguagesCollection fallbackCollection,
     IQueryExecutor queryExecutor)
 {
     _keyBuilder         = keyBuilder;
     _expressionHelper   = expressionHelper;
     _fallbackCollection = fallbackCollection;
     _queryExecutor      = queryExecutor;
 }
コード例 #3
0
        /// <summary>
        /// Translates the specified enum with some formatting arguments (if needed).
        /// </summary>
        /// <param name="target">The enum to translate.</param>
        /// <param name="culture">The culture.</param>
        /// <param name="formatArguments">The format arguments.</param>
        /// <returns>Translated enum values</returns>
        /// <exception cref="ArgumentNullException">
        /// target
        /// or
        /// culture
        /// </exception>
        public static string TranslateByCulture(this Enum target, CultureInfo culture, params object[] formatArguments)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (culture == null)
            {
                throw new ArgumentNullException(nameof(culture));
            }

            var resourceKey = ResourceKeyBuilder.BuildResourceKey(target.GetType(), target.ToString());

            return(LocalizationProvider.Current.GetStringByCulture(resourceKey, culture, formatArguments));
        }
コード例 #4
0
        public static MvcHtmlString TranslateForByCulture <TModel, TValue>(this HtmlHelper <TModel> html,
                                                                           Expression <Func <TModel, TValue> > expression,
                                                                           Type customAttribute,
                                                                           CultureInfo culture,
                                                                           params object[] formatArguments)
        {
            if (customAttribute == null)
            {
                throw new ArgumentNullException(nameof(customAttribute));
            }

            if (culture == null)
            {
                throw new ArgumentNullException(nameof(culture));
            }

            if (!typeof(Attribute).IsAssignableFrom(customAttribute))
            {
                throw new ArgumentException($"Given type `{customAttribute.FullName}` is not of type `System.Attribute`");
            }

            var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);

            var pi = metadata.ContainerType.GetProperty(metadata.PropertyName);

            if (pi != null)
            {
                if (pi.GetCustomAttribute(customAttribute) == null)
                {
                    return(MvcHtmlString.Empty);
                }
            }

            return(new MvcHtmlString(LocalizationProvider.Current.GetStringByCulture(ResourceKeyBuilder.BuildResourceKey(metadata.ContainerType,
                                                                                                                         metadata.PropertyName,
                                                                                                                         customAttribute),
                                                                                     culture,
                                                                                     formatArguments)));
        }
コード例 #5
0
        internal static string GetFullMemberName(Expression <Func <object> > memberSelector)
        {
            var memberStack = WalkExpression(memberSelector);

            return(ResourceKeyBuilder.BuildResourceKey(memberStack.Pop(), memberStack));
        }