2

I can't get my translations to work in my theme. Here is what I have

In functions.php, I have:

load_theme_textdomain( 'transparent', get_template_directory_uri() .'/languages' );

I have

.../theme/languages/transparent-en_US.po,

which contains:

# English translations for Transparent package.
# Copyright (C) 2014 Steven Doig
# This file is distributed under the same license as the Transparent package.
# Automatically generated, 2012.
#
msgid ""
msgstr ""
"Project-Id-Version: my-pluginname 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-08-06 13:46-0400\n"
"PO-Revision-Date: 2013-03-21 11:20+0400\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"X-Poedit-SourceCharset: iso-8859-1\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 1.5.4\n"


#: header.php:58
msgid "thanks-for-visiting"
msgstr "Hello!  Thank you for visiting.  Take a look around and subscribe to the "

#: header.php:60
msgid "subscribe-by-email"
msgstr "subscribe by email"

#: header.php:62
msid "for-updates"
msgstr "for updates."

In header.php, from line 58 I have:

<p><?php echo __("thanks-for-visiting", 'transparent'); ?><a href="<?php bloginfo('rss2_url'); ?>" title="<?php bloginfo('name'); ?> RSS feed">RSS feed</a> 
    <?php if (of_get_option('feedburner', '') !== '') {?>
        or <a href="http://feedburner.google.com/fb/a/mailverify?uri=<?php echo of_get_option('feedburner', 'no entry');?>&amp;loc=en_US" target="_blank"><?php echo __("subscribe-by-email", 'transparent'); ?></a> 
    <?php } ?>
    <?php echo __("for-updates", 'transparent'); ?></p>

In my style.css I have the following

/*
Theme Name: Transparent
Theme URI: http://websitetechnology.com.au/category/themes/transparent/
Description: Transparent responsive two-column theme.
Tags: green, two-columns, right-sidebar
Author:Steven Doig
Author URI:http://tech.doig.com.au
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Version:1.2
*/

However, on the front end, I see:

thanks-for-visitingRSS feed for-updates

Can you see why this translation is not working?

1
  • Just checking in to see if your problem is resolved. If it is using my solution, please accepting my answer if my solution helped, if not, let me know Mar 17, 2014 at 10:42

3 Answers 3

14

After inspecting your theme, this is what I came up with. I've taken the liberty to include Ruben's solution as well to make this answer as complete as possible.

There are several problems here:

Number 1. load_theme_textdomain( 'transparent', get_template_directory_uri() .'/languages' );

should be:

    function transparent_theme_setup() {
    load_theme_textdomain( 'transparent', get_template_directory() . '/languages' );

    $locale = get_locale();
    $locale_file = get_template_directory() . "/languages/$locale.php";

    if ( is_readable( $locale_file ) ) {
        require_once( $locale_file );
    }
}
add_action( 'after_setup_theme', 'transparent_theme_setup' );

Language files must always be called in the after_setup_theme hook.

Also get_template_directory_uri()

should be

get_template_directory().

Number 2. <?php echo __("thanks-for-visiting", 'transparent'); ?>.

is totally wrong. Firstly, never ever use " in a translation string. The translator doesn't recognize it and will skip that string. Always use '. Secondly, don't echo a string with echo. _e should be used in this case. So your string should look like this: <?php _e('thanks-for-visiting', 'transparent'); ?>.

Number 3. There is no text domain set in your stylesheet's header. Add Text Domain: transparent just above the closing */ in your header. So your header should look like this:

/*
Theme Name: Transparent
Theme URI: http://websitetechnology.com.au/category/themes/transparent/
Description: Transparent responsive two-column theme.
Tags: green, two-columns, right-sidebar
Author:Steven Doig
Author URI:http://tech.doig.com.au
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Version:1.2
Text Domain: transparent
*/

Number 4. The naming for your po file is wrong, so should your mo file be. somename-en_US.po and somename-en_US.mo is only used by plugins. Themes use only the filenames en_US.po and en_US.mo

Number 5. Your po file is wrongly created. To create a po file with poedit, follow the following steps. Please note that my poedit is in Afrikaans.

Open poedit and select the option to create a catalogue. The following screen will appear

image1.

Fill in the required fields as shown.

Now click "Sources paths" tab and complete as shown. Use . in the "Paths" field if your language file is in the main directory, and use .. if the language file is in a folder like language

image2

Now click the "Sources keywords" tab and fill in as shown.

image3

Now save the poedit file as en_US.po in your languages file. It is important that you save the po file directly in the location where it must be, otherwise it will not work. Poedit will now search and add all strings for translation to your po file. A mo file is automatically created when the po file is saved.

At the end, your po header should look like this

 msgid ""
msgstr ""
"Project-Id-Version: Transparent v1.2\n"
"POT-Creation-Date: 2014-03-12 16:25+0200\n"
"PO-Revision-Date: 2014-03-12 16:28+0200\n"
"Last-Translator: Pieter Goosen <[email protected]>\n"
"Language-Team: Transparent <[email protected]>\n"
"Language: en\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.5.5\n"
"X-Poedit-KeywordsList: gettext;gettext_noop;__;_e;_n:1,2;_x:1,2c;_ex:1,2c\n"
"X-Poedit-Basepath: .\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-SearchPath-0: ..\n"

For further reading, check this tutorial. It really helped me when I started on translations. Hope this helps.

1
  • In my case this was the essential part: Language files must always be called in the after_setup_theme hook. I used to set them up in init but it doesn't work anymore. Feb 17, 2016 at 11:58
3

Don't use get_template_directory_uri(). Use get_template_directory() instead. Your code should look like this:

    load_theme_textdomain( 'transparent', get_template_directory() . '/languages' );

    $locale = get_locale();
    $locale_file = get_template_directory() . "/languages/$locale.php";

    if ( is_readable( $locale_file ) ) {
        require_once( $locale_file );
    }
6
  • Thanks @Ruben. I made this change but the issue remains.
    – Steve
    Mar 11, 2014 at 2:18
  • 1
    1. Make sure that you add that code inside a after_setup_theme filter. 2. Try using this plugin, it may show if there's an error with your .po/.mo files: wordpress.org/plugins/codestyling-localization Mar 11, 2014 at 7:16
  • 1
    1. I added the code above into a function called transparent_theme_setup(), and then called add_action( 'after_setup_theme', 'transparent_theme_setup' );. Is this what you mean? 2. Codestyling plugin shows there's no error with .po/.mo files.
    – Steve
    Mar 12, 2014 at 5:44
  • @Steve, that is correct. Mar 12, 2014 at 6:15
  • Ok. Well, the issue remained after I did that.
    – Steve
    Mar 13, 2014 at 7:22
1

When using load_theme_textdomain your language filename must be named based on the locale exactly. Name it en_US.mo instead of transparent-en_US.mo

Read the instructions carefully at: https://codex.wordpress.org/Function_Reference/load_theme_textdomain

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.