0

Can we access Umbraco Dictionary item value from JavaScript ?

As I am using Umbraco 6.0.5

BJ Patel
  • 6,148
  • 11
  • 47
  • 81

3 Answers3

2

No, you can't do it out-of-the-box.

I had to do this multiple times, and the best way I came up with, is to print all dictionary items as an object when the page loads (in <head> or something). Of curse only items for the current language.

So in your source you have

<script>
    var dic = {"quantity":"Quantity","totalPrice":"Total price","securePayment":"Secure payment"};
</script>

And then get is as

window.dic["quantity"]
Morten OC
  • 1,784
  • 2
  • 23
  • 30
  • 1
    Thumbs up for providing a better idea than what I suggested. This makes more sense considering how the mapping from alias to item will likely have to be done many times in each view, and will, in those cases, give better over all performance.... assuming your dictionary isn't massive :D – Scherling Jan 22 '15 at 13:49
  • Yup, it will give much better performance this way. I actually also cache this list for each language :) – Morten OC Jan 22 '15 at 14:49
1

As far as I know, you can't do it out of the box - haven't actually tried, though.

I would first create a simple rest service in my website that returns the Umbraco dictionary item using a querystring parameter as the alias value

var alias = HttpContext.Current.Request.QueryString["alias"]
if(alias != null)
{
    var dictionaryItem = umbraco.GetDictionaryItem(alias) 
    ...
}  

Then call your own webservice through javascript to get the value

Scherling
  • 1,350
  • 2
  • 13
  • 23
0

What I have done is declare variable in using Javascript

<script type="text/javascript">    
    var UmbracoDicKeyValue = '<%= kraftvaerk.umbraco.Translations.translate("Umbraco_Dic_Key", lang) %>';
</script>

And for setting value of lang variable from server side.

protected string lang = (!String.IsNullOrEmpty(umbraco.library.Session("lang")) ? umbraco.library.Session("lang") : "en-GB");
umbraco.library.setSession("lang", lang);

Now use variable UmbracoDicKeyValue in javascript code.

BJ Patel
  • 6,148
  • 11
  • 47
  • 81