localization.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /* CHANGELOG:
  3. @1.1.1
  4. - sostituita la funzione deprecata split() con explode()
  5. @1.1:
  6. - supporta locale con forma la_LA, la, la_LA.utf-8, la_LA.UTF-8, la.UTF-8, la.utf-8
  7. */
  8. $version="1.1.1";
  9. $_td_stack = array(); // text domains stack
  10. $langarray = array('it_IT','en_US','es_ES','de_DE','eo_EO');
  11. //$lang = "eo_EO";
  12. If (isSet($_GET["lang"]))
  13. $lang = $_GET["lang"];
  14. else {
  15. $lang = $langarray[0];
  16. /* vedi le preferenze del browser */
  17. $user_langs = explode(",", getenv("HTTP_ACCEPT_LANGUAGE"));
  18. foreach ($user_langs as $user_lang) {
  19. $user_lang_items = explode(";", $user_lang);
  20. if (in_array($user_lang_items[0], $langarray)) {
  21. $lang = $user_lang_items[0];
  22. break;
  23. }
  24. }
  25. }
  26. $charset="UTF-8";
  27. $gettext_domain="messages";
  28. $locale_dir="res/locale";
  29. /**
  30. * Il locale deve essere presente nel sistema usa "locale -a" per
  31. * verificarlo, non ovunque si chiama allo stesso modo e non tutti
  32. * i locali si chiamano allo stesso modo, l'esperanto ad esempio
  33. * non rispetta la nomenclatura classica
  34. */
  35. putenv("LC_ALL=$lang");
  36. $l=explode("_",$lang);
  37. if(! setlocale(LC_ALL, $lang.".".$charset))
  38. if(! setlocale(LC_ALL, $lang))
  39. if(! setlocale(LC_ALL,$l[0].".".$charset))
  40. setlocale(LC_ALL,$l[0]);
  41. bindtextdomain($gettext_domain, "res/locale");
  42. textdomain($gettext_domain);
  43. bind_textdomain_codeset($gettext_domain, $charset);
  44. function printLangSelector() {
  45. global $lang;
  46. global $langarray;
  47. $targetname = $_SERVER['PHP_SELF'];
  48. $l_arg="lang=";
  49. $newQueryString="";
  50. $queryString= $_SERVER['QUERY_STRING'];
  51. $argS = explode("&",$queryString);
  52. if(strpos($queryString,$l_arg)!==FALSE){
  53. foreach ($argS as $arg){
  54. if(strpos($arg,$l_arg)===FALSE)
  55. $newQueryString.=$arg."&";
  56. }
  57. }
  58. echo "<ul class='langsel'>";
  59. foreach ($langarray as $l) {
  60. $label=split("_",$l);
  61. $class="";
  62. if ($l == $lang)
  63. $class="class='it'";
  64. echo "<li ".$class."><a href='".$targetname."?".$newQueryString.$l_arg.$l."'>".$label[0]."</a></li>";
  65. }
  66. $foo = getenv("HTTP_ACCEPT_LANGUAGE");
  67. echo "</ul>\n";
  68. }
  69. /**
  70. * Sets a new text domain after recording the current one
  71. * so it can be restored later with restore_textdomain().
  72. *
  73. * It's possible to nest calls to these two functions.
  74. * @param string the new text domain to set
  75. */
  76. function set_textdomain($td)
  77. {
  78. global $_td_stack;
  79. $old_td = textdomain(NULL);
  80. if ($old_td) {
  81. if (!strcmp($old_td, $td))
  82. array_push($_td_stack, false);
  83. else
  84. array_push($_td_stack, $old_td);
  85. }
  86. textdomain($td);
  87. }
  88. /**
  89. * Restore the text domain active before the last call to
  90. * set_textdomain().
  91. */
  92. function restore_textdomain()
  93. {
  94. global $_td_stack;
  95. $old_td = array_pop($_td_stack);
  96. if ($old_td)
  97. textdomain($old_td);
  98. }
  99. ?>