I18N: Introduce `word_count_type` property to `WP_Locale`.

This changesets adds a `word_count_type` property, so that it does not need to be translated separately across multiple projects.

List of changes:
- New property: `WP_Locale::word_count_type`.
- New method: `WP_Locale::get_word_count_type()`.
- New function: `wp_get_word_count_type()` as a wrapper for `WP_Locale::get_word_count_type()`.
- All `_x( 'words', 'Word count type. Do not translate!' )` strings have been replaced with a call to `wp_get_word_count_type()`.

Props pedromendonca, desrosj, costdev, mukesh27, johnbillion.
Fixes #56698.

Built from https://develop.svn.wordpress.org/trunk@55279


git-svn-id: http://core.svn.wordpress.org/trunk@54812 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
audrasjb 2023-02-07 17:28:18 +00:00
parent fbf90e465b
commit 3223743a6a
5 changed files with 58 additions and 13 deletions

View File

@ -112,6 +112,16 @@ class WP_Locale {
*/
public $list_item_separator;
/**
* The word count type of the locale language.
*
* Default is 'words'.
*
* @since 6.2.0
* @var string
*/
public $word_count_type;
/**
* Constructor which calls helper methods to set up object variables.
*
@ -236,6 +246,9 @@ class WP_Locale {
} elseif ( 'rtl' === _x( 'ltr', 'text direction' ) ) {
$this->text_direction = 'rtl';
}
// Set the word count type.
$this->word_count_type = $this->get_word_count_type();
}
/**
@ -396,4 +409,31 @@ class WP_Locale {
public function get_list_item_separator() {
return $this->list_item_separator;
}
/**
* Retrieves the localized word count type.
*
* Options are 'characters_excluding_spaces', 'characters_including_spaces or 'words'. Defaults to 'words'.
*
* @since 6.2.0
*
* @return string Localized word count type.
*/
public function get_word_count_type() {
/*
* translators: If your word count is based on single characters (e.g. East Asian characters),
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
* Do not translate into your own language.
*/
$word_count_type = is_null( $this->word_count_type ) ? _x( 'words', 'Word count type. Do not translate!' ) : $this->word_count_type;
// Check for valid types.
if ( 'characters_excluding_spaces' !== $word_count_type && 'characters_including_spaces' !== $word_count_type ) {
// Defaults to 'words'.
$word_count_type = 'words';
}
return $word_count_type;
}
}

View File

@ -3945,12 +3945,7 @@ function wp_trim_words( $text, $num_words = 55, $more = null ) {
$text = wp_strip_all_tags( $text );
$num_words = (int) $num_words;
/*
* translators: If your word count is based on single characters (e.g. East Asian characters),
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
* Do not translate into your own language.
*/
if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
if ( str_starts_with( wp_get_word_count_type(), 'characters' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
preg_match_all( '/./u', $text, $words_array );
$words_array = array_slice( $words_array[0], 0, $num_words + 1 );

View File

@ -1809,3 +1809,18 @@ function wp_get_list_item_separator() {
return $wp_locale->get_list_item_separator();
}
/**
* Retrieves the word count type based on the locale.
*
* @since 6.2.0
*
* @global WP_Locale $wp_locale WordPress date and time locale object.
*
* @return string Locale-specific word count type.
*/
function wp_get_word_count_type() {
global $wp_locale;
return $wp_locale->get_word_count_type();
}

View File

@ -1831,12 +1831,7 @@ function wp_just_in_time_script_localization() {
'word-count',
'wordCountL10n',
array(
/*
* translators: If your word count is based on single characters (e.g. East Asian characters),
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
* Do not translate into your own language.
*/
'type' => _x( 'words', 'Word count type. Do not translate!' ),
'type' => wp_get_word_count_type(),
'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array(),
)
);

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-55278';
$wp_version = '6.2-alpha-55279';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.