admin管理员组

文章数量:1122832

I'm trying to set up language translations based on the Wordpress language setting. I feel like I've done all the steps outlined in / but my translations are returning the base translation.

I have a /languages folder with a .pot file that, among other things contain this:

#: header.php:26
msgid "Skip to content"
msgstr ""

In the same folder there's a .po file called {text_domain}-{locale}.po that contains

#: header.php:26
msgid "Skip to content"
msgstr "Skip to content but a different language"

I've opened the .po file with poedit and it's compiled a .mo file inside the same folder

my style.css file includes:

Text Domain: my-theme
Domain Path: /languages

Inside functions.php I've added the snippet

function my_theme_load_theme_textdomain() {
    load_theme_textdomain(_TEXT_DOMAIN, get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'my_theme_load_theme_textdomain' );

On a product page I'm running

echo get_locale(); // The correct locale string
echo _TEXT_DOMAIN; // The correct text domain
e_('Skip to content', _TEXT_DOMAIN) // The wrong string: "Skip to content"

Can anyone figure out what I'm doing wrong?

I'm trying to set up language translations based on the Wordpress language setting. I feel like I've done all the steps outlined in https://developer.wordpress.org/themes/functionality/internationalization/ but my translations are returning the base translation.

I have a /languages folder with a .pot file that, among other things contain this:

#: header.php:26
msgid "Skip to content"
msgstr ""

In the same folder there's a .po file called {text_domain}-{locale}.po that contains

#: header.php:26
msgid "Skip to content"
msgstr "Skip to content but a different language"

I've opened the .po file with poedit and it's compiled a .mo file inside the same folder

my style.css file includes:

Text Domain: my-theme
Domain Path: /languages

Inside functions.php I've added the snippet

function my_theme_load_theme_textdomain() {
    load_theme_textdomain(_TEXT_DOMAIN, get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'my_theme_load_theme_textdomain' );

On a product page I'm running

echo get_locale(); // The correct locale string
echo _TEXT_DOMAIN; // The correct text domain
e_('Skip to content', _TEXT_DOMAIN) // The wrong string: "Skip to content"

Can anyone figure out what I'm doing wrong?

Share Improve this question asked Sep 13, 2024 at 13:55 FluxianFluxian 1809 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

The issue was that for themes the .mo and .po files needs to be called {locale}.mo or .po

not {textDomain}-{locale}.mo/po

本文标签: phpi18n in Wordpress Theme