仅仅创建一个子主题就足够了吗?比如说,从技术上来说,除了最简单的样式之外,没有添加任何其他内容。css-让父主题的翻译也自动用于子主题?
基本上,答案是否定的。。。but... 有一个选项:
添加mu插件
这个(MU-)插件做了几件事:
它与after_setup_theme 优先级为20-假设父textdomain/i18n。mo文件以正确挂钩上的默认优先级正确加载然后它检索instanceof 这个WP_Theme - 在本例中是子主题然后它检查是否确实有子主题在使用如果这是真的,那么它将只从父文件加载textdomain这其实很简单,因为核心类为我们做了很多检查:它检索WP_Theme 对于父主题。然后检查TextDomain 标题设置,使用:$current_theme->get( \'TextDomain\' );. 因此,关键是有一个convention 进入游戏:如果父主题得到Text Domain 和(!)A.Domain Path 标题集。
<?php
/**
* Plugin Name: (#113391) Parent Theme i18n Autoloader
* Description: Load Twenty12 Child theme translation files automagically from Parent
*/
add_action( \'after_setup_theme\', \'wpse113391_parent_theme_i18n_autoloader\', 20 );
function wpse113391_parent_theme_i18n_autoloader()
{
$current_theme = wp_get_theme();
if ( is_child_theme() )
$current_theme->parent()->load_textdomain();
}
现在问题来了:核心交付的默认/标准二十*主题不(!)拥有
Domain Path 标题条目。这是我们必须做的
fix 立即,作为
load_theme_textdomain() else不在父主题文件夹中搜索翻译文件,但
子主题文件夹中的第一个:get_stylesheet_directory().WP_Theme::get( \'DomainPath\' ), 这意味着(A)Domain Path 需要设置and 它需要以斜杠作为前缀:/. 然后在子主题文件夹中:` get\\u stylesheet\\u directory()\'/“语言”最后一个WP_LANGUAGE_DIR.\'/themes\' 目录
注意:我想这只是一个永远不会为“向后兼容性”修复的bug,换句话说,这意味着有一个bug,但可能已经有开发人员在解决它了:P然后还有另一个问题。这个WP_Theme 类方法load_textdomain() 内部通过a$path 到load_theme_textdomain(). 这个参数是$this->get_stylesheet_directory(). 此方法返回$this->theme_root . \'/\' . $this->stylesheet. 所以这个函数实际上工作得很好,but 只需调用内部替换get_stylesheet_directory() (可过滤)。人们现在可能会想
“嘿!全班同学implements ArrayAccess! 所以只需设置缺少的数组键Domain Path!“”
Wrong. 所有类属性都已标记private 无法访问。
那么你可能会想
“为什么不简单extend 这个WP_Theme 类并定义set() 方法,以便手动设置缺少的标头条目?“”
Wrong. 类本身是final 并且不可扩展。
Result: 我们还剩下什么load_theme_textdomain() - 调用链中的最后一个函数为我们提供了。现在我们有了一个更大的插件,可以拦截load_theme_textdomain() 调用以加载正确的文件。为了不干扰其他i18n文件加载,它会立即从过滤器中删除回调以保持环境整洁。
<?php
/**
* Plugin Name: (#113391) Parent Theme i18n Autoloader
* Description: Load Twenty12 Child theme translation files automagically from Parent
*/
add_action( \'muplugins_loaded\', array( \'WPSE113391Parenti18nLoader\', \'getInstance\' ) );
class WPSE113391Parenti18nLoader
{
public static $instance = null;
private $theme = null;
public static function getInstance()
{
null === self::$instance AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
add_action( \'after_setup_theme\', array( $this, \'i18nAutoloader\' ), 20 );
}
public function setTheme( $theme )
{
return $this->theme = $theme;
}
public function getTheme()
{
return $this->theme;
}
public function i18nAutoloader()
{
if ( ! is_child_theme() )
return;
$current_theme = wp_get_theme();
if ( \'\' === $current_theme->parent()->get( \'DomainPath\' ) )
{
$this->setTheme( $current_theme->parent() );
add_filter( \'override_load_textdomain\', array( $this, \'overrideI18nLoader\' ), 10, 3 );
}
$current_theme->parent()->load_textdomain();
}
public function overrideI18nLoader( $activate, $domain, $mofile )
{
// Don\'t intercept anything else: Self removing
remove_filter( current_filter(), __FUNCTION__ );
// Rebuild the internals of WP_Theme::get_stylesheet_directory() and load_theme_textdomain()
$theme = $this->getTheme();
$path = trailingslashit( $theme->get_theme_root() ).$theme->get_template();
$locale = apply_filters( \'theme_locale\', get_locale(), $domain );
load_textdomain( $domain, "{$path}/{$locale}.mo" );
// Return true to abort further attempts
return true;
}
}