Advertisement
HuguesDelorme

Untitled

Dec 20th, 2011
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. <?php
  2. // define constants
  3. define('PROJECT_DIR', realpath('./'));
  4. define('LOCALE_DIR', PROJECT_DIR .'/locales');
  5. define('DEFAULT_LOCALE', 'fr_FR');
  6.  
  7. require_once('../libs/php-gettext-1.0.11/gettext.inc');
  8.  
  9. $supported_locales = array('fr_FR', 'en_US', 'fr_FR.utf8', 'en_US.utf8');
  10.  
  11. // Set cookie to keep track of the active language
  12. if (isset($_GET['lang'])) {
  13.   // Langage is passed through URL parameters
  14.   $lang = $_GET['lang'];
  15.   setcookie('lang', $lang, time() + 3600*24, '/'); // Expires in 24h
  16. }
  17. elseif (isset($_COOKIE['lang']))
  18.   $lang = $_COOKIE['lang'];
  19.  
  20.  
  21. if (!isset($lang)) {
  22.   // Use browser's locale
  23.   $lang = substr($HTTP_SERVER_VARS['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  24.   if (!isset($lang) || !in_array($lang, $supported_locales)) // Default locale
  25.     $lang = DEFAULT_LOCALE;
  26.   //echo "isset($lang)   " + $lang;
  27.   setcookie('lang', $lang, time() + 3600*24, '/'); // Expires in 24h
  28. }
  29.  
  30. $encoding = 'UTF-8';
  31. $locale = $lang;
  32.  
  33. // gettext setup
  34. T_setlocale(LC_MESSAGES, $locale);
  35. // Set the text domain as 'translation'
  36. $domain = 'translation';
  37. bindtextdomain($domain, LOCALE_DIR);
  38. // bind_textdomain_codeset is supported only in PHP 4.2.0+
  39. if (function_exists('bind_textdomain_codeset'))
  40.   bind_textdomain_codeset($domain, $encoding);
  41. textdomain($domain);
  42.  
  43. header("Content-type: text/html; charset=$encoding");
  44. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement