Index: lang-guess-ajax.php
===================================================================
--- lang-guess-ajax.php	(revision 20201)
+++ lang-guess-ajax.php	(working copy)
@@ -1,24 +1,26 @@
 <?php
 define( 'DB_USER', '' );
 define( 'DB_PASSWORD', '' );
 define( 'DB_NAME', '' );
 define( 'DB_HOST', '' );
 
 // Relevant: Environment, versions, errors.
 // Not relevant: options, users, super admins, keys, secrets, sso.
 require dirname( __DIR__ ) . '/define.php';
+require WPORGPATH . 'wp-includes/plugin.php';
+require WPORGPATH . 'wp-includes/formatting.php';
 require WPORGPATH . 'hyperdb/bb-10-hyper-db.php';
 require WPORGPATH . 'object-cache.php';
 wp_cache_init();
 require __DIR__ . '/class-guess-lang.php';
 require GLOTPRESS_LOCALES_PATH;
 
 // Search engines don't need to index this content. See #meta4154
 header( 'X-Robots-Tag: noindex' );
 
 // Cache the result for a day in browsers.
 header( 'Vary: Origin, Accept-Language' );
 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 86400 /* 24 * 60 * 60 */ ) . ' GMT' );
 
 // Set a CORS header to allow WordPress.org subdomains to request it.
 if ( isset( $_SERVER['HTTP_ORIGIN'] ) && preg_match( '!^https?://([^.]+\.)?wordpress.org/?$!i', $_SERVER['HTTP_ORIGIN'], $m ) ) {
@@ -66,102 +68,105 @@ function lang_guess_request() {
 	/*
 	 * Add a debugging endpoint, so that we can get feedback from end-users easier.
 	 */
 	if ( ! empty( $_GET['debug'] ) ) {
 		header( 'Content-type: text/plain; charset=utf-8' );
 
 		$language_header = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? 'Not Set';
 		$anonymized_ip   = preg_replace( '!\.\d+$!', '.xxx', $_SERVER['REMOTE_ADDR'] );
 		$ip_country      = $guesser->get_country_by_ip();
 
 		echo json_encode( compact( 'language_header', 'anonymized_ip', 'ip_country', 'guesses' ), JSON_PRETTY_PRINT );
 		die();
 	}
 
 	if ( is_array( $guesses ) && ! empty( $guesses ) ) {
-		$num_guesses = count( $guesses );
-
 		if ( $is_forums ) {
-			if ( 1 === $num_guesses ) {
-				$string = translate_gp( 'WordPress support forums are also available in %s.', $guesses[0]['locale'] );
-			} else {
-				$string = translate_gp( 'WordPress support forums are also available in %s (also %s).', $guesses[0]['locale'] );
-			}
+			$string = translate_gp( 'WordPress support forums are also available in %s.', $guesses[0]['locale'] );
 		} else {
-			if ( 1 === $num_guesses ) {
-				$string = translate_gp( 'WordPress is also available in %s.', $guesses[0]['locale'] );
-			} else {
-				$string = translate_gp( 'WordPress is also available in %s (also %s).', $guesses['0']['locale'] );
-			}
+			$string = translate_gp( 'WordPress is also available in %s.', $guesses[0]['locale'] );
 		}
 
 		$translated = array();
 		foreach ( $guesses as $guess ) {
 			if ( isset( $guess['name'], $guess['subdomain'] ) ) {
 				$path = '/';
 				if ( $is_forums ) {
 					$path = '/support/';
 				} else if ( $is_download ) {
 					$path = '/downloads/';
 				}
 
-				$translated[] = "<a href='https://{$guess['subdomain']}.wordpress.org{$path}'>{$guess['name']}</a>";
+				$name = \GP_Locales::by_field( 'wp_locale', $guess['locale'] )->native_name;
+				$translated[] = "<a href='https://{$guess['subdomain']}.wordpress.org{$path}'>{$name}</a>";
 			}
 		}
 
 		// All locales found didn't pass the Name/Subdomain checks above.
 		if ( ! $translated ) {
 			return;
 		}
 
-		echo '<div id="lang-guess">';
+		// Load translations for wp_sprintf_l().
+		add_filter( 'wp_sprintf_l', function( $translations ) use( $guesses ) {
+			$translations['between']          = sprintf( translate_gp( '%1$s, %2$s', $guesses[0]['locale'] ), '', '' );
+			$translations['between_last_two'] = sprintf( translate_gp( '%1$s, and %2$s', $guesses[0]['locale'] ), '', '' );
+			$translations['between_only_two'] = sprintf( translate_gp( '%1$s and %2$s', $guesses[0]['locale'] ), '', '' );
 
-		if ( 1 === $num_guesses ) {
-			printf( $string, $translated[0] );
-		} else {
-			$main_language = array_shift( $translated );
-			printf( $string, $main_language, implode( ', ', $translated ) );
-		}
+			return $translations;
+		} );
 
+		echo '<div id="lang-guess">';
+		printf( $string, wp_sprintf_l( '%l', $translated ) );
 		echo '</div>';
 	}
 }
 
 function translate_gp( $string, $wp_locale ) {
 	global $wpdb;
+
+	// Short-circuit for english variants.
+	if ( 0 === stripos( $wp_locale, 'en_' ) ) {
+		return $string;
+	}
+
 	$strings = array(
+		// These are in wp/dev
+		7233895 => '%1$s, %2$s',
+		7234333 => '%1$s, and %2$s',
+		7960    => '%1$s and %2$s',
+
+		// These are in meta/forums-old
 		204369 => 'WordPress support forums are also available in %s.',
-		204370 => 'WordPress support forums are also available in %s (also %s).',
 		204371 => 'WordPress is also available in %s.',
-		204372 => 'WordPress is also available in %s (also %s).',
 	);
 
 	$original_id = array_search( $string, $strings, true );
 	$gp_locale = GP_Locales::by_field( 'wp_locale', $wp_locale )->slug;
 
 	$cache = wp_cache_get( 'original-' . $original_id, 'lang-guess-translations' );
 	if ( false !== $cache ) {
 		return isset( $cache[ $gp_locale ] ) ? $cache[ $gp_locale ] : $string;
 	}
 
-	// Magic number: 81 is rosetta/forums.
+	// Magic number: 81 is rosetta/forums-old, 2 is wp/dev
 	$translations = $wpdb->get_results( $wpdb->prepare(
 		"SELECT locale as gp_locale, translation_0 as translation
 		FROM translate_translation_sets ts
 		INNER JOIN translate_translations t
 		ON ts.id = t.translation_set_id
-		WHERE project_id = 81 AND slug = 'default' AND t.status = 'current'
+		WHERE project_id IN( 2, 81 ) AND slug = 'default' AND t.status = 'current'
 		AND original_id = %d", $original_id ), OBJECT_K );
 
 	foreach ( $translations as &$translation ) {
 		$translation = $translation->translation;
 	}
 	unset( $translation );
 
 	wp_cache_add( 'original-' . $original_id, $translations, 'lang-guess-translations', 900 );
 
 	return isset( $translations[ $gp_locale ] ) ? $translations[ $gp_locale ] : $string;
 }
 
 function lang_guess_jsonp_request() {
 	if ( ! isset( $_GET['callback'] ) ) {
 		return;
@@ -187,19 +192,16 @@ function lang_guess_jsonp_request() {
 	$jsonp_callback = preg_replace( '/[^a-z0-9]/', '', $_GET['callback'] );
 	echo '/**/' . $jsonp_callback . '(' . $result . ')';
 }
 
 
 $return = isset( $_GET['return'] ) ? preg_replace( '/[^a-z]/', '', $_GET['return'] ) : 'html';
 if ( 'jsonp' === $return ) {
 	lang_guess_jsonp_request();
 } else {
 	lang_guess_request();
 }
 
 exit;
 
 __( 'WordPress support forums are also available in %s.' );
-__( 'WordPress support forums are also available in %s (also %s).' );
 __( 'WordPress is also available in %s.' );
-__( 'WordPress is also available in %s (also %s).' );
-
