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
The code itself is very simple:
- Add a filter
- Create the translation function
- Inside the translation function, you use str_ireplace with the not-translated input and the translated output for every string you need
- 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; }
WordPress Translate Strings Functions.php
Make sure to check out my other WordPress articles.