Esempio n. 1
0
        /// <summary>
        /// Gets a translated error message for the given controller action.
        /// </summary>
        /// <param name="controllerAction">The controller action that originated the error.</param>
        /// <param name="errorCode">The code of the error to translate.</param>
        /// <returns>A localized string.</returns>
        public LocalizedString GetLocalizedErrorMessage(ControllerActionDescriptor controllerAction, string errorCode)
        {
            LocalizedString errorMessage;
            // build a key for the resource.
            ResourceKey resourceKey = _resourceMapper.GetKey(controllerAction.ControllerTypeInfo.Namespace,
                                                             controllerAction.ControllerTypeInfo.Name,
                                                             controllerAction.ActionName,
                                                             errorCode);

            if (resourceKey != null)
            {
                // if the key was built, proceed to translation.
                IStringLocalizer stringLocalizer = _stringLocalizerFactory.Create(resourceKey.ResourceName, resourceKey.ResourceLocation);
                errorMessage = stringLocalizer.GetString(resourceKey.KeyName, _resourceMapper.GetSupportInfoArguments());
                if (errorMessage.ResourceNotFound)
                {
                    errorMessage = GetFallbackErrorMessage(errorCode);
                }
            }
            else
            {
                // the key was not built, get the default error for the message.
                errorMessage = GetFallbackErrorMessage(errorCode);
            }

            return(errorMessage);
        }