-
-
Save JimBobSquarePants/80762a1233bbb112689e to your computer and use it in GitHub Desktop.
Vorto + Ditto + MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Globalization; | |
using System.Threading; | |
using System.Web; | |
using System.Web.Mvc; | |
/// <summary> | |
/// Applies localized content from the CMS for the current <see cref="ActionResult"/> | |
/// </summary> | |
public class LocalizedAttribute : ActionFilterAttribute | |
{ | |
/// <summary> | |
/// Called by the ASP.NET MVC framework before the action method executes. | |
/// </summary> | |
/// <param name="filterContext">The filter context.</param> | |
public override void OnActionExecuting(ActionExecutingContext filterContext) | |
{ | |
HttpCookie languageCookie = filterContext.HttpContext.Request.Cookies["vorto.language"]; | |
if (languageCookie != null) | |
{ | |
switch (languageCookie.Value) | |
{ | |
case "zh-Hans": | |
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("zh-Hans"); | |
break; | |
case "ja-JP": | |
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("ja-JP"); | |
break; | |
case "ko-KR": | |
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("ko-KR"); | |
break; | |
default: | |
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en-AU"); | |
break; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Our.Umbraco.Ditto; | |
/// <summary> | |
/// The vorto property attribute. | |
/// Used for returning multilingual properties from Umbraco via the Vorto plugin. | |
/// </summary> | |
public class VortoPropertyAttribute : DittoValueResolverAttribute | |
{ | |
/// <summary> | |
/// Initializes a new instance of the <see cref="VortoPropertyAttribute"/> class. | |
/// </summary> | |
public VortoPropertyAttribute() | |
: base(typeof(VortoValueResolver)) | |
{ | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="VortoPropertyAttribute"/> class. | |
/// </summary> | |
/// <param name="recursive">Whether the property should be retrieved recursively up the tree.</param> | |
public VortoPropertyAttribute(bool recursive = false) | |
: base(typeof(VortoValueResolver)) | |
{ | |
this.Recursive = recursive; | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="VortoPropertyAttribute"/> class. | |
/// </summary> | |
/// <param name="propertyName">The property name.</param> | |
/// <param name="recursive">Whether the property should be retrieved recursively up the tree.</param> | |
public VortoPropertyAttribute(string propertyName, bool recursive = false) | |
: base(typeof(VortoValueResolver)) | |
{ | |
this.PropertyName = propertyName; | |
this.Recursive = recursive; | |
} | |
/// <summary> | |
/// Gets or sets the property name. | |
/// </summary> | |
public string PropertyName { get; set; } | |
/// <summary> | |
/// Gets or sets a value indicating whether the property should be retrieved recursively up the tree. | |
/// </summary> | |
public bool Recursive { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Our.Umbraco.Ditto; | |
using Our.Umbraco.Vorto.Extensions; | |
using Umbraco.Core.Models; | |
/// <summary> | |
/// The Vorto value resolver. | |
/// </summary> | |
public class VortoValueResolver : DittoValueResolver<DittoValueResolverContext, VortoPropertyAttribute> | |
{ | |
/// <summary> | |
/// Default back to the default culture; | |
/// </summary> | |
private static string fallbackCultureName = "en-AU"; | |
/// <summary> | |
/// Gets the raw value for the current property from Umbraco. | |
/// </summary> | |
/// <returns> | |
/// The <see cref="object"/> representing the raw value. | |
/// </returns> | |
public override object ResolveValue() | |
{ | |
string umbracoPropertyName = this.Attribute.PropertyName; | |
bool recursive = this.Attribute.Recursive; | |
if (this.Context.PropertyDescriptor != null) | |
{ | |
if (string.IsNullOrWhiteSpace(umbracoPropertyName)) | |
{ | |
umbracoPropertyName = this.Context.PropertyDescriptor.Name; | |
} | |
} | |
IPublishedContent content = this.Context.Instance as IPublishedContent; | |
return content.GetVortoValue(umbracoPropertyName, null, recursive, null, fallbackCultureName); | |
} | |
} |
Just spotted your comment. Yeah, it was just for known cultures.
Need to update this some time to use the current Ditto codebase.
@JimBobSquarePants I would love to use this with the current Ditto codebase! I'm going to see if I can figure it out myself, but I might end up with questions for you, haha.
This is what I ended up doing, which worked :)
using Our.Umbraco.Ditto;
using Our.Umbraco.Vorto.Extensions;
using Umbraco.Core.Models;
namespace Offroadcode.Web.Attributes
{
/// <summary>
/// The vorto property attribute.
/// Used for returning multilingual properties from Umbraco via the Vorto plugin.
/// </summary>
public class VortoPropertyAttribute : DittoProcessorAttribute
{
/// <summary>
/// Default back to the default culture;
/// </summary>
private static string fallbackCultureName = "en-GB";
/// <summary>
/// Initializes a new instance of the <see cref="VortoPropertyAttribute"/> class.
/// </summary>
public VortoPropertyAttribute()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="VortoPropertyAttribute"/> class.
/// </summary>
/// <param name="recursive">Whether the property should be retrieved recursively up the tree.</param>
public VortoPropertyAttribute(bool recursive = false)
{
this.Recursive = recursive;
}
/// <summary>
/// Initializes a new instance of the <see cref="VortoPropertyAttribute"/> class.
/// </summary>
/// <param name="propertyName">The property name.</param>
/// <param name="recursive">Whether the property should be retrieved recursively up the tree.</param>
public VortoPropertyAttribute(string propertyName, bool recursive = false)
{
this.PropertyName = propertyName;
this.Recursive = recursive;
}
/// <summary>
/// Gets or sets the property name.
/// </summary>
public string PropertyName { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the property should be retrieved recursively up the tree.
/// </summary>
public bool Recursive { get; set; }
public override object ProcessValue()
{
string umbracoPropertyName = PropertyName;
bool recursive = Recursive;
if (Context.PropertyDescriptor != null)
{
if (string.IsNullOrWhiteSpace(umbracoPropertyName))
{
umbracoPropertyName = Context.PropertyDescriptor.Name;
}
}
return Context.Content.GetVortoValue(umbracoPropertyName, null, recursive, null, fallbackCultureName);
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any particular reason to do a switch statement in the LocalizedAttribute rather than just parse the cookie value as a Culture? (other than ensuring it's a known supported culture I guess)