Advertisement

Introduction

If you are living in a non-english speaking country like me, or you create a website for a customer in a country like that you probably need to translate the website to the country-specific language. When you are using the content management system WordPress for example, maybe there are some translation options shipped with the theme you are using. If there are some missing or even not available, there is a simple possibility to translate certain strings. I recently had this case with the Blog overview page and the Posts page. There you have strings like “Author”, “Post Comment”, “Log Out” and so on. WordPress Translate Strings Functions.php

Edit functions.php for Translations

In order to use that method, I strongly recommend you to create a child theme in WordPress, thus you are safe to update everything. Otherwise, when you are working without a child theme, you might lose your changes after an theme update if the file gets overwritten. For more information about WordPress child themes I suggest you to check out the documentation at WordPress.org

Advertisement

The code itself is very simple:

  1. Add a filter
  2. Create the translation function
  3. Inside the translation function, you use str_ireplace with the not-translated input and the translated output for every string you need
  4. You return the translated string
add_filter('gettext', 'translate_my_text' );
function translate_my_text($translated) {
    $translated = str_ireplace('Leave a comment on: “%s”', 'Kommentar zu “%s” verfassen', $translated);
    $translated = str_ireplace('Leave a Comment', 'Kommentar verfassen', $translated);
    return $translated;
}
After that, save the functions.php inside your child theme folder, clear your page cache if you have a plugin enabled and refresh the page to see the results.
WordPress Translate Strings Functions.php
Make sure to check out my other WordPress articles.
Advertisement
Previous articleWooCommerce: Show Custom Text for Products with no Price
Next articleWindows: Spot Disk Usage with TreeSize

LEAVE A REPLY

Please enter your comment!
Please enter your name here