Allow on the fly change of locale
Allow on the fly change of locale

This only adds the GET variable to do the switch, the UI option isn't
there yet.
This parsing needs to be done really early because we need to do the
language switch/load before anything else happens... however I really
don't like parsing get variables so early in the index like this because
it's breaking the controller scheme that everything else uses. I may
not be able to get around that, but I'd at least like to clean it up and
maybe move this code out of the index into a function or something.

--- a/include/Resource.class.php
+++ b/include/Resource.class.php
@@ -11,6 +11,8 @@
 
 require_once(GITPHP_BASEDIR . 'lib/php-gettext/streams.php');
 require_once(GITPHP_BASEDIR . 'lib/php-gettext/gettext.php');
+
+define('GITPHP_LOCALE_COOKIE', 'GitPHPLocale');
 
 /**
  * Resource

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -38,6 +38,21 @@
 
 date_default_timezone_set('UTC');
 
+/*
+ * Set the locale based on the user's preference
+ */
+if (isset($_GET['l']) && !empty($_GET['l'])) {
+	setcookie(GITPHP_LOCALE_COOKIE, $_GET['l']);
+	GitPHP_Resource::Instantiate($_GET['l']);
+} else if (isset($_COOKIE[GITPHP_LOCALE_COOKIE]) && !empty($_COOKIE[GITPHP_LOCALE_COOKIE])) {
+	GitPHP_Resource::Instantiate($_COOKIE[GITPHP_LOCALE_COOKIE]);
+} else {
+	/*
+	 * Create the cookie to prevent browser delay
+	 */
+	setcookie(GITPHP_LOCALE_COOKIE, 0);
+}
+
 try {
 
 	// Define these here because these get used in the config file
@@ -51,9 +66,12 @@
 	GitPHP_Config::GetInstance()->LoadConfig(GITPHP_CONFIGDIR . 'gitphp.conf.php');
 
 	/*
-	 * Resource
+	 * Use the default language in the config if user has no preference
+	 * with en_US as the fallback
 	 */
-	GitPHP_Resource::Instantiate(GitPHP_Config::GetInstance()->GetValue('locale', 'en_US'));
+	if (!GitPHP_Resource::Instantiated()) {
+		GitPHP_Resource::Instantiate(GitPHP_Config::GetInstance()->GetValue('locale', 'en_US'));
+	}
 
 	/*
 	 * Debug
@@ -88,7 +106,7 @@
 		throw $e;
 	}
 
-	if (GitPHP_Resource::GetInstance() == null) {
+	if (!GitPHP_Resource::Instantiated()) {
 		/*
 		 * In case an error was thrown before instantiating
 		 * the resource manager

comments