Commit 358e8abd authored by Taylor Otwell's avatar Taylor Otwell

Merge pull request #1390 from Anahkiasen/feature/url_language

Add URL::to_language and HTML::link_to_language localization helpers
parents 829fa4ed 89e3bf1f
......@@ -59,6 +59,17 @@ Sometimes you may need to generate a URL to a named route, but also need to spec
$url = URL::to_action('user@profile', array($username));
<a name="urls-to-a-different-language"></a>
## URLs To A Different Language
#### Generating a URL to the same page in another language:
$url = URL::to_language('fr');
#### Generating a URL to your home page in another language:
$url = URL::to_language('fr', true);
<a name="urls-to-assets"></a>
## URLs To Assets
......
......@@ -87,6 +87,17 @@ For example, the < symbol should be converted to its entity representation. Conv
echo HTML::link_to_action('user@profile', 'User Profile', array($username));
<a name="links-to-a-different-language"></a>
## Links To A Different Language
#### Generating a link to the same page in another language:
echo HTML::link_to_language('fr');
#### Generating a link to your home page another language
echo HTML::link_to_language('fr', true);
<a name="mail-to-links"></a>
## Mail-To Links
......
......@@ -247,6 +247,19 @@ class HTML {
return static::link(URL::to_action($action, $parameters), $title, $attributes);
}
/**
* Generate an HTML link to a different language
*
* @param string $language
* @param string $title
* @param array $attributes
* @return string
*/
public static function link_to_language($language, $title = null, $attributes = array())
{
return static::link(URL::to_language($language), $title, $attributes);
}
/**
* Generate an HTML mailto link.
*
......
......@@ -106,6 +106,26 @@ class URLTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('http://localhost/index.php/url/test/taylor/otwell', URL::to_route('url-test-2', array('taylor', 'otwell')));
}
/**
* Test the URL::to_language method.
*
* @group laravel
*/
public function testToLanguageMethodGeneratesURLsToDifferentLanguage()
{
URI::$uri = 'foo/bar';
Config::set('application.languages', array('sp', 'fr'));
Config::set('application.language', 'sp');
$this->assertEquals('http://localhost/index.php/fr/foo/bar', URL::to_language('fr'));
$this->assertEquals('http://localhost/index.php/fr/', URL::to_language('fr', true));
Config::set('application.index', '');
$this->assertEquals('http://localhost/fr/foo/bar', URL::to_language('fr'));
$this->assertEquals('http://localhost/sp/foo/bar', URL::to_language('en'));
}
/**
* Test language based URL generation.
......
......@@ -294,6 +294,31 @@ class URL {
return static::to($uri, $https);
}
/**
* Get the URL to switch language, keeping the current page or not
*
* @param string $language The new language
* @param boolean $reset Whether navigation should be reset
* @return string An URL
*/
public static function to_language($language, $reset = false)
{
// Get the url to use as base
$url = $reset ? URL::home() : URL::to(URI::current());
// Validate the language
if (!in_array($language, Config::get('application.languages')))
{
return $url;
}
// Get the language we're switching from and the one we're going to
$from = '/'.Config::get('application.language').'/';
$to = '/'.$language.'/';
return str_replace($from, $to, $url);
}
/**
* Substitute the parameters in a given URI.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment