0

我无法让本地化/翻译功能正常工作。

我使用以下组件:

Wordpress 4.4.2
Timber -> Twig

该模板工作正常。只是本地化似乎不起作用。

在我的研究过程中,我在 github 上看到了这篇文章,这似乎是不完整的,因为他们没有说明如何设置它...... https://github.com/jarednova/timber/wiki/Text-Cookbook#internationalization

我最终使用了以下代码 - 由于某种原因它不起作用:

/functions.php

class StarterSite extends TimberSite {

function __construct() {
    add_theme_support( 'post-formats' );
    add_theme_support( 'post-thumbnails' );
    add_theme_support( 'menus' );
    add_theme_support( 'widgets' );
    add_filter( 'timber_context', array( $this, 'add_to_context' ) );
    add_filter( 'get_twig', array( $this, 'add_to_twig' ) );
    add_action( 'init', array( $this, 'register_post_types' ) );
    add_action( 'init', array( $this, 'register_taxonomies' ) );

    $this->register_theme_locals();
    parent::__construct();
}

function register_theme_locals(){
    $path = get_template_directory() . '/languages';
    $result = load_theme_textdomain('panther', $path );

    if ( $result )
        return;

    $locale = apply_filters( 'theme_locale', get_locale(), 'panther' );
    die( "Could not find $path/$locale.mo" );
}
.
.
.

/模板/base.twig

<button type="submit" class="btn btn-info">
    <i class="fa fa-search"></i> {{__('Search', 'panther')}}
</button>

/languages/en_US.po

msgid ""
msgstr ""
"Project-Id-Version: Panther 0.1.20150828\n"
"Report-Msgid-Bugs-To: http://webklex.com/support/theme/panther\n"
"POT-Creation-Date: 2015-11-20 12:58:54+00:00\n"
"PO-Revision-Date: Mon Feb 01 2016 21:29:54 GMT+0100 (CET)\n"
"Last-Translator: admin <info@webklex.com>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: English\n"
"Plural-Forms: nplurals=2; plural=n != 1\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-Basepath: .\n"
"X-Poedit-SearchPath-0: ..\n"
"X-Poedit-KeywordsList: _:1;gettext:1;dgettext:2;ngettext:1,2;dngettext:2,3;"
"__:1;_e:1;_c:1;_n:1,2;_n_noop:1,2;_nc:1,2;__ngettext:1,2;__ngettext_noop:1,2;"
"_x:1,2c;_ex:1,2c;_nx:1,2,4c;_nx_noop:1,2,3c;_n_js:1,2;_nx_js:1,2,3c;"
"esc_attr__:1;esc_html__:1;esc_attr_e:1;esc_html_e:1;esc_attr_x:1,2c;"
"esc_html_x:1,2c;comments_number_link:2,3;t:1;st:1;trans:1;transChoice:1,2\n"
"X-Loco-Target-Locale: en_US\n"
"X-Generator: Loco - https://localise.biz/"

#: templates/base.twig:72
msgctxt "submit button"
msgid "Search"
msgstr "Search EN"

为了调试问题,我转储了翻译。结果如下: 在此处输入图像描述

如果我渲染视图/页面,我仍然会得到未翻译的字符串:

在此处输入图像描述

我确实检查了是否加载了正确的语言,是的。我什至检查了是否调用了正确的翻译函数(不是 NOOP 函数),是的,即使调用了正确的函数..

我有点迷失在这里。因此,如果有人知道我做错了什么或发生了什么,请告诉我。

4

1 回答 1

0

只需简单的方法:

function panther_theme_setup() {
    load_theme_textdomain('panther', get_template_directory() . '/languages');
}
add_action( 'after_setup_theme', 'panther_theme_setup' ); //this is recommended

而对于编辑/更新语言文件,我个人只使用“LOCO Translate”插件,从那时起我就不需要其他程序来搞乱了。

资料来源:我,但也在这里:https ://developer.wordpress.org/reference/functions/load_theme_textdomain/

于 2019-09-13T14:53:00.330 回答