Adds helper method to translate shortcodes
Adds translations to Date shortcode
This commit is contained in:
@@ -1,7 +1,61 @@
|
||||
<?php
|
||||
|
||||
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,
|
||||
@@ -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;
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Newsletter\Shortcodes;
|
||||
|
||||
use MailPoet\Models\CustomField;
|
||||
@@ -115,11 +116,24 @@ class ShortcodesHelper {
|
||||
static function getCustomFields() {
|
||||
$custom_fields = CustomField::findMany();
|
||||
if(!$custom_fields) return false;
|
||||
return array_map(function ($custom_field) {
|
||||
return array_map(function($custom_field) {
|
||||
return array(
|
||||
'text' => $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;
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user