コード例 #1
0
        /// <summary>
        /// 通过资源文件获取 ErrorMessage 方法
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        protected virtual string?GetLocalizerErrorMessage(ValidationContext context)
        {
            var errorMesssage = ErrorMessage;

            if (!string.IsNullOrEmpty(context.MemberName) && !string.IsNullOrEmpty(errorMesssage))
            {
                // 查找 resx 资源文件中的 ErrorMessage
                var memberName = context.MemberName;

                var isResx   = false;
                var resxType = ServiceProviderHelper.ServiceProvider.GetRequiredService <IOptions <JsonLocalizationOptions> >().Value.ResourceManagerStringLocalizerType;
                if (resxType != null && JsonStringLocalizerFactory.TryGetLocalizerString(resxType, errorMesssage, out var resx))
                {
                    errorMesssage = resx;
                    isResx        = true;
                }

                if (!isResx && JsonStringLocalizerFactory.TryGetLocalizerString(context.ObjectType, $"{memberName}.{GetRuleKey()}", out var msg))
                {
                    errorMesssage = msg;
                }

                if (!string.IsNullOrEmpty(errorMesssage))
                {
                    var displayName = new FieldIdentifier(context.ObjectInstance, context.MemberName).GetDisplayName();
                    errorMesssage = string.Format(CultureInfo.CurrentCulture, errorMesssage, displayName ?? memberName);
                }
            }
            return(errorMesssage);
        }
コード例 #2
0
        /// <summary>
        /// 通过资源文件获取 ErrorMessage 方法
        /// </summary>
        /// <param name="context"></param>
        /// <param name="localizerFactory"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        protected virtual string?GetLocalizerErrorMessage(ValidationContext context, IStringLocalizerFactory?localizerFactory = null, JsonLocalizationOptions?options = null)
        {
            var errorMesssage = ErrorMessage;

            if (!string.IsNullOrEmpty(context.MemberName) && !string.IsNullOrEmpty(errorMesssage))
            {
                // 查找 resx 资源文件中的 ErrorMessage
                var memberName = context.MemberName;

                if (localizerFactory != null)
                {
                    // 查找微软格式 resx 格式资源文件
                    var isResx = false;
                    if (options != null && options.ResourceManagerStringLocalizerType != null)
                    {
                        var localizer = localizerFactory.Create(options.ResourceManagerStringLocalizerType);
                        if (JsonStringLocalizerFactory.TryGetLocalizerString(localizer, errorMesssage, out var resx))
                        {
                            errorMesssage = resx;
                            isResx        = true;
                        }
                    }

                    // 查找 json 格式资源文件
                    if (!isResx && JsonStringLocalizerFactory.TryGetLocalizerString(localizerFactory.Create(context.ObjectType), $"{memberName}.{GetRuleKey()}", out var msg))
                    {
                        errorMesssage = msg;
                    }
                }

                if (!string.IsNullOrEmpty(errorMesssage))
                {
                    var displayName = new FieldIdentifier(context.ObjectInstance, context.MemberName).GetDisplayName();
                    errorMesssage = string.Format(CultureInfo.CurrentCulture, errorMesssage, displayName ?? memberName);
                }
            }
            return(errorMesssage);
        }