I18N: Allow the length of automatically generated excerpts to be localized.

This introduces three new strings that can be used to control the maximum length of automatically generated excerpts for posts, comments, and draft post previews in the dashboard. Optionally combined with the existing word count type control this allows languages which include many multibyte characters to specify more appropriate maximum excerpt lengths.

Props miyauchi, birgire, johnbillion

Fixes #44541


git-svn-id: https://develop.svn.wordpress.org/trunk@45505 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn
2019-06-08 18:41:08 +00:00
parent ea0a34c649
commit 393960b182
7 changed files with 336 additions and 23 deletions

View File

@@ -40,3 +40,30 @@ msgstr "number_format_thousands_sep"
#: wp-includes/script-loader.php:620
msgid "Update %s now"
msgstr "今すぐ %s を更新"
#. 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.
#: wp-includes/formatting.php:3372 wp-includes/script-loader.php:1100
msgctxt "Word count type. Do not translate!"
msgid "words"
msgstr "characters_including_spaces"
#. translators: Maximum number of words used in a post excerpt.
#: wp-includes/formatting.php:3640
msgctxt "excerpt_length"
msgid "55"
msgstr "110"
#. translators: Maximum number of words used in a comment excerpt.
#: wp-includes/comment-template.ph:599
msgctxt "comment_excerpt_length"
msgid "20"
msgstr "40"
#. translators: Maximum number of words used in a preview of a draft on the dashboard.
#: wp-admin/includes/dashboard.php:591
msgctxt "draft_length"
msgid "10"
msgstr "40"

View File

@@ -4,6 +4,14 @@
* @group formatting
*/
class Tests_Formatting_WPTrimWords extends WP_UnitTestCase {
/**
* Long Dummy Text.
*
* @since 5.0.0
*
* @var string $long_text
*/
private $long_text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce varius lacinia vehicula. Etiam sapien risus, ultricies ac posuere eu, convallis sit amet augue. Pellentesque urna massa, lacinia vel iaculis eget, bibendum in mauris. Aenean eleifend pulvinar ligula, a convallis eros gravida non. Suspendisse potenti. Pellentesque et odio tortor. In vulputate pellentesque libero, sed dapibus velit mollis viverra. Pellentesque id urna euismod dolor cursus sagittis.';
function test_trims_to_55_by_default() {
@@ -42,4 +50,27 @@ class Tests_Formatting_WPTrimWords extends WP_UnitTestCase {
$text = 'This is some short text.';
$this->assertEquals( $text, wp_trim_words( $text ) );
}
/**
* @ticket 44541
*/
function test_trims_to_20_counted_by_chars() {
switch_to_locale( 'ja_JP' );
$expected = substr( $this->long_text, 0, 20 ) . '…';
$actual = wp_trim_words( $this->long_text, 20 );
restore_previous_locale();
$this->assertEquals( $expected, $actual );
}
/**
* @ticket 44541
*/
function test_trims_to_20_counted_by_chars_with_double_width_chars() {
switch_to_locale( 'ja_JP' );
$text = str_repeat( 'あ', 100 );
$expected = str_repeat( 'あ', 19 ) . '…';
$actual = wp_trim_words( $text, 19 );
restore_previous_locale();
$this->assertEquals( $expected, $actual );
}
}

View File

@@ -6,6 +6,15 @@
*/
class Tests_L10n extends WP_UnitTestCase {
/**
* Long Dummy Text.
*
* @since 5.0.0
*
* @var string $long_text
*/
private $long_text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
/**
* @ticket 35961
*/
@@ -260,4 +269,251 @@ class Tests_L10n extends WP_UnitTestCase {
$this->assertNotEmpty( $array['Project-Id-Version'] );
$this->assertNotEmpty( $array['X-Generator'] );
}
/**
* @ticket 44541
*/
function test_length_of_excerpt_should_be_counted_by_words() {
global $post;
switch_to_locale( 'en_US' );
$args = array(
'post_content' => $this->long_text,
'post_excerpt' => '',
);
$post = $this->factory()->post->create_and_get( $args );
setup_postdata( $post );
$expect = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat [&hellip;]</p>\n";
the_excerpt();
restore_previous_locale();
$this->expectOutputString( $expect );
}
/**
* @ticket 44541
*/
function test_length_of_excerpt_should_be_counted_by_chars() {
global $post;
switch_to_locale( 'ja_JP' );
$args = array(
'post_content' => $this->long_text,
'post_excerpt' => '',
);
$post = $this->factory()->post->create_and_get( $args );
setup_postdata( $post );
$expect = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore [&hellip;]</p>\n";
the_excerpt();
restore_previous_locale();
$this->expectOutputString( $expect );
}
/**
* @ticket 44541
*/
function test_length_of_excerpt_should_be_counted_by_chars_in_japanese() {
global $post;
switch_to_locale( 'ja_JP' );
$args = array(
'post_content' => str_repeat( 'あ', 200 ),
'post_excerpt' => '',
);
$post = $this->factory()->post->create_and_get( $args );
setup_postdata( $post );
$expect = '<p>' . str_repeat( 'あ', 110 ) . " [&hellip;]</p>\n";
the_excerpt();
restore_previous_locale();
$this->expectOutputString( $expect );
}
/**
* @ticket 44541
*/
function test_length_of_excerpt_rss_should_be_counted_by_words() {
global $post;
switch_to_locale( 'en_US' );
$args = array(
'post_content' => $this->long_text,
'post_excerpt' => '',
);
$post = $this->factory()->post->create_and_get( $args );
setup_postdata( $post );
$expect = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat [&#8230;]';
the_excerpt_rss();
restore_previous_locale();
$this->expectOutputString( $expect );
}
/**
* @ticket 44541
*/
function test_length_of_excerpt_rss_should_be_counted_by_chars() {
global $post;
switch_to_locale( 'ja_JP' );
$args = array(
'post_content' => $this->long_text,
'post_excerpt' => '',
);
$post = $this->factory()->post->create_and_get( $args );
setup_postdata( $post );
$expect = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore [&#8230;]';
the_excerpt_rss();
restore_previous_locale();
$this->expectOutputString( $expect );
}
/**
* @ticket 44541
*/
function test_length_of_draft_should_be_counted_by_words() {
require_once ABSPATH . 'wp-admin/includes/dashboard.php';
switch_to_locale( 'en_US' );
$args = array(
'post_content' => $this->long_text,
'post_excerpt' => '',
'post_status' => 'draft',
);
$this->factory()->post->create( $args );
$expect = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do&hellip;';
wp_dashboard_recent_drafts();
restore_previous_locale();
$this->expectOutputRegex( '/' . $expect . '/' );
}
/**
* @ticket 44541
*/
function test_length_of_draft_should_be_counted_by_chars() {
require_once ABSPATH . 'wp-admin/includes/dashboard.php';
switch_to_locale( 'ja_JP' );
$args = array(
'post_content' => $this->long_text,
'post_excerpt' => '',
'post_status' => 'draft',
);
$post = $this->factory()->post->create( $args );
$expect = 'Lorem ipsum dolor sit amet, consectetur &hellip;';
wp_dashboard_recent_drafts();
restore_previous_locale();
$this->expectOutputRegex( '/' . $expect . '/' );
}
/**
* @ticket 44541
*/
function test_length_of_draft_should_be_counted_by_chars_in_japanese() {
require_once ABSPATH . 'wp-admin/includes/dashboard.php';
switch_to_locale( 'ja_JP' );
$args = array(
'post_content' => str_repeat( 'あ', 200 ),
'post_excerpt' => '',
'post_status' => 'draft',
);
$this->factory()->post->create( $args );
$expect = str_repeat( 'あ', 40 ) . '&hellip;';
wp_dashboard_recent_drafts();
restore_previous_locale();
$this->expectOutputRegex( '/' . $expect . '/' );
}
/**
* @ticket 44541
*/
function test_length_of_comment_excerpt_should_be_counted_by_words() {
switch_to_locale( 'en_US' );
$args = array(
'comment_content' => $this->long_text,
);
$comment_id = $this->factory()->comment->create( $args );
$expect = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut&hellip;';
$comment_excerpt = get_comment_excerpt( $comment_id );
restore_previous_locale();
$this->assertSame( $expect, $comment_excerpt );
}
/**
* @ticket 44541
*/
function test_length_of_comment_excerpt_should_be_counted_by_chars() {
switch_to_locale( 'ja_JP' );
$args = array(
'comment_content' => $this->long_text,
);
$comment_id = $this->factory()->comment->create( $args );
$expect = 'Lorem ipsum dolor sit amet, consectetur &hellip;';
$comment_excerpt = get_comment_excerpt( $comment_id );
restore_previous_locale();
$this->assertSame( $expect, $comment_excerpt );
}
/**
* @ticket 44541
*/
function test_length_of_comment_excerpt_should_be_counted_by_chars_in_Japanese() {
switch_to_locale( 'ja_JP' );
$args = array(
'comment_content' => str_repeat( 'あ', 200 ),
);
$comment_id = $this->factory()->comment->create( $args );
$expect = str_repeat( 'あ', 40 ) . '&hellip;';
$comment_excerpt = get_comment_excerpt( $comment_id );
restore_previous_locale();
$this->assertSame( $expect, $comment_excerpt );
}
}