Localization of WP theme

I am working on WP theme localization. On custom search template I have function which gets text of some search terms and replace them with other (just visually).

function search_replace_search() {
$search_term = esc_attr( apply_filters( 'the_search_query', get_search_query( false ) ) ); // Get search term
$search = array('word1', 'word2' );
$replace = array('something 1', 'something 2' );
$replacePairs = array_combine($search, $replace);
echo strtr($search_term, $replacePairs); 
} 

echo search_replace_search();

How to wrap search orreplace words, or function output (echo), so WP will use translation of words (in this example "something 1" and "something 2") from theme's .mo files. I 've tried various ways to wrap this into gettext, without success.

Topic xgettext localization translation Wordpress search

Category Web


Wrap the elements of $search and $replace arrays into __() function to get the translated strings:

function search_replace_search() {
    $search_term = esc_attr( apply_filters( 'the_search_query', get_search_query( false ) ) ); // Get search term

    $search = array(
        __('word1'),
        __('word2')
    );

    $replace = array(
        __('something 1'),
        __('something 2')
    );

    $replacePairs = array_combine($search, $replace);
    echo strtr($search_term, $replacePairs); 
} 

echo search_replace_search();

If you use your own textdomain:

    $search = array(
        __('word1', 'my_textdomain'),
        __('word2', 'my_textdomain')
    );

    $replace = array(
        __('something 1', 'my_textdomain'),
        __('something 2', 'my_textdomain')
    );

More information:

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.