#region License // Copyright (c) Jeremy Skinner (http://www.jeremyskinner.co.uk) // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // The latest version of this file can be found at http://www.codeplex.com/FluentValidation #endregion namespace ServiceStack.FluentValidation.Resources { using System; using System.Linq.Expressions; using System.Reflection; using Internal; /// /// Represents a localized string. /// public class LocalizedStringSource : IStringSource { readonly Func accessor; readonly Type resourceType; readonly string resourceName; /// /// Creates a new instance of the LocalizedErrorMessageSource class using the specified resource name and resource type. /// /// The resource type /// The resource name /// Strategy used to construct the resource accessor public LocalizedStringSource(Type resourceType, string resourceName, IResourceAccessorBuilder resourceAccessorBuilder) { this.resourceType = resourceType; this.resourceName = resourceName; this.accessor = resourceAccessorBuilder.GetResourceAccessor(resourceType, resourceName); } /// /// Creates an IErrorMessageSource from an expression: () => MyResources.SomeResourceName /// /// The expression /// Strategy used to construct the resource accessor /// Error message source public static IStringSource CreateFromExpression(Expression> expression, IResourceAccessorBuilder resourceProviderSelectionStrategy) { var constant = expression.Body as ConstantExpression; if (constant != null) { return new StaticStringSource((string)constant.Value); } var member = expression.GetMember(); if (member == null) { throw new InvalidOperationException("Only MemberExpressions an be passed to BuildResourceAccessor, eg () => Messages.MyResource"); } var resourceType = member.DeclaringType; var resourceName = member.Name; return new LocalizedStringSource(resourceType, resourceName, resourceProviderSelectionStrategy); } /// /// Construct the error message template /// /// Error message template public string GetString() { return accessor(); } /// /// The name of the resource if localized. /// public string ResourceName { get { return resourceName; } } /// /// The type of the resource provider if localized. /// public Type ResourceType { get { return resourceType; } } } }