From 89e3bf1fbd3325a97b1eea3ea1ca07c2e9dc0e07 Mon Sep 17 00:00:00 2001 From: Anahkiasen Date: Fri, 26 Oct 2012 22:32:52 +0100 Subject: [PATCH] Add URL::to_language and HTML::link_to_language localization helpers Signed-off-by: Anahkiasen --- laravel/documentation/urls.md | 11 +++++++++++ laravel/documentation/views/html.md | 13 ++++++++++++- laravel/html.php | 19 ++++++++++++++++--- laravel/tests/cases/url.test.php | 20 ++++++++++++++++++++ laravel/url.php | 25 +++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 4 deletions(-) diff --git a/laravel/documentation/urls.md b/laravel/documentation/urls.md index 4263712f..8c03f9b1 100644 --- a/laravel/documentation/urls.md +++ b/laravel/documentation/urls.md @@ -59,6 +59,17 @@ #### Generating a URL to an action with wildcard values: $url = URL::to_action('user@profile', array($username)); + +## 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); + ## URLs To Assets diff --git a/laravel/documentation/views/html.md b/laravel/documentation/views/html.md index aabe244d..dcdb253f 100644 --- a/laravel/documentation/views/html.md +++ b/laravel/documentation/views/html.md @@ -87,6 +87,17 @@ ### Generating a link to a controller action with wildcard values: echo HTML::link_to_action('user@profile', 'User Profile', array($username)); + +## 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); + ## Mail-To Links @@ -119,7 +130,7 @@ #### Creating lists from an array of items: echo HTML::ol(array('Get Peanut Butter', 'Get Chocolate', 'Feast')); echo HTML::ul(array('Ubuntu', 'Snow Leopard', 'Windows')); - + echo HTML::dl(array('Ubuntu' => 'An operating system by Canonical', 'Windows' => 'An operating system by Microsoft')); diff --git a/laravel/html.php b/laravel/html.php index 211e4022..c53db632 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -238,6 +238,19 @@ public static function link_to_action($action, $title = null, $parameters = arra 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. * @@ -347,7 +360,7 @@ private static function listing($type, $list, $attributes = array()) return '<'.$type.static::attributes($attributes).'>'.$html.''; } - + /** * Generate a definition list. * @@ -360,13 +373,13 @@ public static function dl($list, $attributes = array()) $html = ''; if (count($list) == 0) return $html; - + foreach ($list as $term => $description) { $html .= '
'.static::entities($term).'
'; $html .= '
'.static::entities($description).'
'; } - + return ''.$html.''; } diff --git a/laravel/tests/cases/url.test.php b/laravel/tests/cases/url.test.php index 603265ab..cd0a4223 100644 --- a/laravel/tests/cases/url.test.php +++ b/laravel/tests/cases/url.test.php @@ -105,6 +105,26 @@ public function testToRouteMethodGeneratesURLsToRoutes() $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. diff --git a/laravel/url.php b/laravel/url.php index c87139e4..e0693252 100644 --- a/laravel/url.php +++ b/laravel/url.php @@ -294,6 +294,31 @@ public static function to_route($name, $parameters = array()) 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. *