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.0n"
"Report-Msgid-Bugs-To: n"
"POT-Creation-Date: 2012-08-06 13:46-0400n"
"PO-Revision-Date: 2013-03-21 11:20+0400n"
"Last-Translator: Automatically generatedn"
"Language-Team: nonen"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=utf-8n"
"X-Poedit-SourceCharset: iso-8859-1n"
"Content-Transfer-Encoding: 8bitn"
"Plural-Forms: nplurals=2; plural=(n != 1);n"
"X-Generator: Poedit 1.5.4n"
#: 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');?>&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?
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:
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’techo
a string withecho
._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:Number 4. The naming for your po file is wrong, so should your mo file be.
somename-en_US.po
andsomename-en_US.mo
is only used by plugins. Themes use only the filenamesen_US.po
anden_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
.
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 languageNow click the “Sources keywords” tab and fill in as shown.
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
For further reading, check this tutorial. It really helped me when I started on translations. Hope this helps.
Don’t use get_template_directory_uri(). Use get_template_directory() instead. Your code should look like this:
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