User:Fxparlant/BrowserDetection

From Meta, a Wikimedia project coordination wiki

Jump to: navigation, search
Blue Glass Arrow.svg MediaWiki logo.png
This page should be moved to MediaWiki.org.
Please do not move the page by hand. It will be imported by a MediaWiki.org administrator with the full edit history. In the meantime, you may continue to edit the page as normal.

Contents

Browser language Detection

Here is my language detection extension (to detect the language of client browser), to load the corresponding left menu file (default is english when there no file for the detected language) : My site


The extension has to be declared in LocalSettings.php, and a few lines needs to be added in templates/xhtml_slim.pt and includes/SkinPHPTal.php


templates/xhtml_slim.pt

	<div class="portlet" id="p-logo">
	  <a style="background-image: url(${logopath});" href="${nav_urls/mainpage/href}" i18n:attributes="title string:mainpage"></a>
	</div>
    <div class="portlet" id="p-menu">
        <h5 >Menu</h5>
        <div class="pBody">
	     <ul id="t-menu" tal:src-include="string:menu${menulang}.txt">
         </ul>
        </div>
    </div>
	<div class="portlet" id="p-nav">
	  <h5 i18n:translate="string:navigation">Navigation</h5>


includes/SkinPHPTal.php

          $tpl->setRef( "logopath", $wgLogo );
	  $tpl->setRef( "lang", $wgLanguageCode );
          //Modified FXP detect browser language
          $tpl->setRef( "browserlang", $wgBrowserLanguageCode );
          //$wgBrowserLanguageCodeMenu="Fr";
          $tpl->setRef( "menulang", $wgBrowserLanguageCodeMenu );
	  $tpl->set( "dir", $wgLang->isRTL() ? "rtl" : "ltr" );
	  $tpl->set( "rtl", $wgLang->isRTL() );
	  $tpl->set( "langname", $wgLang->getLanguageName( $wgLanguageCode ) );

extension/BrowserDetection.php

<?php 
// DWD Modify -> Add: Set Language from Browser Language Settings.
// Taken from Dokuwiki tips
//Modified FXP
  /*
  #=============================================================================#
  # Function: detect_browser_language()                                         #
  #-----------------------------------------------------------------------------#
  # Purpose: This function detects the user browser language.                   #
  #   If no valid language is found then it returns the default content language#
  # that is set on the configuration file                                       #
  #                                                                             #
  # Arguments:                                                                  #
  # Optionals:                                                                  #
  # Result: returns a string with the content language.                         #
  #                                                                             #
  #=============================================================================#
  */

    global $wgLanguageCode, $wgBrowserLanguageCode, $wgBrowserLanguageCodeMenu;
 
    $wgBrowserLanguageCode = preg_replace('/(;q=\d .\d )/i', '', getenv('HTTP_ACCEPT_LANGUAGE'));
    $wgBrowserLanguageCode = strtolower(substr($wgBrowserLanguageCode,0, 2));
//$wgBrowserLanguageCodeMenu = $wgBrowserLanguageCode;

 switch ($wgBrowserLanguageCode){
    case 'fr' : $wgBrowserLanguageCodeMenu='Fr';
    break;
    case 'de' : $wgBrowserLanguageCodeMenu='De';
    break;
    case 'en' : $wgBrowserLanguageCodeMenu='En';
    break;
    default :
    $wgBrowserLanguageCodeMenu ="";
    $wgBrowserLanguageCode ="";
 }

 
?>

Credits

This code comes mainly from dokuwiki tips