While creating Facebook Fan Page apps, you might have experienced certain use case when you want to display the fan page in various languages ? I have noticed that many forums and blogs suggested to use <fb:restricted-to> FBML tag which apparently is deprecated.

In this post, we are going to see how you can detect language preference of a user who is viewing your app or fanpage on facebook. After getting the current user’s language you can easily show your facebook fan page in multiple languages. I am assuming that you already know how to setup app on facebook, if no, then please go through this basic tutorial which shows how to setup app on facebook and update your status using PHP.

Once we have the app setup, then  simply use the below given code to know visitor’s preferred language. The file which is going to serve the app, most likely an index file in the canvas folder, should have the following code –

<?php 
require_once 'src/facebook.php';
 
$app_id = "YOUR_APP_ID_HERE";
$app_secret = "APP_SECRET_HERE";
 
 
 
$facebook = new Facebook(array(
	'appId' => $app_id,
	'secret' => $app_secret,
	'cookie' => true
	));
 
$signed_request = $facebook->getSignedRequest();   // create the signed object
 
 
$locale = $signed_request['user']['locale'];  //grab the locale information
 
//write the logic based on the language found
switch ($localte) {
	case 'de_DE':
		//your german content goes here
		break;
	case 'fr_FR'
		//  your french content goes here
	break;
 
	default: //en_US
		//your english content goes here
		break;
}

Once you have the above code in your php file, you should be able to see the language code of the user you are logged in. If English is your current language then you should be able to see en_US. You can write your logic based on these language codes and hence the end user will see your fan page in his/her local language.If you want to see a complete list of all the language codes then these can be found in this XML file provide by facebook.

Amazing no ? So go ahead and add sparkles to your facebook fan page with this small code snippet. Hope this helps!

Stay Digified!!
Sachin Khosla

Share this post: