Git Product home page Git Product logo

slim-polyglot's Introduction

Slim Framework Polyglot

Build Status

Resolves the response's current language based on the requested URI, the client's preferred language, and the available languages.

Alters the ResponseInterface to assign the Content-Language header.

Uses willdurand/Negotiation to detect and negotiate the client language.

Install

Via Composer

$ composer require mcaskill/slim-polyglot

Requires Slim 3.0.0 or newer.

Usage

$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

// Register Middleware
$app->add( new Polyglot([ 'en', 'fr', 'es' ]) );

// Example route with ETag header
$app->get('/foo', function ($request, $response) {
	$language = $response->getHeader('Content-Language');

	// Handle response!
});

$app->run();

The Polyglot middleware can also accept callbacks that are executed after a language is chosen.

$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

// Register Middleware
$app->add(
	new Polyglot(
		// Available languages
		[ 'en', 'fr', 'es' ],
		// Fallback language
		'fr',
		// Hooks to call after language is resolved
		[
			function ($language) use ($container) {
				$container['environment']['language.current'] = $language;
			},
			[ 'MySuperApp', 'set_language' ]
		]
	)
);

// Example route with ETag header
$app->get('/foo', function ($request, $response) {
	$language = $this->environment['language.current'];

	// Handle response!
});

$app->run();

Testing

TBD

Notes

The language code may be formatted as ISO 639-1 alpha-2 (en), ISO 639-3 alpha-3 (msa), or ISO 639-1 alpha-2 combined with an ISO 3166-1 alpha-2 localization (zh-tw).

[ "en", "fr-CA" ]

When giving a list of languages to the Polyglot middleware, the first language is used as the fallback language. When switching to a language that is not in the list of supported languages, the first language is used instead.

Definitions:

  • "language-fallback" — Default language; determined as the first among your supported languages.
  • "language-preferred" — Preferred language(s); determined as the client's localization preferences.
  • "language-current" — Current language; determined as the localization to use based on a cross-reference of the application's supported languages and the client's priority of preferred languages.

License

The MIT License (MIT). Please see License File for more information.

slim-polyglot's People

Contributors

mcaskill avatar ironsmile avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.