admin管理员组文章数量:1290949
Inside of a plugin, I'm using the gettext filter to alter strings of a theme so that I don't have to copy the template files over to a child theme and edit them. Also, this isn't always possible since many of the strings are inside class definitions in the theme setup files. Here is my code:
function ctf_filter_gettext($translated, $original, $domain) {
// This is an array of original strings
// and what they should be replaced with
$strings = array(
'Howdy, %1$s' => 'Greetings, %1$s!', //wordpress core
'Apply for Job' => 'Reply by Email', //theme specific
// Add some more strings here
);
// See if the current string is in the $strings array
// If so, replace it's translation
if (isset($strings[$original])) {
// This accomplishes the same thing as __()
// but without running it through the filter again
$translations = &get_translations_for_domain($domain);
$translated = $translations->translate($strings[$original]);
}
return $translated;
}
add_filter('gettext', 'ctf_filter_gettext', 99, 3);
Inside of the theme there is this line, for example:
__('Apply for Job','jobify')
You can see in the above code that I want to change that to 'Reply by Email'
But this doesn't work.
The other line in the code (Howdy...) is part of Wordpress core, and that DOES work. In fact, I can override any other plugin's translatable text or anything from Wordpress core with the above code.
What would cause this particular theme to ignore my string translations? Note, it doesn't matter if the priority of the filter is 1, 99, or 9999, it still doesn't work.
Inside of a plugin, I'm using the gettext filter to alter strings of a theme so that I don't have to copy the template files over to a child theme and edit them. Also, this isn't always possible since many of the strings are inside class definitions in the theme setup files. Here is my code:
function ctf_filter_gettext($translated, $original, $domain) {
// This is an array of original strings
// and what they should be replaced with
$strings = array(
'Howdy, %1$s' => 'Greetings, %1$s!', //wordpress core
'Apply for Job' => 'Reply by Email', //theme specific
// Add some more strings here
);
// See if the current string is in the $strings array
// If so, replace it's translation
if (isset($strings[$original])) {
// This accomplishes the same thing as __()
// but without running it through the filter again
$translations = &get_translations_for_domain($domain);
$translated = $translations->translate($strings[$original]);
}
return $translated;
}
add_filter('gettext', 'ctf_filter_gettext', 99, 3);
Inside of the theme there is this line, for example:
__('Apply for Job','jobify')
You can see in the above code that I want to change that to 'Reply by Email'
But this doesn't work.
The other line in the code (Howdy...) is part of Wordpress core, and that DOES work. In fact, I can override any other plugin's translatable text or anything from Wordpress core with the above code.
What would cause this particular theme to ignore my string translations? Note, it doesn't matter if the priority of the filter is 1, 99, or 9999, it still doesn't work.
Share Improve this question asked Aug 17, 2015 at 16:59 user658182user658182 6252 gold badges14 silver badges35 bronze badges1 Answer
Reset to default 2Filtering gettext
is horrible for performance. I would strongly recommend to try do it via any other approach. And if all else fails to add and remove filter with extreme precision around necessary parts.
As for why it fails my educated guess would be — timing. It is quite common for strings to be centralized and processed early in the process. For example WP's own labels for post types and taxonomies are processed like that.
It might be that by the time you have added your filter, the theme had already generated string a stashed away a final copy somewhere to make use of.
本文标签: translationWhat would cause the gettext filter to not work for a given text domain
版权声明:本文标题:translation - What would cause the gettext filter to not work for a given text domain? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741506448a2382349.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论