最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

translation - How to translate functions like get_the_date()?

programmeradmin0浏览0评论

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.

Share Improve this question asked Oct 22, 2019 at 17:43 Кристиян КацаровКристиян Кацаров 662 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论