Luis Delgado
Luis Delgado

Reputation: 3734

How to localize child theme with Loco Translate

I am getting my head around adding localizable strings to a wordpress child theme, and have not been able to do this successfully.

I have a child theme with a .php page to which I want to add a localizable string. In my child theme's functions.php, I have added the following line:

load_theme_textdomain( 'i-craft-child', get_template_directory() . '/languages' );

Next, using Loco Translate, I uploaded the files de_DE.po and de_DE.mo to the directory /languages within the child theme directory.

Finally, I added the following line to my html page:

<span><small>><?php _e( 'Your email address is also your username and it cannot be changed', 'i-craft-child' ); ?></small></span>

However, the span above is displayed in English (instead of German). I am not sure where in the localization process am I failing and would appreciate any pointers to solve this problem.

Upvotes: 0

Views: 2431

Answers (3)

gtamborero
gtamborero

Reputation: 3202

I achieved that problem going to the Advanced configuration inside Loco:

Themes -> Child Theme Name -> Advanced.

Inside this area you can define the 'text domain' you will use when translating your theme, for example:

<?php _e('My string','generatepress-child'); ?>

Where generatepress-child is the text domain.

In my case the child theme and parent theme where merging on the same path (on the parent path) and loco wasn't able to 'fetch / scan' the strings inside the child theme, so I added manually the new child path inside this advanced section and now I can translate the strings inside my child theme.

After this I have had to add this lines on my functions.php parent theme:

function wpdocs_child_theme_setup() {
   load_child_theme_textdomain( 'generatepress-child', get_stylesheet_directory()  );
}
add_action( 'after_setup_theme', 'wpdocs_child_theme_setup' );

Hope it helps!

Upvotes: 0

Luis Delgado
Luis Delgado

Reputation: 3734

Following up on the pointer from unixarmy above, the problem was that the function load_child_theme_textdomain was not able to read the files, due to me using the function get_template_directory() as a parameter. get_template_directory() will return the path of the parent theme, not the child. Substituting that for get_stylesheet_directory() solved the problem.

Upvotes: 2

Kristian Vitozev
Kristian Vitozev

Reputation: 5971

Make sure that your theme text-domain is included, you can use something like this (if file doesn't exists, it will break your site, so use it on test environment).

$loaded = load_child_theme_textdomain( 'i-craft-child', get_template_directory() . '/languages' );

if( ! $loaded ) {  
    echo 'Unable to load files';
    die;
}

Also, did you specify WP_LANG in wp-config.php, like this?

define('WP_LANG', 'de_DE'); // for example

Upvotes: 2

Related Questions