2017 © Pedro Peláez
 

library php-language

PHP Language class - This class detects the language (from url, session, cookie and browser) and then sets a current one

image

vades/php-language

PHP Language class - This class detects the language (from url, session, cookie and browser) and then sets a current one

  • Sunday, March 23, 2014
  • by vades
  • Repository
  • 0 Watchers
  • 1 Stars
  • 9 Installations
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Language switch PHP class

This class detects the language (from url, session, cookie and browser) and then sets a current one, (*1)

Usage, (*2)

1. Download

Git: https://github.com/vades/php-language.git, (*3)

Composer: {"require": {"vades/php-language": "dev-master"} }, (*4)

2. Include class and config into your code

require_once '../src/Vades/Language/LanguageSwitch.php';, (*5)

$cfg = require_once '../src/Vades/Language/config.php';';, (*6)

use \Vades\Language\LanguageSwitch;, (*7)

OR autoload with Composer, (*8)

require 'vendor/autoload.php';, (*9)

3. Set values in config file config.php

Default language: 'default' => 'en', (*10)

Supported languages: 'supported' => array('en','de','fr','es','it'), (*11)

4. Initialize the LanguageSwitch

     $cfg['default'],
                    'supported' => $cfg['supported'],
                    'url' => $lang_from_url ,
                    'session' => $lang_from_session,
                    'cookie' => $lang_from_ucookie,
                    'browser' => substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)
                ));

         ?>

5. Use it with method chaining

    <?php

            $lang = $switch->fromUrl()
                ->fromSession()
                ->fromCookie()
                ->fromBrowser()
                ->get();

     ?>

6. Code snippet

    <?php

            require_once '../src/Vades/Language/LanguageSwitch.php';
            use \Vades\Language\LanguageSwitch;

            $cfg = require_once '../src/Vades/Language/config.php';

            // Start session
            if (strlen(session_id()) < 1) {
                session_start();
                $_SESSION['lang'] = 'de';
            }
            // Set cookie
            setcookie('lang', 'fr', time()+60);

            // Initialize the language switch
            $switch = new LanguageSwitch(array(
                'default' => $cfg['default'],
                'supported' => $cfg['supported'],
                'url' => (isset($_GET['lang']) ? $_GET['lang'] : null) ,
                'session' => (isset($_SESSION['lang']) ? $_SESSION['lang'] : null),
                'cookie' => (isset($_COOKIE['lang']) ? $_COOKIE['lang'] : null),
                // Extract the two digit language code from the http headers 
               'browser' => substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)
            ));

            $lang = $switch->fromUrl()
                    ->fromSession()
                    ->fromCookie()
                    ->fromBrowser()
                    ->get();

            var_dump($switch,$lang);

     ?>

The Versions

23/03 2014

dev-master

9999999-dev

PHP Language class - This class detects the language (from url, session, cookie and browser) and then sets a current one

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

language php library class

23/03 2014

dev-dev

dev-dev

PHP Language class - This class detects the language (from url, session, cookie and browser) and then sets a current one

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

language php library class