0

I'm trying to add translations to my theme. I went through the documentation and many questions here on Wordpress Development Stack.

Setted a text domain in my style.css file:

/*
Theme Name: My Theme
Text Domain: my-theme
*/

Loaded the text domain in my functions.php file:

add_action( 'after_setup_theme', 'my_theme_load_theme_textdomain' );
function my_theme_load_theme_textdomain() {
    load_theme_textdomain( 'my-theme' );
}

Also tried passing a 2nd argument to load_theme_textdomain():

load_theme_textdomain( 'my-theme', get_template_directory() . '/languages' );

Added _e( 'Featured', 'my-theme' ) in my index.php file. Also tried without the 2nd argument _e( 'Featured' ).

Added both es.mo and es.po files into a languages folder in my theme root directory.

  • languages\es.mo
  • languages\es.po

Also tried naming them with the text domain as a prefix: my-theme-es.mo and my-theme-es.mo.

The content of es.po is as follow:

msgid "Featured"
msgstr "Destacados"

The es.mo file was generated using Poedit free version. Also tried with po2mo.net.

I setted the language to spanish in the General Settings page. Also tried adding define( 'WPLANG', 'es' ) to my wp-config.php file.

But everytime I get "Featured" instead of "Destacados" when the page is rendered.

I'm using WordPress 5.3 on Windows (XAMPP). I checked with phpinfo() that GetText support is enabled.

Any clues?

Thanks for your time.

7
  • 1
    you write _e( 'Featured', ' my-theme' ) with a space before "my-theme", is it a typo ?
    – Kaperto
    Commented Nov 23, 2019 at 23:06
  • Good catch. Yes, it's a typo in the example. The actual code is echo __('Featured', 'gurim'), which uses the real name of the theme.
    – Camilo
    Commented Nov 23, 2019 at 23:07
  • 1
    in the General Settings, spanish code is es_ES. try with filenames es_ES.po and es_ES.mo
    – Kaperto
    Commented Nov 23, 2019 at 23:10
  • @Kaperto You are right, it's es_ES but just tried that and same result. Also removed define( 'WPLANG', 'es' ) from the config file.
    – Camilo
    Commented Nov 23, 2019 at 23:13
  • look that you don't have a file wp-content/languages/themes/my-theme-es_ES.mo.
    – Kaperto
    Commented Nov 23, 2019 at 23:46

1 Answer 1

2

Start a new theme from scratch to see if you have the same result. I test with theses files and the translation works

style.css

/*
Theme Name: My Theme
*/

functions.php

<?php

add_action( 'after_setup_theme', 'my_theme_load_theme_textdomain' );

function my_theme_load_theme_textdomain() {

    load_theme_textdomain( 'my-theme', get_template_directory() . '/languages' );

}

languages/es_ES.po (file languages/es_ES.mo created with poedit)

msgid "Featured"
msgstr "Destacados"

index.php

<?php

_e( 'Featured', 'my-theme' );
1
  • Yes, at the end I didn't need to copy the .mo file to wp-content/languages/themes. Looks like I wasn't properly loading the textdomain. Thank you so much!
    – Camilo
    Commented Nov 24, 2019 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.