Tuesday, May 12, 2015

Localize Property Name Based on Content Language

We recently had a client request that a page property have a different display name for each language branch.  The function of the property was as an identifier and would always be used the same in the back end but the editors in each country had a different name for the property.

This isn't a particularly inspired solution, but I'm hoping to save someone else the time it took to stumble upon this.

It is possible to localize EPi property names using the built in Display attribute and keeping the translations in resources files (see this stackoverflow post here), but this is only based on the UI language in Edit/Admin mode and not on content language.

I created an EditorDescriptor that will take the string entered in the Name field and try to translate it using the registered LocalizationService (whatever that may be).  An important thing to note is the EditorDescriptorBehavior.  We have to have it as EditorDescriptorBehavior.PlaceLast to ensure that any other logic to get the Display Name (built in translation, etc) does not interfere with what we want displayed.

Code


[EditorDescriptorRegistration(TargetType = typeof(String), UIHint = SiteUIHints.LocalizedDisplay, EditorDescriptorBehavior = EditorDescriptorBehavior.PlaceLast)]
public class LocalizableDisplayEditorDescriptor : EditorDescriptor
{
   public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
   {
      base.ModifyMetadata(metadata, attributes);

      Attribute attr = attributes.FirstOrDefault(a => a.GetType() == typeof(DisplayAttribute));
      if (attr != null)
      {
         string translated =                                     

             ServiceLocator.Current.GetInstance<LocalizationService().GetStringByCulture(metadata.DisplayName, 
             FallbackBehaviors.None, ContentLanguage.PreferredCulture);

         if (!string.IsNullOrWhiteSpace(translated))
            metadata.DisplayName = translated;
      }
   }
}



Usage

[UIHint(SiteUIHints.LocalizedDisplay)]
[Display(Name = "/properties/InternalId/displayname", Order = 200)]
public virtual string InternalId { get; set; }

17 comments :

  1. Howdy. Very nice web site!! Guy .. Excellent .. Superb .. I will bookmark this site and take the feeds additionally…I am glad to find a lot of helpful info here within the article. Thank you for sharing… Boulevard 88 Price

    ReplyDelete
  2. Great Article it its really informative and innovative keep us posted with new updates. its was really valuable. thanks a lot. villa for sale Nagueles

    ReplyDelete
  3. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Riviere Jiak Kim Street

    ReplyDelete
  4. Mmm.. good to be here in your article or post, whatever, I think I should also work hard for my own website like I see some good and updated working in your site. House blog

    ReplyDelete
  5. You have done a great job. I will definitely dig it and personally recommend to my friends. I am confident they will be benefited from this site. General Contractor Maryland

    ReplyDelete
  6. I got what you mean, thanksfor posting .Woh I am happyto find this website through google. Las Colinas Properties For Sale

    ReplyDelete
  7. Hiya, I am really glad I have found this information. Today bloggers publish just about gossips and web and this is really annoying. A good web site with interesting content, that’s what I need. Thanks for keeping this site, I’ll be visiting it. Do you do newsletters? Can’t find it. apartemen jakarta selatan

    ReplyDelete
  8. Thank you for taking the time to publish this information very useful! Kopar at Newton

    ReplyDelete
  9. The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention. Assertiveness Training Brisbane

    ReplyDelete
  10. I was just browsing through the internet looking for some information and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject. Bookmarked this page, will come back for more. Costa Del Sol Property For Sale

    ReplyDelete
  11. Algarve Property For Sale When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your.

    ReplyDelete
  12. nature is definately not expensive. go on a hike with friends, or just explore a forest. there are always creeks to walk thru and all kinds of colorful plants and flowers. even frisbee golf is fun, free, and good exercise best real estate agency manila

    ReplyDelete
  13. Excellent! I thank you your contribution to this matter. It has been insightful. warehouse to let

    ReplyDelete
  14. On the off chance that you see this documentation and consider in the evaluating cycle for the property, it is insightful to consider the genuine freedom of the valuation when it was done and its significance to the current market. https://yes-mallorca-property.com

    ReplyDelete
  15. Is the property in good condition and is it in a good location? These should be crucial considerations, as you will either want to let the property, or sell it to another property investor or home-buyer. property wealth advisor

    ReplyDelete
  16. Our profit for this speculation will be driven by two essential factors: our rental salary and the energy about the property estimation. Along these lines, we should start by determining rental salary and the valuation for the property in thought. https://yes-mallorca-property.com/information/faq/general-issues/

    ReplyDelete
  17. A Property Advisor works for a business building administration organization to oversee and give office upkeep and fixes, parking garage fixes and clearing administrations, pressure washing and hydro-cleaning, mass garbage evacuation and business painting while keeping the you, the customer, exceptional through the whole cycle and guaranteeing fulfillment investment property advice

    ReplyDelete