diff --git a/lib/Newsletter/Shortcodes/Categories/Date.php b/lib/Newsletter/Shortcodes/Categories/Date.php index f86e185bfd..196e947a69 100644 --- a/lib/Newsletter/Shortcodes/Categories/Date.php +++ b/lib/Newsletter/Shortcodes/Categories/Date.php @@ -1,7 +1,61 @@ '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, @@ -17,10 +71,10 @@ class Date { 'y' => $date->format('Y') ); if(!empty($action_formats[$action])) { - return $action_formats[$action]; + return ShortcodesHelper::translateShortcode(self::$translations, $action_formats[$action]); } return ($action === 'custom' && $action_argument === 'format') ? - $date->format($action_argument_value) : + ShortcodesHelper::translateShortcode(self::$translations, $date->format($action_argument_value)) : false; } } \ No newline at end of file diff --git a/lib/Newsletter/Shortcodes/ShortcodesHelper.php b/lib/Newsletter/Shortcodes/ShortcodesHelper.php index eb8aee78dc..ed2daa6558 100644 --- a/lib/Newsletter/Shortcodes/ShortcodesHelper.php +++ b/lib/Newsletter/Shortcodes/ShortcodesHelper.php @@ -1,4 +1,5 @@ $custom_field->name, 'shortcode' => '[subscriber:cf_' . $custom_field->id . ']' ); }, $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; + } } \ No newline at end of file diff --git a/tests/unit/Newsletter/ShortcodesHelperTest.php b/tests/unit/Newsletter/ShortcodesHelperTest.php index 3026c778d8..7f8126680f 100644 --- a/tests/unit/Newsletter/ShortcodesHelperTest.php +++ b/tests/unit/Newsletter/ShortcodesHelperTest.php @@ -5,7 +5,7 @@ use MailPoet\Models\CustomField; use MailPoet\Newsletter\Shortcodes\ShortcodesHelper; class ShortcodesHelperTest extends \MailPoetTest { - function testItCanGetShortcodes() { + function testGetsShortcodes() { $shortcodes = ShortcodesHelper::getShortcodes(); expect(array_keys($shortcodes))->equals( array( @@ -18,7 +18,7 @@ class ShortcodesHelperTest extends \MailPoetTest { ); } - function testItCanGetCustomShortShortcodes() { + function testItGetsCustomShortShortcodes() { $shortcodes = ShortcodesHelper::getShortcodes(); expect(count($shortcodes['Subscriber']))->equals(5); $custom_field = CustomField::create(); @@ -33,6 +33,15 @@ 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); } diff --git a/tests/unit/Newsletter/ShortcodesTest.php b/tests/unit/Newsletter/ShortcodesTest.php index 7db8bb6a4d..759bbf62a1 100644 --- a/tests/unit/Newsletter/ShortcodesTest.php +++ b/tests/unit/Newsletter/ShortcodesTest.php @@ -91,6 +91,40 @@ class ShortcodesTest extends \MailPoetTest { expect(Date::process('custom', 'format', 'U'))->equals($date->format('U')); } + function testItTranslatesDateShortcodes() { + $date = new \DateTime('now'); + $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'); + } + } + function testItCanProcessNewsletterShortcodes() { $shortcodes_object = $this->shortcodes_object; $content = @@ -109,7 +143,6 @@ class ShortcodesTest extends \MailPoetTest { expect($result['0'])->equals($wp_post->post_title); } - function itCanProcessPostNotificationNewsletterNumberShortcode() { // create first post notification $post_notification_history = $this->_createNewsletter( @@ -365,4 +398,5 @@ class ShortcodesTest extends \MailPoetTest { wp_delete_post($this->WP_post, true); wp_delete_user($this->WP_user->ID); } + } \ No newline at end of file