Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rely on WP site-default option + only display available langs #3587

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 86 additions & 44 deletions includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,90 @@ function action_admin_menu() {
);
}

/**
* Languages supported in Elasticsearch mappings.
*
* If $format is 'elasticsearch', the array format is `Elasticsearch analyzer name => [ WordPress language package names ]`.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-lang-analyzer.html
* @since 4.7.0
* @param string $format Format of the return ('locales' or 'elasticsearch' )
* @return array
*/
function get_available_languages( string $format = 'elasticsearch' ) : array {
/**
* Filter available languages in Elasticsearch.
*
* The returned array should follow the format `Elasticsearch analyzer name => [ WordPress language package names ]`.
*
* @since 4.7.0
* @hook ep_available_languages
* @param {bool} $available_languages List of available languages
* @return {bool} New list
*/
$es_languages = apply_filters(
'ep_available_languages',
[
'arabic' => [ 'ar', 'ary' ],
'armenian' => [ 'hy' ],
'basque' => [ 'eu' ],
'bengali' => [ 'bn', 'bn_BD' ],
'brazilian' => [ 'pt_BR' ],
'bulgarian' => [ 'bg', 'bg_BG' ],
'catalan' => [ 'ca' ],
'cjk' => [], // CJK characters (not a language)
'czech' => [ 'cs', 'cs_CZ' ],
'danish' => [ 'da', 'da_DK' ],
'dutch' => [ 'nl_NL_formal', 'nl_NL', 'nl_BE' ],
'english' => [ 'en', 'en_AU', 'en_GB', 'en_NZ', 'en_CA', 'en_US', 'en_ZA' ],
'estonian' => [ 'et' ],
'finnish' => [ 'fi' ],
'french' => [ 'fr', 'fr_CA', 'fr_FR', 'fr_BE' ],
'galician' => [ 'gl_ES' ],
'german' => [ 'de', 'de_DE', 'de_DE_formal', 'de_CH', 'de_CH_informal', 'de_AT' ],
'greek' => [ 'el' ],
'hindi' => [ 'hi_IN' ],
'hungarian' => [ 'hu_HU' ],
'indonesian' => [ 'id_ID' ],
'irish' => [], // WordPress doesn't support Irish as an active locale currently
'italian' => [ 'it_IT' ],
'latvian' => [ 'lv' ],
'lithuanian' => [ 'lt_LT' ],
'norwegian' => [ 'nb_NO' ],
'persian' => [ 'fa_IR' ],
'portuguese' => [ 'pt', 'pt_AO', 'pt_PT', 'pt_PT_ao90' ],
'romanian' => [ 'ro_RO' ],
'russian' => [ 'ru_RU' ],
'sorani' => [ 'ckb' ],
'spanish' => [ 'es_CR', 'es_MX', 'es_VE', 'es_AR', 'es_CL', 'es_GT', 'es_PE', 'es_ES', 'es_UY', 'es_CO' ],
'swedish' => [ 'sv_SE' ],
'turkish' => [ 'tr_TR' ],
'thai' => [ 'th' ],
]
);

if ( 'locales' === $format ) {
$arr = array_reduce(
$es_languages,
function ( $acc, $lang ) {
$lang = array_filter(
$lang,
function ( $locale ) {
// English is always added. This removes the duplicates
return ! in_array( $locale, [ 'en', 'en_US' ], true );
}
);
$acc = array_merge( $acc, $lang );
return $acc;
},
[]
);
return $arr;
}

return $es_languages;
}

/**
* Uses the language from EP settings in mapping.
*
Expand All @@ -743,7 +827,7 @@ function use_language_in_setting( $language = 'english', $context = '' ) {
*
* @see https://core.trac.wordpress.org/ticket/49263
*/
if ( 'ep_site_default' === $ep_language ) {
if ( 'site-default' === $ep_language ) {
$locale = null;
$wp_local_package = null;
$ep_language = get_locale();
Expand All @@ -759,49 +843,7 @@ function use_language_in_setting( $language = 'english', $context = '' ) {
$wp_language = 'en_US';
}

/**
* Languages supported in Elasticsearch mappings.
* Array format: Elasticsearch analyzer name => WordPress language package name
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-lang-analyzer.html
*/
$es_languages = [
'arabic' => [ 'ar', 'ary' ],
'armenian' => [ 'hy' ],
'basque' => [ 'eu' ],
'bengali' => [ 'bn', 'bn_BD' ],
'brazilian' => [ 'pt_BR' ],
'bulgarian' => [ 'bg' ],
'catalan' => [ 'ca' ],
'cjk' => [], // CJK characters (not a language)
'czech' => [ 'cs' ],
'danish' => [ 'da' ],
'dutch' => [ 'nl_NL_formal', 'nl_NL', 'nl_BE' ],
'english' => [ 'en', 'en_AU', 'en_GB', 'en_NZ', 'en_CA', 'en_US', 'en_ZA' ],
'estonian' => [ 'et' ],
'finnish' => [ 'fi' ],
'french' => [ 'fr', 'fr_CA', 'fr_FR', 'fr_BE' ],
'galician' => [ 'gl_ES' ],
'german' => [ 'de', 'de_DE', 'de_DE_formal', 'de_CH', 'de_CH_informal', 'de_AT' ],
'greek' => [ 'el' ],
'hindi' => [ 'hi_IN' ],
'hungarian' => [ 'hu_HU' ],
'indonesian' => [ 'id_ID' ],
'irish' => [], // WordPress doesn't support Irish as an active locale currently
'italian' => [ 'it_IT' ],
'latvian' => [ 'lv' ],
'lithuanian' => [ 'lt_LT' ],
'norwegian' => [ 'nb_NO' ],
'persian' => [ 'fa_IR' ],
'portuguese' => [ 'pt', 'pt_AO', 'pt_PT', 'pt_PT_ao90' ],
'romanian' => [ 'ro_RO' ],
'russian' => [ 'ru_RU' ],
'sorani' => [ 'ckb' ],
'spanish' => [ 'es_CR', 'es_MX', 'es_VE', 'es_AR', 'es_CL', 'es_GT', 'es_PE', 'es_ES', 'es_UY', 'es_CO' ],
'swedish' => [ 'sv_SE' ],
'turkish' => [ 'tr_TR' ],
'thai' => [ 'th' ],
];
$es_languages = get_available_languages();

/**
* Languages supported in Elasticsearch snowball token filters.
Expand Down
25 changes: 11 additions & 14 deletions includes/partials/settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
* @package elasticpress
*/

use ElasticPress\Utils as Utils;
use ElasticPress\Elasticsearch as Elasticsearch;
use ElasticPress\Dashboard;
use ElasticPress\Utils;
use ElasticPress\Elasticsearch;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
Expand Down Expand Up @@ -165,21 +166,17 @@
<?php
$ep_language = Utils\get_language();

$dropdown = wp_dropdown_languages(
wp_dropdown_languages(
[
'id' => 'ep_language',
'name' => 'ep_language',
'selected' => $ep_language,
'echo' => false,
'id' => 'ep_language',
'name' => 'ep_language',
'selected' => $ep_language,
'languages' => Dashboard\get_available_languages( 'locales' ),
'show_option_site_default' => true,
'explicit_option_en_us' => true,
'show_available_translations' => 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
4 changes: 2 additions & 2 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,13 @@ function get_term_tree( $all_terms, $orderby = 'count', $order = 'desc', $flat =
}

/**
* Returns the defaiult language for ES mapping.
* Returns the default language for ES mapping.
*
* @return string Default EP language.
*/
function get_language() {
$ep_language = get_option( 'ep_language' );
$ep_language = ! empty( $ep_language ) ? $ep_language : 'ep_site_default';
$ep_language = ! empty( $ep_language ) ? $ep_language : 'site-default';

/**
* Filter the default language to use at index time
Expand Down
34 changes: 33 additions & 1 deletion tests/php/TestDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function test_use_language_in_setting_for_stop() {
}

/**
* Test the `use_language_in_setting` function when on multisite using `ep_site_default`
* Test the `use_language_in_setting` function when on multisite using `site-default`
*
* @group skip-on-single-site
* @group dashboard
Expand Down Expand Up @@ -139,4 +139,36 @@ public function test_use_language_in_setting_for_multisite() {
switch_to_blog( $site_he_il );
$this->assertSame( 'english', Dashboard\use_language_in_setting() );
}

/**
* Test the `get_available_languages` function
*
* @group dashboard
*/
public function test_get_available_languages() {
$languages = Dashboard\get_available_languages();
$this->assertSame( [ 'ar', 'ary' ], $languages['arabic'] );
$this->assertSame( [ 'th' ], $languages['thai'] );

$languages = Dashboard\get_available_languages( 'locales' );
$this->assertContains( 'ar', $languages );
$this->assertContains( 'ary', $languages );
$this->assertContains( 'th', $languages );
}

/**
* Test the `ep_available_languages` filter
*
* @group dashboard
*/
public function test_get_available_languages_ep_available_languages_filter() {
$add_language = function ( $languages ) {
$languages['custom'] = [ 'cu_ST', 'om' ];
return $languages;
};
add_filter( 'ep_available_languages', $add_language );

$languages = Dashboard\get_available_languages();
$this->assertSame( [ 'cu_ST', 'om' ], $languages['custom'] );
}
}
2 changes: 1 addition & 1 deletion tests/php/TestUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public function test_delete_transient() {
* @group utils
*/
public function test_get_language() {
$this->assertSame( 'ep_site_default', Utils\get_language() );
$this->assertSame( 'site-default', Utils\get_language() );

$set_lang_via_option = function() {
return 'custom_via_option';
Expand Down