简体   繁体   English

WordPress + Timber + Twig i18n扩展

[英]Wordpress + Timber + Twig i18n extension

Im trying to implement localization to a site Im currently building. 我正在尝试将本地化实施到我当前正在构建的站点。 My goal is to use the i18n Extension for Twig since thats my template engine of choose. 我的目标是使用i18n Extension for Twig,因为那是我选择的模板引擎。

However, when I'm adding the extension the site just crashes, screen gets white, and returning an 500 error code. 但是,当我添加扩展名时,该站点只会崩溃,屏幕变白,并返回500错误代码。 Log says 408 timeout. 日志显示408超时。 In the same function where I'm adding the extension i also adding another extension wich works great. 在添加扩展名的相同功能中,我还添加了另一个扩展名,效果很好。 I have successfully installed the extensions via Composer. 我已经通过Composer成功安装了扩展。 ( https://twig-extensions.readthedocs.io/en/latest/i18n.html ) https://twig-extensions.readthedocs.io/en/latest/i18n.html

What am i doing wrong? 我究竟做错了什么?

Here's my function.php 这是我的function.php

<?php
/**
* Timber starter-theme
* https://github.com/timber/starter-theme
*
* @package  WordPress
* @subpackage  Timber
* @since   Timber 0.1
*/

Timber::$dirname = array( 'templates');
/** Start Timber! */

class StarterSite extends Timber\Site {
    /** Add timber support. */
    public function __construct() {
    add_theme_support( 'post-formats' );
    add_theme_support( 'post-thumbnails' );
    add_filter( 'timber_context', array( $this, 'add_to_context' ) );
    add_filter( 'get_twig', array( $this, 'add_to_twig' ) );
    add_filter('show_admin_bar', '__return_false');
    define( 'WP_DEBUG', true );
    parent::__construct();
}

function add_to_context( $context ) {
    $context['site'] = $this;
    return $context;
}

public function add_to_twig( $twig ) {
    $twig->addExtension( new Twig_Extension_StringLoader() );
    $twig->addExtension( new Twig_Extensions_Extension_I18n() ); // This line breaks the site
    return $twig;
}
}

new StarterSite();

When working with WordPress and Timber, you don't need to use Twig's i18n extension, as stated in the Internationalization Guide in Timber's documentation: 使用WordPress和Timber时,无需使用Twig的i18n扩展名,如Timber文档中的《 国际化指南》所述:

Twig has its own i18n extension that gives you {% trans %} tags to define translatable blocks, but there's no need to use it , because with Timber, you have all you need. Twig有其自己的i18n扩展名,可为您提供{% trans %}标签来定义可翻译的块,但无需使用它 ,因为有了Timber,您便拥有了全部。

This means that in your Twig templates, you can use all the functions that you'd normally use with normal WordPress templates. 这意味着在Twig模板中,您可以使用通常与普通WordPress模板一起使用的所有功能。 Here's an example for a button. 这是一个按钮的示例。

<button>{{ __('Submit', 'my-text-domain') }}</button>

You can set up your theme for internationalization like you would in a normal WordPress theme. 您可以像设置普通WordPress主题一样为国际化设置主题。 Follow the Internationalization section in the Theme Handbook. 遵循主题手册中的“ 国际化”部分。

Set up your style.css with a text domain and a domain path: 使用文本域和域路径设置您的style.css

/**
 * Theme Name: My Theme
 * Author: Theme Author
 * Text Domain: my-text-domain
 * Domain Path: /languages
 */

Then, WordPress will look for a my-text-domain.pot file in the /languages folder of your theme. 然后,WordPress将在主题的/ languages文件夹中查找my-text-domain.pot文件。 You can generate that POT file with Poedit Pro. 您可以使用Poedit Pro生成该POT文件。 Poedit Pro will find the translations in your Twig file. Poedit Pro将在您的Twig文件中找到翻译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM