Date formatting problem
-
when I call the frontend to show today’s date, it is not translated to “bg-BG”, it is shown to EN, but when it has to show another date, the translation is normal?
-
This topic was modified 1 year, 6 months ago by
Steven Stern (sterndata). Reason: removed nsfw
The page I need help with: [log in to see the link]
-
This topic was modified 1 year, 6 months ago by
-
1. Check WordPress Locale Settings
WordPress uses the locale (
bg-BG) to determine how dates (and other content) are displayed. First, make sure the locale is set correctly in WordPress.- Go to Settings > General in the WordPress admin panel.
- Check the Site Language field and ensure that it is set to Bulgarian (
bg-BG).
If the language is correct, WordPress should be translating dates properly, but some custom code or plugins may override this.
2. Ensure Proper Locale for PHP
date()orstrftime()If you’re using PHP to display the date (
date()orstrftime()), you need to make sure that PHP is set to use the correct locale. You can set the locale with thesetlocale()function in PHP.Try adding the following code to your theme’s
functions.phpfile to ensure the correct locale is set for date-related functions:if (function_exists('setlocale')) {
setlocale(LC_TIME, 'bg_BG.UTF-8');
}This ensures that PHP will use the Bulgarian locale (
bg_BG.UTF-8) when formatting dates.3. WordPress Date Functions
If you are using WordPress date functions like
get_the_date(), WordPress usually handles the locale automatically based on your site’s language settings. However, in some cases, you may need to force the translation.Try using the
get_date_from_gmt()function and ensuring that the correct locale is set before calling it:$today = current_time('Y-m-d'); // or 'U' for timestamp
echo date_i18n(get_option('date_format'), strtotime($today));Here,
date_i18n()is a WordPress function that will respect the locale and display the date according to your language settings.4. JavaScript Date Locale
If the date is being rendered via JavaScript (e.g., using
new Date()), you’ll need to make sure that JavaScript is using the correct locale when displaying the date.For JavaScript, the
toLocaleDateString()method should be used to format the date according to the desired locale. Here’s an example:const today = new Date();
const options = { year: 'numeric', month: 'long', day: 'numeric' };
const formattedDate = today.toLocaleDateString('bg-BG', options);
document.getElementById('date').textContent = formattedDate;In this example, the
toLocaleDateString()function ensures that the date is displayed in Bulgarian format.
5. Custom Date Formatting in Theme
For example, if you’re using custom code like this:echo date('l, F j, Y');You can replace it with WordPress’s
date_i18n()function to ensure proper translation:echo date_i18n('l, F j, Y');6. Multilingual Plugins (WPML, Polylang, etc.)
If you’re using a multilingual plugin like WPML or Polylang, ensure that:
- The correct language is selected for the user viewing the page.
- The date formatting is set correctly within the plugin settings.
The problem could be caused by the current bug regarding the language files of themes. See: https://core.trac.ww.wp.xz.cn/ticket/62337
I was unable to resolve the issue that is in the “generic” Theme!
I created a separate site with a sub-domain, in which there is only wordpress and the theme, no plugins.
test.divine-age.com
When I call today’s date in functions.php, via “echo date(‘l, F j, Y’);”, the translation is in Bulgarian, but when I use a function created in functions.php for a shortcode by which I call today’s date, it is in english. The shortcode function is:function displayTodaysDate( $atts )
{
return date(get_option(‘date_format’));
}
add_shortcode( ‘datetoday’, ‘displayTodaysDate’ );
where the translation is in English!
I am not good with codes and programming for which I apologize and Please help!-
This reply was modified 1 year, 6 months ago by
gels13.
Hello,
Here’s a solution to fix that:
- Set the locale to Bulgarian: You can use the
setlocale()function to ensure that the date is displayed in the correct language. - Modify your shortcode function: Update the
displayTodaysDatefunction to set the locale for Bulgarian before generating the date.
Here’s an updated version of your
functions.phpcode:function displayTodaysDate( $atts )
{
// Set the locale to Bulgarian (Bulgarian locale may vary depending on your server setup)
setlocale(LC_TIME, 'bg_BG.UTF-8', 'bg_BG', 'bg', 'bulgarian');
// Return the date in the desired format
return strftime('%A, %B %d, %Y');
}
add_shortcode( 'datetoday', 'displayTodaysDate' );Key points:
setlocale(): This function is used to set the current locale. We’re setting it to Bulgarian (bg_BG.UTF-8). The other variants (bg_BG,bg,bulgarian) are used as fallbacks in case one of them isn’t available on the server.strftime(): This function formats the date according to the locale set bysetlocale(). It will output the date in Bulgarian. The format'%A, %B %d, %Y'is similar to the format you wanted (l, F j, Y), but it works withstrftime().
Things to check:
- Locale availability: Ensure that the Bulgarian locale (
bg_BG.UTF-8) is available on your server. If it’s not, you might need to install or enable it on your server, or use a different format that doesn’t depend onsetlocale(). - WordPress language settings: Make sure that your WordPress language is set to Bulgarian in Settings > General. This helps ensure that WordPress uses the correct translation for dates, numbers, and other locale-specific data.
With this modification, when you use the
[datetoday]shortcode, the date should be displayed in Bulgarian, as long as the correct locale is supported on your server.Thank you very much, you were able to help me with your guidance!
Thank you!
The topic ‘Date formatting problem’ is closed to new replies.