recover data from JSON file

Asked

Viewed 558 times

1

I am needing to implement a translation into a plugin called pikaday, which is actually a datepicker, and as the site is multilingual I am trying to implode the i18n. pulling translations from a JSON: https://myjson.com/1bazm

and here in jsfiddle the example: http://jsfiddle.net/pz4pwkLq/1/

what is the best way to take each translation at a time? for example, and if I want to take the value "en" and move to a variable? what would it be like?

  • You can set the language as path, hashbang or search of a URL. Each time it (URL) is loaded, you invoke the function that renders the calendar with the respective language input.

1 answer

2


In case you want to get the translated texts you have to use AJAX. You can also do this on the server, but assuming you do it with Javascript you can use $.getJSON. Note that AJAX ($.getJSON) is asynchronous, so you have to put the code that needs those texts inside the callback of $.getJSON.

Thus, an example, using "en-us":

$.getJSON('https://api.myjson.com/bins/1bazm', function (i18n) {
    var picker = new Pikaday({
        field: $datePicker,
        format: 'DD/MM/YYYY',
        i18n: i18n.langs["en-us"]
    })
});

jsFiddle: http://jsfiddle.net/pz4pwkLq/2/

  • I put the EN version: http://jsfiddle.net/pz4pwkLq/3/

  • You can also do this direct, without api: http://jsfiddle.net/pz4pwkLq/4/

  • opa, sorry for the delay in responding, but that was it @Sergio

Browser other questions tagged

You are not signed in. Login or sign up in order to post.