Copied to clipboard

Flag this post as spam?

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


  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    May 07, 2013 @ 22:59
    Damiaan
    0

    Get dictionary items in your IApplicationEventHandler events

    Hi

    We are busy setting up a brand new umbraco 6 site.  I have some events i've wired up through the IApplicationEventHandler.  Everything is working as expected.

    Except for getting dictionary items in these events.  I've tried umbraco.library.GetDictionaryItem("myKey"), i've tried to mess around with LocalizationService.GetDictionaryItemByKey, but I can't get my head around this. They all retrun empty results or throw exceptions telling me the keys don't exist.

    Has anyone and idea how to do this?

    Kind regards

  • Comment author was deleted

    May 08, 2013 @ 11:42

    Try setting the correct culture first

  • Comment author was deleted

    May 08, 2013 @ 11:44
  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    May 08, 2013 @ 16:20
    Damiaan
    0

    Hi,

    I tried things like this using the v6 API.  But I never got that working, probably because I need to polish my knowledge still a bit on it. 

        var newLanguage = services.ContentService.GetById(publishedEntry.Id).Language;
        var dict =
            services.LocalizationService.GetDictionaryItemByKey("myDictKey")
                    .Translations.First(t => t.Language.IsoCode == newLanguage);

    I should probably have done something like Tim said:

    System.Threading.Thread.CurrentThread.CurrentCulture = needSomeLookupAnyway;

    In the end I ended up using the old API's.  This was the working code I ended up with:

        int languageId = MyEvents.LanguageHelpers.GetLanguageId(publishedEntry.Id);
        var subject = (new umbraco.cms.businesslogic.Dictionary.DictionaryItem("myDictKey")).Value(languageId);

    Where the GetLanguageId does nothing more than

        Domain[] domains = umbraco.library.GetCurrentDomains(pageId);
        if (domains != null && domains.Length >= 0)
        {
             return domains[0].Language.id; //yes lowercase id
        }

    If you have a better solution using the v6 Api, please let it know.

  • Brett Spencer 88 posts 259 karma points
    Jan 13, 2016 @ 16:50
    Brett Spencer
    0

    Does anybody know if this has been updated in version 7? I am trying to find any info I can on the latest API method for this but keep getting routed back to this thread.

  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Jan 13, 2016 @ 16:57
    Damiaan
    0

    The LocalizationService is available in v7:

            Services.LocalizationService.GetDictionaryItemByKey(key)
    

    you should be able to use this. The issue you might face, which language is being handled.

    If the application is starting up, you probably don't need languages anyway. Unless you need funky stuff. What issue you trying to solve?

  • Brett Spencer 88 posts 259 karma points
    Jan 13, 2016 @ 21:14
    Brett Spencer
    0

    Yes, lots of fun, funky stuff in this one! Many different sites in one instance with some sites having their own multilingual capabilities.

    In this scenario, the page is loading, calling a surfacecontroller to load a form which sets up to post back to the same controller. So state is being lost, which means no localization. The form is using a custom Umbraco.Web.Mvc.UmbracoViewPage

    But this means that I have to get the Dictionary terms by key as well as the Umbraco Table's id for that language. I have a solution passing the culture along and hitting the database to get the corresponding language id. I am just curious if there was a native (to umbraco) method to call and pass in the key and language id. I have used the same method as you mentioned earlier but still wonder if this hasn't been updated.

    What namespace is that Service located? Gave it a quick try, but need to find out where it is.

  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Jan 14, 2016 @ 20:30
    Damiaan
    0

    So you are not trying to access the Dictionary from IApplicationHandler events? That was what the forum post was about.

Please Sign in or register to post replies

Write your reply to:

Draft