localization.php 2.6 KB

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