我正在考虑在wordpress子主题中添加可本地化的字符串,并且无法成功完成此操作。
我有一个带有.php页面的子主题,我想在其中添加一个可本地化的字符串。在我的孩子主题 var table = $('#myTable').DataTable();
$('#btn').click(function() {
table.row.add( [
'new 1',
'new 2'
]);
var currentPage = table.page();
table.page(currentPage).draw(false);
});
中,我添加了以下内容:
functions.php
接下来,使用Loco Translate,我将文件load_theme_textdomain( 'i-craft-child', get_template_directory() . '/languages' );
和de_DE.po
上传到子主题目录中的目录de_DE.mo
。
最后,我将以下行添加到我的html页面:
/languages
但是,上面的<span><small>><?php _e( 'Your email address is also your username and it cannot be changed', 'i-craft-child' ); ?></small></span>
以英语显示(而不是德语)。我不知道在本地化过程中我在哪里失败了,并且不胜感激任何指针来解决这个问题。
答案 0 :(得分:2)
确保包含您的主题文字域,您可以使用类似的内容(如果文件不存在,则会破坏您的网站,因此请在测试环境中使用它。)
$loaded = load_child_theme_textdomain( 'i-craft-child', get_template_directory() . '/languages' );
if( ! $loaded ) {
echo 'Unable to load files';
die;
}
另外,您是否在WP_LANG
中指定了wp-config.php
,就像这样?
define('WP_LANG', 'de_DE'); // for example
答案 1 :(得分:2)
跟上上面unixarmy的指针,问题是函数load_child_theme_textdomain
无法读取文件,因为我使用函数get_template_directory()
作为参数。 get_template_directory()
将返回父主题的路径,而不是子主题。将其替换为get_stylesheet_directory()
解决了问题。
答案 2 :(得分:0)
我在Loco的高级配置中遇到了这个问题:
主题->儿童主题名称->高级。
在此区域内,您可以定义翻译主题时将使用的“文本域”,例如:
<?php _e('My string','generatepress-child'); ?>
generatepress-child是文本域。
在我的情况下,子主题和父主题合并在同一路径(在父路径上)和loco无法“获取/扫描”子主题中的字符串,因此我手动添加了新子主题这个高级部分中的路径,现在我可以翻译子主题中的字符串了。
此后,我必须在我的functions.php父主题上添加以下行:
function wpdocs_child_theme_setup() {
load_child_theme_textdomain( 'generatepress-child', get_stylesheet_directory() );
}
add_action( 'after_setup_theme', 'wpdocs_child_theme_setup' );
希望有帮助!