Copied to clipboard

Flag this post as spam?

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


  • Mike 18 posts 39 karma points
    Dec 11, 2015 @ 16:50
    Mike
    0

    Passing all dictionary translations to javascript

    Hi. I am wanting to create an equivalent array of translation phrases for use my custom javascript functions. Is there any way (in razor if possible) I can loop through the Dictionary, getting each alias and translation so I can generate an javascript object. I can find much documentation about using the localizationservice and dictionary. I'm using the latest version 7 of Umbraco.

    Thanks!

  • Ian 178 posts 752 karma points
    Dec 11, 2015 @ 19:29
    Ian
    0

    Hi mike i believe all languages are currently held in xml files in umbraco so maybe as a last resort you could load the relevant language into javascript.

    But really the localization service is there to just get the strings you need. If you are in, the backed you can just use the angular localize directive which keeps everything more javascript oriented. Even if you can't use the directive for your needs you could take a look at its code to get a head start on creating something yourself

  • Mike 18 posts 39 karma points
    Dec 14, 2015 @ 08:44
    Mike
    0

    Hi. Thanks for the reply! I'm looking at using translations in the front-end of a multi-language website. Rather than duplicate all the language 'if-this-then-write-that' code I wanted to use the values already entered in the back-end. I'll take a look at the code and see if I can work it out. Thanks again!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Dec 14, 2015 @ 09:18
    Chriztian Steinmeier
    0

    Hi Mike,

    You could try to have a look at the Dictionary 2 JavaScript package and maybe see how it's done. The source is available as far as I can see...

    Hope that helps,

    /Chriztian

  • Mike 18 posts 39 karma points
    Dec 14, 2015 @ 10:02
    Mike
    1

    Thanks Chriztian. I'll take a look. In the meantime I've got this far...

    StringBuilder dict = new StringBuilder();
    var dictObjs = ApplicationContext.Current.Services.LocalizationService.GetRootDictionaryItems();
    if(dictObjs.Any()) {
      dict.Append("var jsDictionary = []; ");
      foreach(IDictionaryItem phrase in dictObjs) {
        dict.Append("jsDictionary.push(\"" + phrase.ItemKey + "\",\"" +     Umbraco.GetDictionaryValue(phrase.ItemKey) + "\"); ");
      }
    }
    

    I'll do a few tests and see how I get on with this :)

    Cheers!

  • Mike 18 posts 39 karma points
    Dec 14, 2015 @ 10:20
    Mike
    0

    Seems to work! :)

Please Sign in or register to post replies

Write your reply to:

Draft