Browse Source

reimplement locale uri slugs.

Taylor Otwell 12 years ago
parent
commit
926cdaa7f5
3 changed files with 71 additions and 6 deletions
  1. 13 0
      application/config/application.php
  2. 42 2
      laravel/laravel.php
  3. 16 4
      laravel/url.php

+ 13 - 0
application/config/application.php

@@ -94,6 +94,19 @@ return array(
 
 
 	'language' => 'en',
 	'language' => 'en',
 
 
+	/*
+	|--------------------------------------------------------------------------
+	| Supported Languages
+	|--------------------------------------------------------------------------
+	|
+	| These languages may also be supported by your application. If a request
+	| enters your application with a URI beginning with one of these values
+	| the default language will automatically be set to that language.
+	|
+	*/
+
+	'languages' => array(),
+
 	/*
 	/*
 	|--------------------------------------------------------------------------
 	|--------------------------------------------------------------------------
 	| SSL Link Generation
 	| SSL Link Generation

+ 42 - 2
laravel/laravel.php

@@ -107,6 +107,48 @@ Routing\Router::register('*', '(:all)', function()
 	return Event::first('404');
 	return Event::first('404');
 });
 });
 
 
+/*
+|--------------------------------------------------------------------------
+| Gather The URI And Locales
+|--------------------------------------------------------------------------
+|
+| When routing, we'll need to grab the URI and the supported locales for
+| the route so we can properly set the language and route the request
+| to the proper end-point in the application.
+|
+*/
+
+$uri = URI::current();
+
+$languages = Config::get('application.languages', array());
+
+$languages[] = Config::get('application.language');
+
+/*
+|--------------------------------------------------------------------------
+| Set The Locale Based On The Route
+|--------------------------------------------------------------------------
+|
+| If the URI starts with one of the supported languages, we will set
+| the default lagnauge to match that URI segment and shorten the
+| URI we'll pass to the router to not include the lang segment.
+|
+*/
+
+foreach ($languages as $language)
+{
+	if (starts_with($uri, $language))
+	{
+		Config::set('application.language', $language);
+
+		$uri = trim(substr($uri, strlen($language)), '/'); break;
+	}
+}
+
+if ($uri == '') $uri = '/';
+
+URI::$uri = $uri;
+
 /*
 /*
 |--------------------------------------------------------------------------
 |--------------------------------------------------------------------------
 | Route The Incoming Request
 | Route The Incoming Request
@@ -118,8 +160,6 @@ Routing\Router::register('*', '(:all)', function()
 |
 |
 */
 */
 
 
-$uri = URI::current();
-
 Request::$route = Routing\Router::route(Request::method(), $uri);
 Request::$route = Routing\Router::route(Request::method(), $uri);
 
 
 $response = Request::$route->call();
 $response = Request::$route->call();

+ 16 - 4
laravel/url.php

@@ -26,7 +26,7 @@ class URL {
 	 */
 	 */
 	public static function current()
 	public static function current()
 	{
 	{
-		return static::to(URI::current());
+		return static::to(URI::current(), null, false, false);
 	}
 	}
 
 
 	/**
 	/**
@@ -89,9 +89,11 @@ class URL {
 	 *
 	 *
 	 * @param  string  $url
 	 * @param  string  $url
 	 * @param  bool    $https
 	 * @param  bool    $https
+	 * @param  bool    $asset
+	 * @param  bool    $locale
 	 * @return string
 	 * @return string
 	 */
 	 */
-	public static function to($url = '', $https = null)
+	public static function to($url = '', $https = null, $asset = false, $locale = true)
 	{
 	{
 		// If the given URL is already valid or begins with a hash, we'll just return
 		// If the given URL is already valid or begins with a hash, we'll just return
 		// the URL unchanged since it is already well formed. Otherwise we will add
 		// the URL unchanged since it is already well formed. Otherwise we will add
@@ -105,7 +107,17 @@ class URL {
 		// security for any new links generated.  So https for all secure links.
 		// security for any new links generated.  So https for all secure links.
 		if (is_null($https)) $https = Request::secure();
 		if (is_null($https)) $https = Request::secure();
 
 
-		$root = static::base().'/'.Config::get('application.index');
+		$root = static::base();
+
+		if ( ! $asset)
+		{
+			$root .= '/'.Config::get('application.index');
+		}
+
+		if ( ! $asset and $locale and count(Config::get('application.languages')) > 0)
+		{
+			$root .= '/'.Config::get('application.language');
+		}
 
 
 		// Since SSL is not often used while developing the application, we allow the
 		// Since SSL is not often used while developing the application, we allow the
 		// developer to disable SSL on all framework generated links to make it more
 		// developer to disable SSL on all framework generated links to make it more
@@ -232,7 +244,7 @@ class URL {
 			return rtrim($root, '/').'/'.ltrim($url, '/');
 			return rtrim($root, '/').'/'.ltrim($url, '/');
 		}
 		}
 
 
-		$url = static::to($url, $https);
+		$url = static::to($url, $https, true);
 
 
 		// Since assets are not served by Laravel, we do not need to come through
 		// Since assets are not served by Laravel, we do not need to come through
 		// the front controller. So, we'll remove the application index specified
 		// the front controller. So, we'll remove the application index specified