--- wp-includes/l10n.php	2010-07-30 14:00:22.000000000 +0200
+++ wp-includes/l10n_mine.php	2010-10-26 10:28:31.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,14 @@
 }
 
 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 ('dgettext'))
+	  $translation = dgettext ($domain, $text); # TODO
+	else {
+	  $translations = &get_translations_for_domain( $domain );
+	  $translation = $translations->translate( $text, $context );
+	}
+	
+	return apply_filters( 'gettext_with_context', $translation, $text, $context, $domain );
 }
 
 /**
@@ -236,8 +251,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 +269,13 @@
  *
  */
 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, $single, $plural, $number); # TODO
+	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 );
 }
 
@@ -313,10 +338,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;
@@ -468,7 +515,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];
 }
