Wordpress - 木材 - 本地化失败

时间:2016-02-03 12:51:33

标签: php wordpress twig wordpress-theming timber

我无法让本地化/翻译功能正常运行。

我使用以下组件:

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" );
}
.
.
.

/templates/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"

为了调试问题,我转储了翻译。结果如下: enter image description here

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

enter image description here

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

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

1 个答案:

答案 0 :(得分: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/