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>
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.
Hi Marija!
Thanks for the report. I have recreated the problem and created the following bug to handle it:
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;
}
}
}
Exactly the same here, except I named my _name instead of _internalName :DDD
Haha. I just checked in a bug fixed and renamed the private field to _nameOrResourceKey ;)
How do I get the current culture in edit mode to use it to translate the Name property of IDisplayResolution?