Shortcode to translate/format dates to the current locale in Wordpress
We were having issues where using a custom post type with pods would output dates without using date_i18n()
so anything displaying months or weekdays would not translate when used in Beaver Builder Themer templates.
The code is simple, simply add this to your functions.php
of your theme.
function date_locale($atts = [], $content = null, $tag = '') {
$datelocale_atts = shortcode_atts([
'format' => get_option('date_format'),
], $atts, $tag);
return date_i18n($datelocale_atts['format'], strtotime($content));
}
function date_locale_init()
{
add_shortcode('datelocale', 'date_locale');
}
add_action('init', 'date_locale_init');
The usage is simple, you simply have to use the shortcode [datelocale]date to translate[/datelocale]
to translate the date to Wordpress' current set locale (or whatever WPML or other translating plugins set it to).
By default it will use the format of your Wordpress settings, if you want to override it for a specific language, simply add a format
argument with your desired format to change it.
For example when we had to do it from English to French:
[datelocale format="j F Y"]February 21, 2018[/datelocale]
would output 21 Février 2018
.
Apparently it's a known issue and will be resolved soon with an update but in the meantime, this can help you out.