Uses WP's date_i18n() to localize date shortcode
This commit is contained in:
@@ -2,79 +2,25 @@
|
||||
|
||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||
|
||||
use MailPoet\Newsletter\Shortcodes\ShortcodesHelper;
|
||||
|
||||
class Date {
|
||||
static $translations = array(
|
||||
// l - full textual representation of the day of the week
|
||||
'Monday' => 'Monday',
|
||||
'Tuesday' => 'Tuesday',
|
||||
'Wednesday' => 'Wednesday',
|
||||
'Thursday' => 'Thursday',
|
||||
'Friday' => 'Friday',
|
||||
'Saturday' => 'Saturday',
|
||||
'Sunday' => 'Sunday',
|
||||
// D - textual representation of a day, three letters
|
||||
'Mon' => 'Mon',
|
||||
'Tue' => 'Tue',
|
||||
'Wed' => 'Wed',
|
||||
'Thu' => 'Thu',
|
||||
'Fri' => 'Fri',
|
||||
'Sat' => 'Sat',
|
||||
'Sun' => 'Sun',
|
||||
// F - full textual representation of a month
|
||||
'January' => 'January',
|
||||
'February' => 'February',
|
||||
'March' => 'March',
|
||||
'April' => 'April',
|
||||
'May' => 'May',
|
||||
'June' => 'June',
|
||||
'July' => 'July',
|
||||
'August' => 'August',
|
||||
'September' => 'September',
|
||||
'October' => 'October',
|
||||
'November' => 'November',
|
||||
'December' => 'December',
|
||||
// M - short textual representation of a month, three letters
|
||||
'Jan' => 'Jan',
|
||||
'Feb' => 'Feb',
|
||||
'Mar' => 'Mar',
|
||||
'Apr' => 'Apr',
|
||||
'May' => 'May',
|
||||
'Jun' => 'Jun',
|
||||
'Jul' => 'Jul',
|
||||
'Aug' => 'Aug',
|
||||
'Sep' => 'Sep',
|
||||
'Oct' => 'Oct',
|
||||
'Nov' => 'Nov',
|
||||
'Dec' => 'Dec',
|
||||
// a - lowercase Ante meridiem and Post meridiem
|
||||
'am' => 'am',
|
||||
'pm' => 'pm',
|
||||
// A - uppercase Ante meridiem and Post meridiem
|
||||
'AM' => 'AM',
|
||||
'PM' => 'PM'
|
||||
);
|
||||
|
||||
static function process(
|
||||
$action,
|
||||
$action_argument = false,
|
||||
$action_argument_value = false
|
||||
) {
|
||||
$date = new \DateTime(current_time('mysql'));
|
||||
$action_formats = array(
|
||||
'd' => $date->format('d'),
|
||||
'dordinal' => $date->format('dS'),
|
||||
'dtext' => $date->format('l'),
|
||||
'm' => $date->format('m'),
|
||||
'mtext' => $date->format('F'),
|
||||
'y' => $date->format('Y')
|
||||
$action_mapping = array(
|
||||
'd' => 'd',
|
||||
'dordinal' => 'dS',
|
||||
'dtext' => 'l',
|
||||
'm' => 'm',
|
||||
'mtext' => 'F',
|
||||
'y' => 'Y'
|
||||
);
|
||||
if(!empty($action_formats[$action])) {
|
||||
return ShortcodesHelper::translateShortcode(self::$translations, $action_formats[$action]);
|
||||
if(!empty($action_mapping[$action])) {
|
||||
return date_i18n($action_mapping[$action], current_time('timestamp'));
|
||||
}
|
||||
return ($action === 'custom' && $action_argument === 'format') ?
|
||||
ShortcodesHelper::translateShortcode(self::$translations, $date->format($action_argument_value)) :
|
||||
date_i18n($action_argument_value, current_time('timestamp')) :
|
||||
false;
|
||||
}
|
||||
}
|
@@ -123,17 +123,4 @@ class ShortcodesHelper {
|
||||
);
|
||||
}, $custom_fields);
|
||||
}
|
||||
|
||||
static function translateShortcode($translations, $shortcode) {
|
||||
$translations = self::prepareTranslations($translations);
|
||||
return str_replace(array_keys($translations), array_values($translations), $shortcode);
|
||||
}
|
||||
|
||||
static function prepareTranslations($translations = array()) {
|
||||
$prepared_translations = array();
|
||||
foreach($translations as $key => $translation) {
|
||||
$prepared_translations[$key] = __($translation, 'mailpoet');
|
||||
}
|
||||
return $prepared_translations;
|
||||
}
|
||||
}
|
@@ -33,15 +33,6 @@ class ShortcodesHelperTest extends \MailPoetTest {
|
||||
->equals('[subscriber:cf_' . $custom_field->id . ']');
|
||||
}
|
||||
|
||||
function testItTranslatesShortcodes() {
|
||||
$translations = array(
|
||||
'1' => 'one',
|
||||
'2' => 'two'
|
||||
);
|
||||
$shortcode = '1 & 2';
|
||||
expect(ShortcodesHelper::translateShortcode($translations, $shortcode))->equals('one & two');
|
||||
}
|
||||
|
||||
function _after() {
|
||||
\ORM::raw_execute('TRUNCATE ' . CustomField::$_table);
|
||||
}
|
||||
|
@@ -81,48 +81,14 @@ class ShortcodesTest extends \MailPoetTest {
|
||||
|
||||
function testItCanProcessDateShortcodes() {
|
||||
$date = new \DateTime(current_time('mysql'));
|
||||
expect(Date::process('d'))->equals($date->format('d'));
|
||||
expect(Date::process('dordinal'))->equals($date->format('dS'));
|
||||
expect(Date::process('dtext'))->equals($date->format('l'));
|
||||
expect(Date::process('m'))->equals($date->format('m'));
|
||||
expect(Date::process('mtext'))->equals($date->format('F'));
|
||||
expect(Date::process('y'))->equals($date->format('Y'));
|
||||
expect(Date::process('d'))->equals(date_i18n('d', current_time('timestamp')));
|
||||
expect(Date::process('dordinal'))->equals(date_i18n('dS', current_time('timestamp')));
|
||||
expect(Date::process('dtext'))->equals(date_i18n('l', current_time('timestamp')));
|
||||
expect(Date::process('m'))->equals(date_i18n('m', current_time('timestamp')));
|
||||
expect(Date::process('mtext'))->equals(date_i18n('F', current_time('timestamp')));
|
||||
expect(Date::process('y'))->equals(date_i18n('Y', current_time('timestamp')));
|
||||
// allow custom date formats (http://php.net/manual/en/function.date.php)
|
||||
expect(Date::process('custom', 'format', 'U'))->equals($date->format('U'));
|
||||
}
|
||||
|
||||
function testItTranslatesDateShortcodes() {
|
||||
$date = new \DateTime(current_time('mysql'));
|
||||
$date_class = new Date();
|
||||
|
||||
// custom shortcodes are translated
|
||||
$translatable_custom_shortcodes = array(
|
||||
'l',
|
||||
'D',
|
||||
'F',
|
||||
'M',
|
||||
'a',
|
||||
'A'
|
||||
);
|
||||
foreach($translatable_custom_shortcodes as $translatable_custom_shortcode) {
|
||||
$date_class::$translations = array(
|
||||
$date->format($translatable_custom_shortcode) => $date->format($translatable_custom_shortcode) . '_translated'
|
||||
);
|
||||
expect($date_class::process('custom', 'format', $translatable_custom_shortcode))->equals($date->format($translatable_custom_shortcode) . '_translated');
|
||||
}
|
||||
|
||||
// regular shortcodes are translated
|
||||
$translatable_shortcodes = array(
|
||||
'mtext',
|
||||
'dtext'
|
||||
);
|
||||
foreach($translatable_shortcodes as $translatable_shortcode) {
|
||||
$date_formatted_shortcode = ($translatable_shortcode === 'mtext') ? $date->format('F') : $date->format('l');
|
||||
$date_class::$translations = array(
|
||||
$date_formatted_shortcode => $date_formatted_shortcode . '_translated'
|
||||
);
|
||||
expect($date_class::process($translatable_shortcode))->contains($date_formatted_shortcode . '_translated');
|
||||
}
|
||||
expect(Date::process('custom', 'format', 'U F'))->equals(date_i18n('U F', current_time('timestamp')));
|
||||
}
|
||||
|
||||
function testItCanProcessNewsletterShortcodes() {
|
||||
|
Reference in New Issue
Block a user