Using variables with WordPress translation functions

Today I wanted to be able to pass a custom field variable to the WordPress localization (l10n) functions. The custom field is a select field, so all the possible outputs are known.

WordPress uses PO and MO files for translations. These are generated by scanning the site for __() and _e() functions, with a programme such as Poedit, to generate the list of translatable strings.

Using variables in these functions, like _e($output, 'text-domain'), causes a problem. When the scan is done, there is no string to add to the PO file.

In my case, because the outputs are known, I created a file called manual-translations.php, included it from my functions.php file and added the translatable strings like:

__( 'String one', 'text-domain' );
__( 'Second string', 'text-domain' );
__( 'Stringy cheese', 'text-domain' );

Now when the site is scanned it picks these up and I can now add the translations in Poedit.

Hope that helps someone out there!

WordPress SEO: 10 tips for better search rankings

FREE E-book. Learn the basics of WordPress SEO and how to improve the key metrics
  • We won't sell your address to anyone. We hate spam too.
View my portfolio Get in touch

One Response to “Using variables with WordPress translation functions”

  1. Luca

    This was a good help for e thank you. The special use case isn’t generally covered in articles about l18n.


Leave a Reply