--- l10n.php	2011-06-22 12:02:49.616751154 +0200
+++ l10n_neu.php	2011-06-22 11:52:49.000000000 +0200
@@ -47,7 +47,10 @@
 
 	if ( empty( $locale ) )
 		$locale = 'en_US';
-
+	
+	putenv('LC_ALL=' . $locale);
+	setlocale(LC_ALL, $locale);
+	
 	return apply_filters( 'locale', $locale );
 }
 
@@ -65,8 +68,14 @@
  * @return string Translated text
  */
 function translate( $text, $domain = 'default' ) {
-	$translations = &get_translations_for_domain( $domain );
-	return apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );
+	if (function_exists ('dgettext'))
+	  $translation = dgettext ($domain, $text);
+	else {
+	  $translations = &get_translations_for_domain( $domain );
+	  $translation = $translations->translate( $text );
+	}
+	
+	return apply_filters( 'gettext', $translation, $text, $domain );
 }
 
 function before_last_bar( $string ) {
@@ -78,8 +87,17 @@
 }
 
 function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
-	$translations = &get_translations_for_domain( $domain );
-	return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain );
+	if (function_exists ('dcgettext')) {
+	  $translation = dgettext ($domain, $context . "\x04" . $text);
+	  
+	  if ($translation == $context . "\x04" . $text)
+	    $translation = $text;
+	} else {
+	  $translations = &get_translations_for_domain( $domain );
+	  $translation = $translations->translate( $text, $context );
+	}
+	
+	return apply_filters( 'gettext_with_context', $translation, $text, $context, $domain );
 }
 
 /**
@@ -236,8 +254,13 @@
  * @return string Either $single or $plural translated text
  */
 function _n( $single, $plural, $number, $domain = 'default' ) {
-	$translations = &get_translations_for_domain( $domain );
-	$translation = $translations->translate_plural( $single, $plural, $number );
+	if (function_exists ('dngettext'))  
+	  $translation = dngettext ($domain, $single, $plural, $number);
+	else {
+	  $translations = &get_translations_for_domain( $domain );
+	  $translation = $translations->translate_plural( $single, $plural, $number );
+	}
+	
 	return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
 }
 
@@ -249,8 +272,17 @@
  *
  */
 function _nx($single, $plural, $number, $context, $domain = 'default') {
-	$translations = &get_translations_for_domain( $domain );
-	$translation = $translations->translate_plural( $single, $plural, $number, $context );
+	if (function_exists ('dngettext')) {
+	  $translation = dngettext ($domain, $context . "\x04" . $single, $plural, $number);
+	  
+	  if (($translation == $context . "\x04" . $single) || ($translation == $plural))
+	    $translation = ($number == 1 ? $single : $plural);
+	  
+	} else {
+	  $translations = &get_translations_for_domain( $domain );
+	  $translation = $translations->translate_plural( $single, $plural, $number, $context );
+	}
+	
 	return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
 }
 
@@ -328,10 +360,32 @@
 	}
 
 	do_action( 'load_textdomain', $domain, $mofile );
-
+	
 	$mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
-
+	
 	if ( !is_readable( $mofile ) ) return false;
+	
+	// Try to use gettext whenever available
+	if (function_exists ('bindtextdomain')) {
+	  // Do an automatic setup
+	  $Locale = basename ($mofile, '.mo');
+	  
+	  if (($p = strrpos ($Locale, '-')) !== false)
+	    $Locale = substr ($Locale, $p + 1);
+	  
+	  if (!is_file ('./wp-lang/' . $Locale . '/LC_MESSAGES/' . $domain . '.mo') &&
+	      (is_dir ('./wp-lang/' . $Locale . '/LC_MESSAGES') || mkdir ('./wp-lang/' . $Locale . '/LC_MESSAGES', 0775, true)))
+	    copy ($mofile, './wp-lang/' . $Locale . '/LC_MESSAGES/' . $domain . '.mo');
+	  
+	  // Configure gettext
+	  bindtextdomain ($domain, './wp-lang/');
+	  bind_textdomain_codeset ($domain, 'UTF-8');
+	  
+	  // Register a NOOP-Hanlder for this domain - just to keep consistency
+	  $l10n[$domain] = new NOOP_Translations;
+	  
+	  return true;
+	}
 
 	$mo = new MO();
 	if ( !$mo->import_from_file( $mofile ) ) return false;
@@ -483,7 +537,7 @@
 function &get_translations_for_domain( $domain ) {
 	global $l10n;
 	if ( !isset( $l10n[$domain] ) ) {
-		$l10n[$domain] = &new NOOP_Translations;
+		$l10n[$domain] = new NOOP_Translations;
 	}
 	return $l10n[$domain];
 }
