Skip to content

Commit

Permalink
Merge branch 'lang' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Aug 15, 2016
2 parents c37aa68 + f70c2a4 commit 5dcc446
Show file tree
Hide file tree
Showing 20 changed files with 807 additions and 126 deletions.
25 changes: 25 additions & 0 deletions application/Config/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,31 @@ class App extends BaseConfig
*/
public $defaultLocale = 'en';

/*
|--------------------------------------------------------------------------
| Negotiate Locale
|--------------------------------------------------------------------------
|
| If true, the current Request object will automatically determine the
| language to use based on the value of the Accept-Language header.
|
| If false, no automatic detection will be performed.
|
*/
public $negotiateLocale = false;

/*
|--------------------------------------------------------------------------
| Supported Locales
|--------------------------------------------------------------------------
|
| If $negotiateLocale is true, this array lists the locales supported
| by the application in descending order of priority. If no match is
| found, the first locale will be used.
|
*/
public $supportedLocales = ['en'];

/*
|--------------------------------------------------------------------------
| URI PROTOCOL
Expand Down
17 changes: 17 additions & 0 deletions application/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ public static function iterator($getShared = true)

//--------------------------------------------------------------------

/**
* Responsible for loading the language string translations.
*/
public static function language(string $locale = null, $getShared = true)
{
if ($getShared)
{
return self::getSharedInstance('language', $locale);
}

$locale = ! empty($locale) ? $locale : self::request()->getLocale();

return new \CodeIgniter\Language\Language($locale);
}

//--------------------------------------------------------------------

/**
* The file locator provides utility methods for looking for non-classes
* within namespaced folders, as well as convenience methods for
Expand Down
7 changes: 7 additions & 0 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,13 @@ protected function tryToRouteIt(RouteCollectionInterface $routes = null)
$this->controller = $this->router->handle($path);
$this->method = $this->router->methodName();

// If a {locale} segment was matched in the final route,
// then we need to set the correct locale on our Request.
if ($this->router->hasLocale())
{
$this->request->setLocale($this->router->getLocale());
}

$this->benchmark->stop('routing');
}

Expand Down
17 changes: 17 additions & 0 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,24 @@ function shared_service(string $name, ...$params)

//--------------------------------------------------------------------

if (! function_exists('lang'))
{
/**
* A convenience method to translate a string and format it
* with the intl extension's MessageFormatter object.
*
* @param string $line
* @param array $args
*
* @return string
*/
function lang(string $line, array $args=[])
{
return Services::language()->getLine($line, $args);
}
}

//--------------------------------------------------------------------



Expand Down
107 changes: 107 additions & 0 deletions system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ class IncomingRequest extends Request
*/
protected $negotiate;

/**
* The default Locale this request
* should operate under.
*
* @var string
*/
protected $defaultLocale;

/**
* The current locale of the application.
* Default value is set in Config\App.php
*
* @var string
*/
protected $locale;

/**
* Stores the valid locale codes.
*
* @var array
*/
protected $validLocales = [];

//--------------------------------------------------------------------

/**
Expand All @@ -123,6 +146,90 @@ public function __construct($config, $uri = null, $body = 'php://input')
$this->uri = $uri;

$this->detectURI($config->uriProtocol, $config->baseURL);

$this->validLocales = $config->supportedLocales;

$this->detectLocale($config);
}

//--------------------------------------------------------------------

/**
* Handles setting up the locale, perhaps auto-detecting through
* content negotiation.
*
* @param $config
*/
public function detectLocale($config)
{
$this->locale = $this->defaultLocale = $config->defaultLocale;

if (! $config->negotiateLocale)
{
return;
}

$this->setLocale($this->negotiate('language', $config->supportedLocales));
}

//--------------------------------------------------------------------

/**
* Returns the default locale as set in Config\App.php
*
* @return string
*/
public function getDefaultLocale(): string
{
return $this->defaultLocale;
}

//--------------------------------------------------------------------

/**
* Gets the current locale, with a fallback to the default
* locale if none is set.
*
* @return string
*/
public function getLocale(): string
{
return $this->locale ?? $this->defaultLocale;
}

//--------------------------------------------------------------------

/**
* Sets the locale string for this request.
*
* @param string $locale
*
* @return $this
*/
public function setLocale(string $locale)
{
// If it's not a valid locale, set it
// to the default locale for the site.
if (! in_array($locale, $this->validLocales))
{
$locale = $this->defaultLocale;
}

$this->locale = $locale;

// If the intl extension is loaded, make sure
// that we set the locale for it... if not, though,
// don't worry about it.
try {
if (class_exists('\Locale', false))
{
\Locale::setDefault($locale);
}
}
catch (\Exception $e)
{}

return $this;
}

//--------------------------------------------------------------------
Expand Down
59 changes: 0 additions & 59 deletions system/HTTP/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,6 @@ class Request extends Message implements RequestInterface
*/
protected $proxyIPs;

/**
* The default Locale this request
* should operate under.
*
* @var string
*/
protected $defaultLocale;

/**
* The current locale of the application.
* Default value is set in Config\App.php
*
* @var string
*/
protected $locale;

//--------------------------------------------------------------------

/**
Expand All @@ -84,49 +68,6 @@ class Request extends Message implements RequestInterface
public function __construct($config, $uri=null)
{
$this->proxyIPs = $config->proxyIPs;

$this->locale = $this->defaultLocale = $config->defaultLocale;
}

//--------------------------------------------------------------------

/**
* Returns the default locale as set in Config\App.php
*
* @return string
*/
public function getDefaultLocale(): string
{
return $this->defaultLocale;
}

//--------------------------------------------------------------------

/**
* Gets the current locale, with a fallback to the default
* locale if none is set.
*
* @return string
*/
public function getLocale(): string
{
return $this->locale ?? $this->defaultLocale;
}

//--------------------------------------------------------------------

/**
* Sets the locale string for this request.
*
* @param string $locale
*
* @return $this
*/
public function setLocale(string $locale)
{
$this->locale = $locale;

return $this;
}

//--------------------------------------------------------------------
Expand Down
30 changes: 0 additions & 30 deletions system/HTTP/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +84,4 @@ public function getMethod($upper = false): string;
public function getServer($index = null, $filter = null);

//--------------------------------------------------------------------

/**
* Returns the default locale as set in Config\App.php
*
* @return string
*/
public function getDefaultLocale(): string;

//--------------------------------------------------------------------

/**
* Gets the current locale, with a fallback to the default
* locale if none is set.
*
* @return string
*/
public function getLocale(): string;

//--------------------------------------------------------------------

/**
* Sets the locale string for this request.
*
* @param string $locale
*
* @return $this
*/
public function setLocale(string $locale);

//--------------------------------------------------------------------
}
Loading

0 comments on commit 5dcc446

Please sign in to comment.