Search code examples
wordpresslocalizationthemeschild-theming

Translatable strings from child theme not included in translation files


So I think that I have all the configuration for translation set, but my translatable strings are not loaded to .po or .pot.

In functions.php:

function opportune_child_setup() {
    load_child_theme_textdomain( 'opportune', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'opportune_child_setup' );

In style.css:

/*
[...]
Text Domain: opportune
*/

Translatable string (example):

<label for="custom_field"><?php _e( "Company Tax ID", 'opportune' ) ?></label>

Translation files directory:

themes/opportune-child/languages/opportune.pot
themes/opportune-child/languages/pt_PT.mo
themes/opportune-child/languages/pt_PT.po

The .po and .mo files were created with Poedit based on opportune.pot in the parent theme (which is located in equivalent directory:)

themes/opportune/languages/opportune.pot

I've even hardcoded in my wp-config.php (although already set in WP Admin):

define('WP_LANG', 'pt_PT');

What I do to see if the string has been loaded to either .po or .pot is: I go to a page with translatable strings, make hard refresh (deleting cache), download .po and .pot and then search for the string. None of the strings have ever been loaded.

I've used Loco Translate plugin, I think with the right configurations, and still no result.

What am I missing? Thank you so much!


Solution

  • Firstly, child themes should not use the same text domain as the parent theme.

    If you're only using strings from the parent theme, then just translate the parent. But if you're adding new strings into your own PHP files, then use a separate text domain.

    So if "Company Tax ID" is a string you've added and it isn't in the parent, ensure it's in a text domain matching the child theme folder, i.e. "opportune-child".


    Secondly, strings aren't automatically "loaded into po files" as you say. You create your .pot file by extracting strings from your PHP files. Then you base your .po files from that. Read about WordPress internationalization to understand this process.

    Don't create files "based on opportune.pot" because the parent theme is a separate entity. You only need to extract and load strings you've defined for your child. Let the parent take care of itself.


    There is a guide for translating child themes here: https://localise.biz/wordpress/plugin/child-themes

    Full disclosure: I am the author.