PHP code example of germania-kg / localisation

1. Go to this page and download the library: Download germania-kg/localisation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

germania-kg / localisation example snippets



namespace Germania\Localisation;

interface LocalisationInterface 
{
    // Sets the fully-qualified Locale string such as `en_GB`.
    public function setLocale( string $locale ) : LocalisationInterface;

    // Returns a fully-qualified Locale string such as `en_GB`.
    public function __toString();

    // Returns a fully-qualified Locale string such as `en_GB`.
    public function getLocale() : string;

    // Returns ISO 639-1 language code such as `en`.
    public function getLanguage() : string;

  	// Returns an ISO 3166-1 alpha-2 country/region code such as `GB`, if possible.
    public function getRegion() : ?string;
}



namespace Germania\Localisation;

use Germania\Localisation\LocalisationInterface as Localisation;
use Psr\Http\Message\ServerRequestInterface as Request;

interface LocalisationFactoryInterface
{
    /**
     * @param  ServerRequestInterface $request PSR-7 Server Request
     * @return \Germania\Localisation\LocalisationInterface
     * @throws \Germania\Localisation\FactoryException
     */  
    public function createFromRequest( Request $request ) : Localisation;
}



use Germania\Localisation\NegotiationLocalisationFactory;
use Negotiation\LanguageNegotiator;

$negotiator = new LanguageNegotiator;
$available = array(
	"de" => "de_DE",
  "de-de" => "de_DE",
  "de_DE" => "de_DE"
);

$factory = new NegotiationLocalisationFactory( $negotiator, $available);

// Optional, as factory would use the first available locale from above, 
// e.g. "de_DE"
$factory->setDefaultLocale("en_US");


use Germania\Localisation\ExceptionInterface;  
use Psr\Http\Message\ServerRequestInterface;

try {
  $server_request = ...; //
	$localisation = $factory->createFromRequest( $server_request );  
}
catch (\Germania\Localisation\ExceptionInterface $e) {
  echo get_class($e);
  // Germania\Localisation\FactoryException  
}