When using gettext in Wordpress to create a bilingual theme, do we have to create one .php file for every page twice (English, French)? For example, header.php, header-fr.php, sidebar.php, sidebar-fr.php, taxonomy-types.php, taxonomy-types-fr.php, etc. Sidebar-fr.php would then use _e('Text to translate', 'domain'). Is there a way to simply keep one copy of all php files and to switch the locale?
Thank you.
Create languages folder inside the theme directory, that's where you save language files, e.g. ar.po (for Arabic).
When writing strings in the code use the following (I usually use English text as the default text, so I won't need to create extra language file, and for each other language create files):
<?php echo __('This is a test','my_theme_name'); ?>
<?php _e('This is a test','my_theme_name'); ?>
in functions.php in the theme folder add the following code to load the language file (please note my_theme_name as given above):
add_action( 'after_setup_theme', 'my_theme_setup' );
function my_theme_setup(){
load_theme_textdomain('my_theme_name', get_template_directory() . '/languages');
}
This way you create only one file(header.php, index.php, etc), where strings are easy to translate.
[Note: this answer assumes that you have knowledge in how to create gettext translation files]
Related
I am trying to add a translation to a plugin I wrote from scratch but after experimenting a while I'm not sure how to make Wordpress aware that there's a translation available for my plugin.
I'm not sure what else to try so I thought that someone more experienced could point out things I might need to change.
What I did so far:
Added either _e() or __() to the sentences I want to be translated in my plugin files.
Used Loco Translate plugin to generate a .pot file.
Opened .pot file in Poedit, (it displayed a list of all the strings I wanted to translate) translated plugin and generated .po and .mo files from it.
Moved .pot, .po and .mo to my-plugin/languages/.
Renamed files to my-plugin-pt.po and my-plugin-pt.mo.
Changed Wordpress site language to translated language. Language changed everywhere else but the plugin still renders in english.
Not really sure what to do next.
I've created a method that runs load_plugin_textdomain() while following these instructions from Wordpress and have added it as an action to my-plugin __construct():
my-plugin.php
public function __construct() {
// Other filters and actions...
add_action( 'plugins_loaded', array( $this, 'translation_init' ) );
} // __construct
function translation_init() {
load_plugin_textdomain( 'my-plugin', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
The language code at the end of the .po and .mo files was incorrect.
Changed from -pt.po to -pt_PT.po (did the same for .mo file) and it started working perfectly.
There's a list of the available language codes for Wordpress which I should have had a look before naming the files in the first place.
I am programming a custom template in Joomla 3.3. I need to override some language strings, so I have created a folder called language/templates/mytemplate/language/es-ES/ and inside it, I placed a file called es-ES.custom.ini.
In template index.php file I placed:
<?php
$language =& JFactory::getLanguage();
$result = $language->load('custom' , dirname(__FILE__), $language->getTag(), true);
echo $result;
?>
$result variable is true indicating the file is correctly loaded, but the strings are not actually overridden.
Any help will be appreciated.
If you enable Language Debugging in your admin area:
you'll notice that even if your custom language file is loaded, other language files are loaded later, overriding your strings.
A different approach would be to create a file called JROOT/templates/YOURTEMPLATE/language/overrides/es-ES.override.ini. Then add the following to your templateDetails.xml:
<languages folder="language">
...
<language tag="overrides">overrides/es-ES.override.ini</language>
</languages>
This is described more in detail here.
i have a problem.I install stitch theme on wordpress and i want to translate this theme from english to persian.I create fa_IR.mo and fa_IR.po and replace in languages directory of stitch theme.Now add this bottom code to functions.php and header.php :
<?php load_theme_textdomain( 'stitch',get_template_directory().'/languages/fa_IR.mo'); ?>
but my theme still english!!!!Please help me.Where i wrong!!
Thanks alot.
I believe that the stich theme have already included a language file configuration, so you don't have to. BTW, your code should go into functions.php, and the fa_IR.mo part is not needed
All you have to do is, in your wp-config.php, look for this line
define( 'WPLANG', '' );
And change it to
define( 'WPLANG', 'fa_IR' );
This will set your language to persian
Try to use Codestyling Localization - https://wordpress.org/plugins/codestyling-localization/.
You can check if your po/mo files are in proper location, scan your template/plugins for new words for translation, etc. Very useful.
I've created an Arabic website and I am using the zippy theme from MageeWP the things is that I am trying to translate some strings in the theme, but I don't seem to get it working well.
In the theme functions file, there is a specification for the folder location for .PO and .MO files
I created a file ar.mo and put it there, but it still not working, can anybody guide to a clue?
I checked in wp_config.php and indeed the WPLAND is set to ar
I also used the CodeStyling plugin to make the translation, and it didn't work, although the plugin does not problem a .po or .mo files for ONLY ar.
Additionally
the source code of the zippy theme is here: http://themes.svn.wordpress.org/zippy/1.0.9/
Looks like the theme is doing it wrong (simplified code):
define( 'ZIPPY_THEME_BASE_URL', get_template_directory_uri() );
$lang = ZIPPY_THEME_BASE_URL. '/lang';
load_theme_textdomain( 'zippy', $lang );
It's passing an URL and it should be a path, change the load_theme_textdomain line to:
load_theme_textdomain( 'zippy', get_template_directory() . '/lang');
I had a look at the Zippy theme. There are a template called en_US.po inside the lang folder. I had a look at that as well.
If you don't have poedit installed, download poedit now and install it on your computer. Now, make a copy of en_US.po and rename it ar.po. Open ar.po with poedit. Now you can do all your translations in this template. When you're done, just click save in poedit. Poedit will automatically create a ar.mo template when your ar.po is saved.
Note, this is just a quick way of doing it, as there are already a language file available that isn't yet translated.
Hope this helps
I've just built my first plugin for WordPress, and even if it's not a great "code poetry" it works as it should. It's a plugin that transform the default wp gallery using the GalleryView 3.0 jQuery plugin (http://spaceforaname.com/galleryview).
The only thing I'm not able to do is localization.
Localization for this plugin in means translating the admin interface, where someone can configure the jQuery plugin options to change the aspect of the resulting gallery.
I've tried to follow the millions of tutorials present on the web, read a lot of posts about this issue on forums and followed the guidelinees of codex... but still with no luck.
this is what I've done:
every text line is inside a gettext function ( __ and _e )
using poedit I created the .po and .mo file scanning the plugin directory (everything went ok), then i added translations on that file.
I named the .po file like that NAME-OF-THE-PLUGIN-it_IT.po (the .mo file was generated with the same name)
I've put the translations files inside the plugin folder/languages (name of the folder is the same of the plugin and of the translations files)
then I've tried to add the load_plugin_textdomain function inside the main plugin file. I've tried because there's no way to get it working.
The only thing on which I'm not sure is the fact that the plugin I've created is not under a class+constructor functions... just because I'm still not so good in coding.
But I've put the load_plugin_textdomain inside an init add_action, like this:
add_action('init', 'gw_load_translation_file');
function gw_load_translation_file() {
// relative path to WP_PLUGIN_DIR where the translation files will sit:
$plugin_path = dirname(plugin_basename( __FILE__ ) .'/languages' );
load_plugin_textdomain( 'gallery-view-for-wordpress', false, $plugin_path );
}
the lines above are not inside a logic, they are just in the main plugin file, like that.
this is an example of my use of gettext functions:
<h3><?php _e('Panel Options','gallery-view-for-wordpress') ?></h3>
What did I not understand?
My mistake was on language files path declaration.
this fixed:
$plugin_path = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
this was wrong:
$plugin_path = dirname(plugin_basename( __FILE__ ) .'/languages' );
I was answered on Wordpress Stack Exchange
Contrary to my previous statement, I've succeeded using this filename: gallery-view-for-wp-it_IT.mo
Strange, though - themes use just it_IT.mo.