Browse Source

added option for URI languages.

Taylor Otwell 12 years ago
parent
commit
c8718a32ec
2 changed files with 54 additions and 3 deletions
  1. 14 1
      application/config/application.php
  2. 40 2
      laravel/laravel.php

+ 14 - 1
application/config/application.php

@@ -84,7 +84,7 @@ return array(
 
 
 	/*
 	/*
 	|--------------------------------------------------------------------------
 	|--------------------------------------------------------------------------
-	| Application Language
+	| Default Application Language
 	|--------------------------------------------------------------------------
 	|--------------------------------------------------------------------------
 	|
 	|
 	| The default language of your application. This language will be used by
 	| The default language of your application. This language will be used by
@@ -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('fr', 'sp'),
+
 	/*
 	/*
 	|--------------------------------------------------------------------------
 	|--------------------------------------------------------------------------
 	| SSL Link Generation
 	| SSL Link Generation

+ 40 - 2
laravel/laravel.php

@@ -107,6 +107,46 @@ 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();
+
+$locales = Config::get('application.languages', array());
+
+$locales[] = Config::get('application.language');
+
+/*
+|--------------------------------------------------------------------------
+| Set The Locale Based On Route
+|--------------------------------------------------------------------------
+|
+| If the URI starts with one of the supported languages, we will set
+| the default language to match that URI segment and shorten the
+| URI we'll pass to the router to not include the lang segment.
+|
+*/
+
+foreach ($locales as $locale)
+{
+	if (starts_with($uri, $locale))
+	{
+		Config::set('application.language', $locale);
+
+		$uri = trim(substr($uri, strlen($locale)), '/'); break;
+	}
+}
+
+if ($uri === '') $uri = '/';
+
 /*
 /*
 |--------------------------------------------------------------------------
 |--------------------------------------------------------------------------
 | Route The Incoming Request
 | Route The Incoming Request
@@ -118,8 +158,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();