Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Johan Reitsma 48 posts 193 karma points
    Jun 19, 2018 @ 06:45
    Johan Reitsma
    0

    NestedContent and multilanguage backend

    I'm currently using umbraco 7.10 and nested content but if I have items starting with #RelatedFiles as optional document type in Nested Content #RelatedFiles is not translated automatically.

    Does NestedContent support multilanguage for DocumentTypes?

  • Dave Woestenborghs 3387 posts 11466 karma points MVP 6x admin c-trib
    Jun 19, 2018 @ 08:21
    Dave Woestenborghs
    0

    Hi Johan,

    I guess you use dictionary items for your document type names and property names ?

    Dave

  • Dave Woestenborghs 3387 posts 11466 karma points MVP 6x admin c-trib
    Jun 19, 2018 @ 11:10
    Dave Woestenborghs
    100

    Hi Johan,

    I assume you are using dictionary items. We solved this using a webapi handler to intercept the response of the api call and change the doctype name.

    WebApiHandler code :

    namespace MyApp.WebApiHandlers
    {
        using System;
        using System.Globalization;
        using System.Linq;
        using System.Net.Http;    
        using System.Threading.Tasks;
        using System.Web;
    
        using Umbraco.Core;
        using Umbraco.Core.Dictionary;
        using Umbraco.Core.Logging;
        using Umbraco.Web;
        using Umbraco.Web.Models.ContentEditing;
    
        internal class ContentGetEmptyHandler : DelegatingHandler
        {
            protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                switch (request.RequestUri.AbsolutePath.ToLower())
                {
                    case "/umbraco/backoffice/umbracoapi/content/getempty":
                        var userName = HttpContext.Current.User.Identity.Name;
                        return this.SetCorrectDoctypename(request, cancellationToken, userName);
                    default:
                        return base.SendAsync(request, cancellationToken);
                }
            }
    
            private Task<HttpResponseMessage> SetCorrectDoctypename(HttpRequestMessage request, CancellationToken cancellationToken, string userName)
            {
                return base.SendAsync(request, cancellationToken)
                    .ContinueWith(task =>
                            {
                                var response = task.Result;
                                try
                                {
                                    var queryCollection = request.RequestUri.ParseQueryString();
    
                                    if (queryCollection.AllKeys.Contains("parentId") && queryCollection.AllKeys.Contains("contentTypeAlias")
                                        && queryCollection["parentId"] == "-20")
                                    {
                                        // only fiddle with respone when query contains correct parameters and values
                                        // requests from nested content set parent id to -20
    
                                        // Get the content that we want to modify.
                                        var data = response.Content;
                                        var content = ((ObjectContent)data).Value as ContentItemDisplay;
    
                                        if (content != null && content.ContentTypeName.StartsWith("#"))
                                        {
                                            // content type name starts with a # so it's probably a dictionary key
    
                                            // set the culture to the one of the logged in user
                                            var user =
                                                ApplicationContext.Current.Services.UserService.GetByUsername(userName);
    
                                            Thread.CurrentThread.CurrentCulture = new CultureInfo(user.Language);
                                            Thread.CurrentThread.CurrentUICulture = new CultureInfo(user.Language);
    
                                            // we don't have umbraco context so we can't use umbracohelper to get the dictionary items
                                            // just use the internal code used by the helper
                                            var factory = CultureDictionaryFactoryResolver.Current.Factory;
                                            var cultureDictionary = factory.CreateDictionary();
    
                                            var translatedValue = cultureDictionary[content.ContentTypeName.TrimStart('#')];
    
                                            if (!string.IsNullOrEmpty(translatedValue))
                                            {
                                                content.ContentTypeName = translatedValue;
                                            }
                                         }
                                    }                                                             
                                }
                                catch (Exception ex)
                                {
                                    LogHelper.Error<ContentGetEmptyHandler>("Could not change the content type name.", ex);
                                }
                                return response;
                        });
            }
        }
    }
    

    And you need to register the handler to be used :

        public class Bootstrapper : ApplicationEventHandler
        {
    
            protected override void ApplicationStarting(
                UmbracoApplicationBase umbracoApplication,
                ApplicationContext applicationContext)
            {            
                GlobalConfiguration.Configuration.MessageHandlers.Add(new ContentGetEmptyHandler());           
            }
    
    }
    

    Dave

  • Johan Reitsma 48 posts 193 karma points
    Jun 19, 2018 @ 17:16
    Johan Reitsma
    0

    Thanks Dave,

    It works! Now whe can support more languages for our customers!

    Johan

Please Sign in or register to post replies

Write your reply to:

Draft