Translating Names in IDisplayResolution

Marija Jemuovic
Member since: 2010
 

How do I get the current culture in edit mode to use it to translate the Name property of IDisplayResolution?

#65645
Feb 04, 2013 16:59
Marija Jemuovic
Member since: 2010
 

Anyone knows?

#66250
Feb 25, 2013 17:11
Linus Ekström
Member since: 2002
 

If you want to get a translated name you don't have to do this in code. Just add a section in the language files. Here is an extract from the sample templates:

<resolutions>
<standard>Standard (1366x768)</standard>
<ipadhorizontal>iPad horizontal (1024x768)</ipadhorizontal>
<iphonevertical>iPhone vertical (320x480)</iphonevertical>
<androidvertical>Android vertical (480x800)</androidvertical>
</resolutions>

#66261
Feb 26, 2013 7:10
Marija Jemuovic
Member since: 2010
 

I have now investigated this a bit more to figure out what exactly happens.

I have taken the code from Alloy Tech templates. These are the classes: DisplayResolutionBase, DisplayResolutions, MobileChannel and WebChannel.

My Channels.xml looks like this:

<languages>
  <language name="Svenska" id="sv">
    <channels>
      <mobile>Mobil</mobile>
      <web>Webb</web>
    </channels>
    <resolutions>
      <standard>Standard (1366x768)</standard>
      <ipadhorizontal>iPad stående (1024x768)</ipadhorizontal>
      <iphonevertical>iPhone liggande (320x480)</iphonevertical>
      <androidvertical>Android liggande (480x800)</androidvertical>
    </resolutions>
  </language>
  <language name="English" id="en">
    <channels>
      <mobile>Mobile</mobile>
      <web>Web</web>
    </channels>
    <resolutions>
      <standard>Standard (1366x768)</standard>
      <ipadhorizontal>iPad horizontal (1024x768)</ipadhorizontal>
      <iphonevertical>iPhone vertical (320x480)</iphonevertical>
      <androidvertical>Android vertical (480x800)</androidvertical>
    </resolutions>
  </language>
</languages>

When I debug this, the debugger hits the constructor only on application start, when I change the language nothing really happens, the translations are still in English.

#66425
Feb 28, 2013 12:14
Linus Ekström
Member since: 2002
 

Hi Marija!

Thanks for the report. I have recreated the problem and created the following bug to handle it:

Bug #97165: Translation for display resolutions does not work
 
I think that a work around should be to make the "Name"-property dynamic and do the translation on demand rather than in the constructor.
#68259
Mar 19, 2013 15:00
Linus Ekström
Member since: 2002
 

Here is an updated DisplayResolutionBase class that fixes the issue:

using EPiServer.Framework.Localization;
using EPiServer.ServiceLocation;
using EPiServer.Web;

namespace EPiServer.Templates.Alloy.Business.Channels
{
    public abstract class DisplayResolutionBase : IDisplayResolution
    {
        private static readonly LocalizationService _localizationService = ServiceLocator.Current.GetInstance<LocalizationService>();
        private string _internalName;

        /// <summary>
        /// Initializes a new instance of the <see cref="DisplayResolutionBase"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="width">The width in pixels.</param>
        /// <param name="height">The height in pixels.</param>
        protected DisplayResolutionBase(string name, int width, int height)
        {
            Id = this.GetType().FullName;
            _internalName = name;
            Width = width;
            Height = height;
        }

        /// <summary>
        /// Gets the unique id for this resolution
        /// </summary>
        public string Id { get; protected set; }

        /// <summary>
        /// Gets the name of resolution.
        /// </summary>
        public string Name
        {
            get
            {
                return Translate(_internalName);
            }
        }

        /// <summary>
        /// Gets the resolution width in pixels.
        /// </summary>
        public int Width { get; protected set; }

        /// <summary>
        /// Gets the resolution height in pixels.
        /// </summary>
        public int Height { get; protected set; }

        private static string Translate(string resurceKey)
        {
            string value = null;

            if (!_localizationService.TryGetString(resurceKey, out value))
            {
                value = resurceKey;
            }

            return value;
        }
    }
}

#68307
Mar 19, 2013 16:46
Marija Jemuovic
Member since: 2010
 

Exactly the same here, except I named my _name instead of _internalName :DDD

#68309
Mar 19, 2013 16:56
Linus Ekström
Member since: 2002
 

Haha. I just checked in a bug fixed and renamed the private field to _nameOrResourceKey ;)

#68310
Mar 19, 2013 16:57
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.