Skip to content

Commit

Permalink
Merge pull request #3550 from 10up/feature/lang-site-default
Browse files Browse the repository at this point in the history
Allow to use the site default language
  • Loading branch information
felipeelia authored Jul 25, 2023
2 parents 4973ba0 + 436247f commit eb3dfa2
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 11 deletions.
24 changes: 21 additions & 3 deletions includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,8 @@ function action_admin_menu() {
* @return string The updated language.
*/
function use_language_in_setting( $language = 'english', $context = '' ) {
global $locale, $wp_local_package;

// Get the currently set language.
$ep_language = Utils\get_language();

Expand All @@ -730,6 +732,17 @@ function use_language_in_setting( $language = 'english', $context = '' ) {
return $language;
}

/**
* WordPress does not reset the language when switch_blog() is called.
*
* @see https://core.trac.wordpress.org/ticket/49263
*/
if ( 'ep_site_default' === $ep_language ) {
$locale = null;
$wp_local_package = null;
$ep_language = get_locale();
}

require_once ABSPATH . 'wp-admin/includes/translation-install.php';
$translations = wp_get_available_translations();

Expand Down Expand Up @@ -815,6 +828,10 @@ function use_language_in_setting( $language = 'english', $context = '' ) {
'Turkish',
];

$es_snowball_similar = [
'Brazilian' => 'Portuguese',
];

foreach ( $es_languages as $analyzer_name => $analyzer_language_codes ) {
if ( in_array( $wp_language, $analyzer_language_codes, true ) ) {
$language = $analyzer_name;
Expand All @@ -823,11 +840,12 @@ function use_language_in_setting( $language = 'english', $context = '' ) {
}

if ( 'filter_ewp_snowball' === $context ) {
if ( in_array( ucfirst( $language ), $es_snowball_languages, true ) ) {
return ucfirst( $language );
$uc_first_language = ucfirst( $language );
if ( in_array( $uc_first_language, $es_snowball_languages, true ) ) {
return $uc_first_language;
}

return 'English';
return $es_snowball_similar[ $uc_first_language ] ?? 'English';
}

if ( 'filter_ep_stop' === $context ) {
Expand Down
10 changes: 9 additions & 1 deletion includes/partials/settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,21 @@
<?php
$ep_language = Utils\get_language();

wp_dropdown_languages(
$dropdown = wp_dropdown_languages(
[
'id' => 'ep_language',
'name' => 'ep_language',
'selected' => $ep_language,
'echo' => false,
]
);

$default_site_option = sprintf(
"<option value='ep_site_default'>%s</option>",
__( 'Default to Site Language', 'elasticpress' )
);

echo preg_replace( '/<select(.*?)>/', "<select$1>{$default_site_option}", $dropdown ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
?>
<p class="description"><?php esc_html_e( 'Default language for your Elasticsearch mapping.', 'elasticpress' ); ?></p>
</td>
Expand Down
9 changes: 2 additions & 7 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,8 @@ function get_term_tree( $all_terms, $orderby = 'count', $order = 'desc', $flat =
* @return string Default EP language.
*/
function get_language() {
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
$ep_language = get_site_option( 'ep_language' );
} else {
$ep_language = get_option( 'ep_language' );
}

$ep_language = ! empty( $ep_language ) ? $ep_language : get_locale();
$ep_language = get_option( 'ep_language' );
$ep_language = ! empty( $ep_language ) ? $ep_language : 'ep_site_default';

/**
* Filter the default language to use at index time
Expand Down
49 changes: 49 additions & 0 deletions tests/php/TestDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ class TestDashboard extends BaseTestCase {
*/
public function set_up() {
remove_filter( 'translations_api', __NAMESPACE__ . '\skip_translations_api' );

parent::set_up();
}

/**
* Clean up after each test
*/
public function tear_down() {
tests_add_filter( 'translations_api', __NAMESPACE__ . '\skip_translations_api' );

parent::tear_down();
}

/**
Expand Down Expand Up @@ -68,6 +72,15 @@ public function test_use_language_in_setting_for_snowball() {
};
add_filter( 'ep_default_language', $existing_lang );
$this->assertSame( 'English', Dashboard\use_language_in_setting( '', 'filter_ewp_snowball' ) );

/**
* Test similar languages
*/
$existing_lang = function () {
return 'pt_BR';
};
add_filter( 'ep_default_language', $existing_lang );
$this->assertSame( 'Portuguese', Dashboard\use_language_in_setting( '', 'filter_ewp_snowball' ) );
}

/**
Expand All @@ -90,4 +103,40 @@ public function test_use_language_in_setting_for_stop() {
add_filter( 'ep_default_language', $existing_lang );
$this->assertSame( '_english_', Dashboard\use_language_in_setting( '', 'filter_ep_stop' ) );
}

/**
* Test the `use_language_in_setting` function when on multisite using `ep_site_default`
*
* @group skip-on-single-site
* @group dashboard
*/
public function test_use_language_in_setting_for_multisite() {
$site_pt_br = wp_insert_site(
[
'domain' => 'example.org',
'path' => '/pt_BR',
'options' => [
'WPLANG' => 'pt_BR',
],
]
);
$site_he_il = wp_insert_site(
[
'domain' => 'example.org',
'path' => '/he_IL',
'options' => [
'WPLANG' => 'he_IL',
],
]
);

switch_to_blog( $site_pt_br );
$this->assertSame( 'brazilian', Dashboard\use_language_in_setting() );

/*
* Hebrew is not a language supported by Elasticsearch out-of-the-box, so it should fallback to English.
*/
switch_to_blog( $site_he_il );
$this->assertSame( 'english', Dashboard\use_language_in_setting() );
}
}
32 changes: 32 additions & 0 deletions tests/php/TestUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,36 @@ public function test_delete_transient() {

$this->assertSame( 1, did_action( $filter_name ) );
}

/**
* Test the `get_language()` method
*
* @since 4.7.0
* @group utils
*/
public function test_get_language() {
$this->assertSame( 'ep_site_default', Utils\get_language() );

$set_lang_via_option = function() {
return 'custom_via_option';
};
if ( is_multisite() ) {
add_filter( 'pre_site_option_ep_language', $set_lang_via_option );
} else {
add_filter( 'pre_option_ep_language', $set_lang_via_option );
}

$this->assertSame( 'custom_via_option', Utils\get_language() );

/**
* Test the `ep_default_language` filter
*/
$set_lang_via_filter = function( $ep_language ) {
$this->assertSame( 'custom_via_option', $ep_language );
return 'custom_via_filter';
};
add_filter( 'ep_default_language', $set_lang_via_filter );

$this->assertSame( 'custom_via_filter', Utils\get_language() );
}
}

0 comments on commit eb3dfa2

Please sign in to comment.