Skip to content

Commit 024946f

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@55279 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d80c313 commit 024946f

File tree

5 files changed

+113
-12
lines changed

5 files changed

+113
-12
lines changed

src/wp-includes/class-wp-locale.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ class WP_Locale {
112112
*/
113113
public $list_item_separator;
114114

115+
/**
116+
* The word count type of the locale language.
117+
*
118+
* Default is 'words'.
119+
*
120+
* @since 6.2.0
121+
* @var string
122+
*/
123+
public $word_count_type;
124+
115125
/**
116126
* Constructor which calls helper methods to set up object variables.
117127
*
@@ -236,6 +246,9 @@ public function init() {
236246
} elseif ( 'rtl' === _x( 'ltr', 'text direction' ) ) {
237247
$this->text_direction = 'rtl';
238248
}
249+
250+
// Set the word count type.
251+
$this->word_count_type = $this->get_word_count_type();
239252
}
240253

241254
/**
@@ -396,4 +409,31 @@ public function _strings_for_pot() {
396409
public function get_list_item_separator() {
397410
return $this->list_item_separator;
398411
}
412+
413+
/**
414+
* Retrieves the localized word count type.
415+
*
416+
* Options are 'characters_excluding_spaces', 'characters_including_spaces or 'words'. Defaults to 'words'.
417+
*
418+
* @since 6.2.0
419+
*
420+
* @return string Localized word count type.
421+
*/
422+
public function get_word_count_type() {
423+
424+
/*
425+
* translators: If your word count is based on single characters (e.g. East Asian characters),
426+
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
427+
* Do not translate into your own language.
428+
*/
429+
$word_count_type = is_null( $this->word_count_type ) ? _x( 'words', 'Word count type. Do not translate!' ) : $this->word_count_type;
430+
431+
// Check for valid types.
432+
if ( 'characters_excluding_spaces' !== $word_count_type && 'characters_including_spaces' !== $word_count_type ) {
433+
// Defaults to 'words'.
434+
$word_count_type = 'words';
435+
}
436+
437+
return $word_count_type;
438+
}
399439
}

src/wp-includes/formatting.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3945,12 +3945,7 @@ function wp_trim_words( $text, $num_words = 55, $more = null ) {
39453945
$text = wp_strip_all_tags( $text );
39463946
$num_words = (int) $num_words;
39473947

3948-
/*
3949-
* translators: If your word count is based on single characters (e.g. East Asian characters),
3950-
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
3951-
* Do not translate into your own language.
3952-
*/
3953-
if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
3948+
if ( str_starts_with( wp_get_word_count_type(), 'characters' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
39543949
$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
39553950
preg_match_all( '/./u', $text, $words_array );
39563951
$words_array = array_slice( $words_array[0], 0, $num_words + 1 );

src/wp-includes/l10n.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,3 +1809,18 @@ function wp_get_list_item_separator() {
18091809

18101810
return $wp_locale->get_list_item_separator();
18111811
}
1812+
1813+
/**
1814+
* Retrieves the word count type based on the locale.
1815+
*
1816+
* @since 6.2.0
1817+
*
1818+
* @global WP_Locale $wp_locale WordPress date and time locale object.
1819+
*
1820+
* @return string Locale-specific word count type.
1821+
*/
1822+
function wp_get_word_count_type() {
1823+
global $wp_locale;
1824+
1825+
return $wp_locale->get_word_count_type();
1826+
}

src/wp-includes/script-loader.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,12 +1831,7 @@ function wp_just_in_time_script_localization() {
18311831
'word-count',
18321832
'wordCountL10n',
18331833
array(
1834-
/*
1835-
* translators: If your word count is based on single characters (e.g. East Asian characters),
1836-
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
1837-
* Do not translate into your own language.
1838-
*/
1839-
'type' => _x( 'words', 'Word count type. Do not translate!' ),
1834+
'type' => wp_get_word_count_type(),
18401835
'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array(),
18411836
)
18421837
);

tests/phpunit/tests/locale.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,62 @@ public function test_is_rtl() {
173173
$this->locale->text_direction = 'ltr';
174174
$this->assertFalse( $this->locale->is_rtl() );
175175
}
176+
177+
/**
178+
* Tests that `WP_Locale::get_word_count_type()` returns
179+
* the appropriate value.
180+
*
181+
* @ticket 56698
182+
*
183+
* @covers WP_Locale::get_word_count_type
184+
*
185+
* @dataProvider data_get_word_count_type
186+
*
187+
* @param string $word_count_type The word count type.
188+
* @param string $expected The expected return value.
189+
*/
190+
public function test_get_word_count_type( $word_count_type, $expected ) {
191+
if ( is_string( $word_count_type ) ) {
192+
$this->locale->word_count_type = $word_count_type;
193+
194+
}
195+
196+
$this->assertSame( $expected, $this->locale->get_word_count_type() );
197+
}
198+
199+
/**
200+
* Data provider.
201+
*
202+
* @return array[]
203+
*/
204+
public function data_get_word_count_type() {
205+
return array(
206+
'default' => array(
207+
'word_count_type' => null,
208+
'expected' => 'words',
209+
),
210+
'empty string' => array(
211+
'word_count_type' => '',
212+
'expected' => 'words',
213+
),
214+
'an invalid option - "foo"' => array(
215+
'word_count_type' => 'foo',
216+
'expected' => 'words',
217+
),
218+
'a valid option - "words"' => array(
219+
'word_count_type' => 'words',
220+
'expected' => 'words',
221+
),
222+
'a valid option - "characters_excluding_spaces"' => array(
223+
'word_count_type' => 'characters_excluding_spaces',
224+
'expected' => 'characters_excluding_spaces',
225+
),
226+
'a valid option - "characters_including_spaces"' => array(
227+
'word_count_type' => 'characters_including_spaces',
228+
'expected' => 'characters_including_spaces',
229+
),
230+
);
231+
}
176232
}
177233

178234
class Custom_WP_Locale extends WP_Locale {

0 commit comments

Comments
 (0)