jgauffin / griffin.mvccontrib

A contribution project for ASP.NET MVC3
http://blog.gauffin.org/tag/griffin-mvccontrib/
GNU Lesser General Public License v3.0
83 stars 40 forks source link

Fallbacks for model properties - ResourceStringProvider #3

Closed shelakel closed 12 years ago

shelakel commented 12 years ago

I want to propose a useful modification to the ResourceStringProvider I'm currently using.

        /// <summary>
        /// Get a localized string for a model property
        /// </summary>
        /// <param name="model">Model being localized</param>
        /// <param name="propertyName">Property to get string for</param>
        /// <returns>Translated string</returns>
        public string GetModelString(Type model, string propertyName)
        {
            var result = GetString(Format(model, propertyName));

            return string.IsNullOrEmpty(result) ? GetString(Format(propertyName)) : result;
        }

        /// <summary>
        /// Get a localized metadata for a model property
        /// </summary>
        /// <param name="model">Model being localized</param>
        /// <param name="propertyName">Property to get string for</param>
        /// <param name="metadataName">Valid names are: Watermark, Description, NullDisplayText, ShortDisplayText.</param>
        /// <returns>Translated string</returns>
        /// <remarks>
        /// Look at <see cref="ModelMetadata"/> to know more about the meta data
        /// </remarks>
        public string GetModelString(Type model, string propertyName, string metadataName)
        {
            var result = GetString(Format(model, propertyName, metadataName));

            return string.IsNullOrEmpty(result) ? GetString(Format(propertyName, metadataName)) : result;
        }

This simply provides a default fallback to retrieve model property display names and metadata. ->MyModel_FullName = Full name if not found, checks for: ->FullName = Full name

This still allows for overriding the defaults which is a win-win situation.

Thanks for your contribution :)

jgauffin commented 12 years ago

->MyModel_FullName = Full name if not found, checks for: ->CommonPrompts_FullName = Full name

The repositories works the same way.