Created
October 21, 2016 16:30
-
-
Save jamiepollock/fb5e20a69a42a6a39270fb3f8b018ba9 to your computer and use it in GitHub Desktop.
Working with EditorModelEventManager to localise an EditorContentModel powered by /config/lang/*.user.xml files
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
<?xml version="1.0" encoding="utf-8" standalone="yes"?> | |
<language> | |
<area alias="tabs"> | |
<key alias="My Custom Tab">My Custom Tab localized in French!</key> | |
</area> | |
<area alias="properties"> | |
<key alias="myCustomProperty">My Custom Property localized in French!</key> | |
</area> | |
<area alias="propertyDescriptions"> | |
<key alias="myCustomProperty">My Custom Property description localized in French!</key> | |
</area> | |
</language> |
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.Collections.Generic; | |
using System.Globalization; | |
using Umbraco.Core; | |
using Umbraco.Core.Services; | |
using Umbraco.Web; | |
using Umbraco.Web.Editors; | |
using Umbraco.Web.Models.ContentEditing; | |
namespace MyWebsite | |
{ | |
public class LocalizedEditorModelEventManagerEventHandler : ApplicationEventHandler | |
{ | |
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) | |
{ | |
EditorModelEventManager.SendingContentModel += (sender, args) => | |
{ | |
var cultureCode = UmbracoContext.Current.Security.CurrentUser.Language; | |
var culture = CultureInfo.GetCultureInfo(cultureCode); | |
TranslateTabs(args.Model.Tabs, culture, applicationContext.Services.TextService); | |
TranslateProperties(args.Model.Properties, culture, applicationContext.Services.TextService); | |
}; | |
} | |
private static void TranslateProperties(IEnumerable<ContentPropertyDisplay> properties, CultureInfo culture, ILocalizedTextService textService) | |
{ | |
foreach (var property in properties) | |
{ | |
if (property.HideLabel == false) | |
{ | |
var localizedLabelValue = textService.Localize("properties/" + property.Alias, culture); | |
if (string.IsNullOrWhiteSpace(localizedLabelValue) == false && | |
IsDefaultLocalizedValue(localizedLabelValue, property.Alias) == false) | |
{ | |
property.Label = localizedLabelValue; | |
} | |
var localizedDescriptionValue = textService.Localize("propertyDescriptions/" + property.Alias, | |
culture); | |
if (string.IsNullOrWhiteSpace(localizedDescriptionValue) == false && | |
IsDefaultLocalizedValue(localizedDescriptionValue, property.Alias) == false) | |
{ | |
property.Description = localizedDescriptionValue; | |
} | |
} | |
} | |
} | |
private static void TranslateTabs(IEnumerable<Tab<ContentPropertyDisplay>> tabs, CultureInfo culture, ILocalizedTextService textService) | |
{ | |
foreach (var tab in tabs) | |
{ | |
var localizedValue = textService.Localize("tabs/" + tab.Alias, culture); | |
if (string.IsNullOrWhiteSpace(localizedValue) == false && IsDefaultLocalizedValue(localizedValue, tab.Alias) == false) | |
{ | |
tab.Label = localizedValue; | |
} | |
} | |
} | |
private static bool IsDefaultLocalizedValue(string value, string key) | |
{ | |
return string.Equals(value, "[" + key + "]"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment