1

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.

1

Why 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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.