My facebook javascript that I added to uses the “like” button from facebook and other components, looks like this:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '164355773607006', // App ID
channelURL : '//WWW.KOOLBUSINESS.COM/static/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
// Additional initialization code here
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
{% ifequal host "www.montao.com.br" %}
js.src = "//connect.facebook.net/pt_BR/all.js";
{% else %}
js.src = "//connect.facebook.net/en_US/all.js";
{% endifequal %}
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
Can I put it at the bottom of the pages instead of at the top since that will load the page faster?
How do I check that the channel file is working?
Can my channel file be on another domain, though it is the same app (my app serves multiple comains)?
Is there another way for my server to access the session than with cookies?
Is there a better was to switch between Portuguese and English than I did? My way works but perhaps is inferior. A better way could look for HTTP ACCEPT_LANGUAGE or cookie or http get parameter for language. In this case one domain should alway be in portuguese but it would be nice to switch the language on the FB components when a switch my app’s languages with the django_language_cookie. So I suppose I could read the django_language_cookie with javascript instead of checking the domain, but then I’d have to learn how to do it.
Can you comment or help me with my solution?
Thank you
Your channel file must be on the same domain, the requesting document is served from.
Well, you can check if it’s working when you request the URL in the browser and getting content 😉
I switch the language of the JS-SDK in the following way for my projects:
With the signed request, there is a “locale” variable passed to your server.
You can grab it by decoding the SR and accessing $sr[‘user’][‘locale’] (or pass the decoded SR to your template engine and access it via {{SR.user.locale}} – this maps to a string formatted like “en_US” or whatever language the user has set on facebook.
Embedding the JS-SDK in the correct localization is a snap, then:
Well, if you want to keep only the Protugese/English switch because it would look weird to serve for example german dialogs when your app is only PT/EN, then include it as following:
But I would stick with the first solution, because the dialogs opened by the SDK would be experienced by the user as a part of facebook – and they would exept it in the language they set facebook to.
Hope this helps you!
(Also remember to cache this locale setting if your app has multiple pages, because you only get the RT on the first request 😉 )