Translating
Templates
It's possible to use php in handlebars templates to translate strings. This is useful when you want to translate static strings that are not in the WordPress translation files.
Note that at this point you don't have access to the datas, meaning you can't use the this
keyword in PHP, or count the number of results for example.
Usage in handlebars template
<?php
/**
* Template .hbs.php
*/
?>
{{#each hits}}
<div class="result">
<h3>{{{ this.post_title }}}</h3>
<p>{{{ this._formatted.post_content }}}</p>
<a href="{{this.permalink}}" >
<?php echo esc_html_x( 'View More', 'results loop item', 'domain' ); ?>
</a>
</div>
{{/each}}
The template is passed to the render later, with the results' data.
Template translations alternative
Using the findstr_translations
filter, you can add translations for your templates.
add_filter(
'findstr_translations',
function ( $translations ) {
$translations['main_search'] = array(
'link_view_more' => _x( 'View more', 'search items loop', 'your_translation_domain' ),
);
return $translations;
}
);
Usage template :
{{#each hits}}
<div class="result">
<h3>{{{ this.post_title }}}</h3>
<p>{{{ this._formatted.post_content }}}</p>
<a href="{{this.permalink}}" >
{{../translations.main_search.link_view_more}}
</a>
</div>
{{/each}}
Fields
It's possible to translate fields values, using the findstr_translations
filter.
add_filter(
'findstr_translations',
function ( $translations ) {
//cf/my_custom_field is the field source
$translations['cf/my_custom_field'] = array( 'original_value' => _x( 'Original value', 'my-text-domain' ) );
return $translations;
}
);