code.fastix.org

Dateiansicht:

Datei:Projekte -> PHP:LanguageSelector -> test.php
md5:416194483d6c6ac7957656a73abc9b9e
sha1:f3873fb27a571392943bd23ce4c63ee8cb101f61
  1. <?php
  2.  
  3. /**
  4. * Please note, this a first test and second a example
  5. * If this show to diffcult, so you have to know: you only need parts of this and you CAN (not must; not shold) simplify many things.
  6. **/
  7.  
  8. # Simulate Cookie with Language:
  9. #$_COOKIE['USER_SELECTED_LANGUAGE'] = 'es';
  10.  
  11. # Simulate hostname with Language:
  12. #$_SERVER['HTTP_HOST'] = 'pt.fastix.org';
  13.  
  14. # Simulate uri:
  15. #$_SERVER['REQUEST_URI'] = '/ru/test.php';
  16.  
  17. $tr['Willkommen']['de'] = 'Willkommen!';
  18. $tr['Willkommen']['en'] = 'Welcome!';
  19. $tr['Willkommen']['fr'] = 'Bienvenue!';
  20. $tr['Willkommen']['pt'] = 'Bem-vindo!';
  21. $tr['Willkommen']['es'] = 'Bienvenida!';
  22. $tr['Willkommen']['cn'] = '欢迎!';
  23. $tr['Willkommen']['ru'] = 'добро пожаловать!';
  24. $tr['Willkommen']['tr'] = 'Karşılama!';
  25.  
  26. $tr['Projektbeschreibung']['de'] = 'Projektbeschreibung';
  27. $tr['Projektbeschreibung']['en'] = 'Project description';
  28. $tr['Projektbeschreibung']['fr'] = 'Description du projet';
  29. $tr['Projektbeschreibung']['pt'] = 'descrição do projecto';
  30. $tr['Projektbeschreibung']['es'] = 'Descripción del proyecto';
  31. $tr['Projektbeschreibung']['cn'] = '项目介绍';
  32. $tr['Projektbeschreibung']['ru'] = 'описание проекта';
  33. $tr['Projektbeschreibung']['tr'] = 'proje açıklaması';
  34.  
  35. $tr['Cookie löschen + neu laden']['de'] = 'Cookie löschen + neu laden';
  36. $tr['Cookie löschen + neu laden']['en'] = 'Delete cookie + reload';
  37. $tr['Cookie löschen + neu laden']['fr'] = 'Supprimer Cookie + Reload';
  38. $tr['Cookie löschen + neu laden']['pt'] = 'Excluir Cookies + Reload';
  39. $tr['Cookie löschen + neu laden']['es'] = 'Eliminar la galleta + Actualizar';
  40. $tr['Cookie löschen + neu laden']['cn'] = '删除Cookie+刷新';
  41. $tr['Cookie löschen + neu laden']['ru'] = 'Удалить Cookie + Reload';
  42. $tr['Cookie löschen + neu laden']['tr'] = 'Çerez + yükle Sil';
  43.  
  44. $tr['Wählen Sie die gewünschte Sprache']['de'] = 'Wählen Sie die gewünschte Sprache';
  45. $tr['Wählen Sie die gewünschte Sprache']['en'] = 'Select the desired language';
  46. $tr['Wählen Sie die gewünschte Sprache']['fr'] = 'Sélectionnez la langue quelque vous prefere';
  47. $tr['Wählen Sie die gewünschte Sprache']['pt'] = 'Seleccione el idioma deseado';
  48. $tr['Wählen Sie die gewünschte Sprache']['es'] = 'Seleccione la lengua';
  49. $tr['Wählen Sie die gewünschte Sprache']['cn'] = '选择的语言的链路你';
  50. $tr['Wählen Sie die gewünschte Sprache']['ru'] = 'Выберите нужный язык';
  51. $tr['Wählen Sie die gewünschte Sprache']['tr'] = 'İstediğiniz dili seçin';
  52.  
  53. $langName['de'] = 'deutsch';
  54. $langName['en'] = 'english';
  55. $langName['fr'] = 'français';
  56. $langName['pt'] = 'português';
  57. $langName['es'] = 'español';
  58. $langName['cn'] = '中国';
  59. $langName['ru'] = 'русский';
  60. $langName['tr'] = 'türk';
  61.  
  62. $startTime = microtime( true );
  63.  
  64. ### RUN!
  65. require_once 'LanguageSelector.php';
  66. $arLanguages = array('de', 'en', 'fr','pt', 'es', 'cn', 'ru', 'tr'  );
  67. $LanguageSelector = new LanguageSelector( $arLanguages );
  68. #$LanguageSelector -> storeMethod = 'cookie';                    # this is the default
  69. #$LanguageSelector -> storeMethod = 'session';                   # alternative method
  70. #$LanguageSelector -> storeName   = 'USER_SELECTED_LANGUAGE';    # this is the default
  71. define ( 'LANGUAGE', $LanguageSelector  -> getLang() );
  72.  
  73.  
  74. ?><!doctype HTML>
  75. <html lang="<?=LANGUAGE;?>">
  76.     <head>
  77.         <meta charset="utf-8">
  78.         <title><?=$tr['Willkommen'][LANGUAGE];?></title>
  79. <?php
  80.                 foreach ($arLanguages as $lang) {
  81. ?>
  82.         <link rel="alternate" hreflang="<?=$lang;?>" href="?<?=urlencode($LanguageSelector -> storeName);?>=<?=urlencode($lang);?>" />
  83. <?php
  84.                 }
  85. ?>
  86.     </head>
  87.     <body>
  88.         <h1><?=$tr['Willkommen'][LANGUAGE];?></h1>
  89. <?php if ( 'cookie' == $LanguageSelector -> storeMethod ) { ?>
  90.         <h2>(Cookie)</h2>
  91.         <p><?=$tr['Wählen Sie die gewünschte Sprache'][LANGUAGE];?>:</p>
  92.         <p>
  93. <?php
  94.                 foreach ($arLanguages as $lang) {
  95. ?>
  96.             <button onclick="document.cookie='<?=$LanguageSelector->storeName;?>=<?=$lang;?>'; location.reload();"><?=$lang;?></button>
  97. <?php
  98.                 }
  99. }
  100. ?>
  101.         </p>
  102.         <h2>(HREF/GET)</h2>
  103.         <p><?=$tr['Wählen Sie die gewünschte Sprache'][LANGUAGE];?>:</p>
  104.         <p>
  105. <?php
  106.                 foreach ($arLanguages as $lang) {
  107. ?>
  108.             <a hreflang="<?=$lang;?>" href="?<?=urlencode($LanguageSelector -> storeName);?>=<?=urlencode($lang);?>"><?=htmlspecialchars($lang);?></a>&nbsp;
  109. <?php
  110.                 }
  111. ?>
  112.         </p>
  113.  
  114.         <h2>(Form/Post)</h2>
  115.         <p><?=$tr['Wählen Sie die gewünschte Sprache'][LANGUAGE];?>:</p>
  116.         <form method="post">
  117.             <select id="<?=$LanguageSelector -> storeName;?>" name="<?=$LanguageSelector -> storeName;?>" onchange="submit()">
  118. <?php
  119.                 foreach ($arLanguages as $lang) {
  120.                 $selected='';
  121.                 if ($lang == $LanguageSelector -> getLastSelection() ) { $selected=' selected="selected"'; }
  122. ?>
  123.                 <option value="<?=htmlspecialchars($lang);?>"<?=$selected;?>><?=htmlentities($langName[$lang]);?></option>
  124. <?php
  125.                 }
  126. ?>
  127.             </select>
  128.         </form>
  129.         <p>
  130. <?php if ( 'cookie' == $LanguageSelector -> storeMethod ) { ?>
  131.             <button onclick="document.cookie = '<?=$LanguageSelector -> storeName;?>=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; location.reload();"><?=$tr['Cookie löschen + neu laden'][LANGUAGE]?></button>
  132. <?php } elseif ( 'session' == $LanguageSelector -> storeMethod ) { ?>
  133.             <button onclick="location.href='<?=$_SERVER['SCRIPT_NAME'];?>?<?=urlencode($LanguageSelector -> storeName);?>=unset';"><?=$tr['Cookie löschen + neu laden'][LANGUAGE]?></button>
  134. <?php } else {
  135.             trigger_error("Fatal: storeMethod must be 'cookie' or 'session'. given is: '" . $this -> storeMethod . "'", E_USER_ERROR );
  136.       }
  137. ?>
  138.         </p>
  139.         <hr />
  140.         <?=round( ( microtime(true) - $startTime ) * 1000, 2 ); ?> ms elapsed on a Banana Pi M3
  141.         <hr />
  142.         <a href="https://code.fastix.org/Projekte/PHP%3ALanguageSelector/"><?=$tr['Projektbeschreibung'][LANGUAGE];?></a>
  143.     </body>
  144. </html>