My wordpress website has two languages. The theme translates properly, but whenever I use get_the_date
it is in the default english language. How do I force the use of another language?
The locale filter does not work.
My wordpress website has two languages. The theme translates properly, but whenever I use get_the_date
it is in the default english language. How do I force the use of another language?
The locale filter does not work.
2 Answers
Reset to default 1Why not do it in plain PHP?
For example, you can write a function like:
function get_the_german_date($date = '', $post = null) {
$d = get_the_date($date, $post);
if ($d) {
$d = new DateTime($d);
return $d->format('d.m.Y');
} else return false;
}
Similary, if you want to follow the WordPress standards:
function the_german_date($date = '', $post = null) {
$d = get_the_date($date, $post);
if ($d) {
$d = new DateTime($d);
echo $d->format('d.m.Y');
}
}
I realize this may be too much work since you want to do it dynamically depending on your locale, but you can do a check of your locale, or you can have a global date function where if website is in english you output get_the_date
and if not the function above.
I found the solution:
add_action('after_setup_theme', function() {
switch_to_locale(get_current_language());
add_filter('locale', function() {
return get_current_language() ? get_current_language() : "en_US";
});
load_theme_textdomain('default', get_template_directory() . '/languages');
});
The sequence is quite important, that the locale filter is defined before load_theme_textdomain
and switch_to_locale
is called before defining the filter.
get_current_language
is a function I wrote, which defined the language from the url.