germania-kg/localisation

Localisation interfaces, classes and factory

1.1.0 2023-01-04 08:04 UTC

This package is auto-updated.

Last update: 2024-11-04 12:23:33 UTC


README

Germania KG · Localisation

Packagist PHP version Tests

Installation

$ composer require germania-kg/localisation

Interfaces

LocalisationInterface

<?php
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;
}

LocalisationFactoryInterface

<?php
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;
}

Factories

NegotiationLocalisationFactory

Class NegotiationLocalisationFactory implements LocalisationFactoryInterface. The constructor requires an instance of Will Durand´s LanguageNegotiator and an array with available language codes and locale strings.

Setup

<?php
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");

Usage

<?php
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  
}

Development

$ git clone git@github.com:GermaniaKG/Localisation.git
$ cd Localisation
$ composer install