phpwordpressinternationalizationwordpress-themingpo

How to translate a WordPress template name?


I know how to create translations for themes and templates generally by generating .po and .mo files with Poedit for example. But since template names are written in PHP comments at top of each template file there is no way to translate this as I see it.

WordPress template header:

/**
 * Template Name: Three columns
 *
 * @package WordPress
 */

Template Name is somehow parsed by Wordpress and used to populate the template select drop-down when creating a page.

So my question is: Is there a way to translate a WordPress template name? Does WordPress also look for any specific variable i can set in my template file? or is it just impossible?


Solution

  • I stumbled upon that quite recently. Here is how I got around this:

    First, add(if you already don't have that) Text Domain: mytext_domain to your style.css, where mytext_domain is the actual text domain for your theme.

    Then add a dummy call to the translating function somewhere in your theme(best thing is to add it just under the Template Name declaration, so you don't wonder why you've put it):

    /**
     * Template Name: Three columns
     *
     * @package WordPress
     */
    __( 'Three columns', 'mytext_domain' );
    

    The reason to do this is because WordPress passes your template name to the translate() function, but since translator plugins parse your code, they are not aware that your template name should be part of the .po(or was it .mo?) file. The dummy call to __() fixes that issue.

    And the reason why you add Text Domain declaration to your style.css is because this is where WordPress looks for your theme's textdomain when it's parsing template names.

    I can't give you exact sources, since I really just poked around the core code, until I figured-out how it works and how to be able to translate my template names.

    PP: I'm not sure how Poedit works - if you add your translations by hand, you might not need the dummy call - just test with and without it and use whichever fits you best :)