Skip to content

Commit

Permalink
Merge pull request timber#2215 from timber/translations
Browse files Browse the repository at this point in the history
Make strings in time_ago() translatable
  • Loading branch information
jarednova authored Apr 14, 2020
2 parents 28356bc + b325566 commit 8df0dc9
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 7 deletions.
32 changes: 26 additions & 6 deletions lib/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function add_timber_filters( $twig ) {

$twig->addFilter(new Twig_Filter('pluck', array('Timber\Helper', 'pluck')));

/**
/**
* @deprecated since 1.13 (to be removed in 2.0). Use Twig's native filter filter instead
* @todo remove this in 2.x so that filter merely passes to Twig's filter without any modification
* @ticket #1594 #2120
Expand Down Expand Up @@ -353,13 +353,33 @@ public function intl_date( $date, $format = null ) {
}

/**
* @param int|string $from
* @param int|string $to
* @param string $format_past
* @param string $format_future
* Returns the difference between two times in a human readable format.
*
* Differentiates between past and future dates.
*
* @see \human_time_diff()
*
* @param int|string $from Base date as a timestamp or a date string.
* @param int|string $to Optional. Date to calculate difference to as a timestamp or
* a date string. Default to current time.
* @param string $format_past Optional. String to use for past dates. To be used with
* `sprintf()`. Default `%s ago`.
* @param string $format_future Optional. String to use for future dates. To be used with
* `sprintf()`. Default `%s from now`.
*
* @return string
*/
public static function time_ago( $from, $to = null, $format_past = '%s ago', $format_future = '%s from now' ) {
public static function time_ago( $from, $to = null, $format_past = null, $format_future = null ) {
if ( null === $format_past ) {
/* translators: %s: Human-readable time difference. */
$format_past = __( '%s ago' );
}

if ( null === $format_future ) {
/* translators: %s: Human-readable time difference. */
$format_future = __( '%s from now' );
}

$to = $to === null ? time() : $to;
$to = is_int($to) ? $to : strtotime($to);
$from = is_int($from) ? $from : strtotime($from);
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ _Twig is the template language powering Timber; if you need a little background
= Develop (next release) =

**Fixes and improvements**

* Allows for translation of time_ago Twig filter #2214 #2215 (thanks @gchtr)

**Changes for Theme Developers**

Expand Down
109 changes: 109 additions & 0 deletions tests/Timber_UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,113 @@ function clearPosts() {
$wpdb->query("TRUNCATE TABLE $wpdb->posts;");
}

/**
* Installs a translation.
*
* Deletes already installed translation first.
*
* @param string $locale The locale to install.
*/
public static function install_translation( $locale ) {
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );

self::uninstall_translation( $locale );
wp_download_language_pack( $locale );
}

/**
* Deletes translation files for a locale.
*
* Logic borrowed from WP CLI Language Command
*
* @link https://github.com/wp-cli/language-command/blob/master/src/Core_Language_Command.php
*
* @param string $locale The locale to delete.
*
* @return bool Whether the locale was deleted.
*/
public static function uninstall_translation( $locale ) {
global $wp_filesystem;

$available = wp_get_installed_translations( 'core' );
$translations = array_keys( $available['default'] );

if ( ! in_array( $locale, $translations, true ) ) {
return false;
}

$files = scandir( WP_LANG_DIR );

if ( ! $files ) {
return false;
}

$current_locale = get_locale();
if ( $locale === $current_locale ) {
// Language is active.
return true;
}

// As of WP 4.0, no API for deleting a language pack
WP_Filesystem();
$deleted = false;
foreach ( $files as $file ) {

if ( '.' === $file[0] || is_dir( $file ) ) {
continue;
}

$extension_length = strlen( $locale ) + 4;
$ending = substr( $file, -$extension_length );
$starting = substr( $file, 0, strlen( $locale ) );

if ( ! in_array( $file, [ $locale . '.po', $locale . '.mo' ], true )
&& ! in_array( $ending, [ '-' . $locale . '.po', '-' . $locale . '.mo' ], true )
&& $locale !== $starting
) {
continue;
}

/** @var WP_Filesystem_Base $wp_filesystem */
$deleted = $wp_filesystem->delete( trailingslashit( WP_LANG_DIR ) . $file );
}

if ( $deleted ) {
return true;
}

return false;
}

/**
* Changes to a different locale.
*
* The translations for the locale you might want to use maybe don’t exist yet. You will
* have to download it first through wp_download_language_pack(). Check the bootstrap.php
* file to see how it works.
*
* After you used this function in a test, don’t forget to restore the current locale using
* $this->restore_locale().
*
* @see \Timber_UnitTestCase::restore_locale()
*
* @param string $locale The locale to switch to.
*/
function change_locale( $locale ) {
// Check if the translation is already installed.
if ( ! in_array( $locale, get_available_languages() ) ) {
self::install_translation( $locale );
}

switch_to_locale( $locale );
}

/**
* Restores the locale after it was changed by $this->change_locale().
*
* @see \Timber_UnitTestCase::change_locale()
*/
function restore_locale() {
restore_current_locale();
}
}
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ function is_post_type_viewable( $post_type_object ) {
return $post_type_object->publicly_queryable || ( $post_type_object->_builtin && $post_type_object->public );
}
}

// Make sure translations are installed.
Timber_UnitTestCase::install_translation( 'de_DE' );
30 changes: 30 additions & 0 deletions tests/test-timber-dates.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,36 @@ function testTimeAgoPast(){
$this->assertEquals('1 day ago', $str);
}

function testTimeAgoFutureTranslated() {
if ( version_compare( get_bloginfo( 'version' ), 5, '>=' ) ) {
$this->change_locale( 'de_DE' );

$str = Timber\Twig::time_ago( '2016-12-01 20:00:00', '2016-11-30, 20:00:00' );
$this->assertEquals( '1 Tag ab jetzt', $str );

$this->restore_locale();

return;
}

$this->markTestSkipped( 'The string `%s from now` is not available in this WordPress version' );
}

function testTimeAgoPastTranslated() {
if ( version_compare( get_bloginfo( 'version' ), 5, '>=' ) ) {
$this->change_locale( 'de_DE' );

$str = Timber\Twig::time_ago( '2016-11-29 20:00:00', '2016-11-30, 20:00:00' );
$this->assertEquals( 'vor 1 Tag', $str );

$this->restore_locale();

return;
}

$this->markTestSkipped( 'The string `%s ago` is not available in this WordPress version' );
}

function testTime(){
$pid = $this->factory->post->create(array('post_date' => '2016-07-07 20:03:00'));
$post = new TimberPost($pid);
Expand Down
1 change: 1 addition & 0 deletions tests/test-timber-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function testStandardThemeLocation() {
}

function testLanguageAttributes() {
$this->restore_locale();
$site = new TimberSite();
$lang = $site->language_attributes();
$this->assertEquals('lang="en-US"', $lang);
Expand Down

0 comments on commit 8df0dc9

Please sign in to comment.