Copied to clipboard

Flag this post as spam?

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


  • Daniel Gustafsson 13 posts 93 karma points
    May 10, 2019 @ 06:15
    Daniel Gustafsson
    0

    UmbracoApiController and language variants

    Hi,

    In a project im working on, we have at the moment 4 language variants with sv-se as the default.

    If im on the english variant of the site and use ajax to get data from the controller i allways get the Swedish version back.

    Is there someway to get the culture specific version of a document with the culture specific properties?

    Right now im using var model = Umbraco.Content(id);.

    Thanks in advance.

  • Kevin Jump 2311 posts 14697 karma points MVP 7x c-trib
    May 10, 2019 @ 07:22
    Kevin Jump
    100

    Hi,

    As you call Umbraco.Content(id) you are actually getting the published content back with all the cultures in it.

    when you call model.Value("someProperty") that will be returning the default (so Swedish in your case) but you can call it with the culture name to get the other language versions.

    e.g

    var myValue = model.value("someProperty", cultureName) 
    

    from an ajax call the controller won't know what language you want so you will need to pass that as part of your method name so a method like this should work* :

    [HttpGet]
    public string GetSomething(int id, string culture)
    {
        var content = Umbraco.Content(id);
        return content.Value<string>("someValue", culture);
    }
    

    *with null checks and all the other stuff

  • Daniel Gustafsson 13 posts 93 karma points
    May 10, 2019 @ 07:37
    Daniel Gustafsson
    0

    Thanks for the reply.

    Is it possible the get the published content back in a specific culture?

  • Kevin Jump 2311 posts 14697 karma points MVP 7x c-trib
    May 10, 2019 @ 08:15
    Kevin Jump
    0

    Hi

    I don't think you can - the PublishedContent item contains all the variant values when you get it.

    You might be able to do something by setting the thread culture so that the default calls to model.Value() return in the culture of the executing thread, but I am not 100% sure that would work, or it wouldn't cause you other issues down the line.

  • Daniel Gustafsson 13 posts 93 karma points
    May 10, 2019 @ 08:19
    Daniel Gustafsson
    0

    Hi,

    Ah okay, the other approach works just fine :).

    Thanks for the help

    Cheers

  • Dave de Moel 122 posts 574 karma points c-trib
    Jul 10, 2019 @ 07:52
    Dave de Moel
    0

    Since Umbraco 8.1 is released with changes for better Multilingual support. Is there a better way to get language specific values now instead of having to rely on magic strings?

  • Harry Gordon 15 posts 92 karma points c-trib
    Jan 19, 2020 @ 11:34
    Harry Gordon
    0

    Bit late to the party here but yes, you can avoid magic strings by using an expression:

    content.Value(x => x.SomeValue, culture);
    
Please Sign in or register to post replies

Write your reply to:

Draft