Localization of a wordpress theme without a textdomain - wordpress

I have a wordpress theme without textdomain (i.e. e(x) and not e(x,domain)). I also have the .po and .mo files in a folder under /themes/My Theme/localization (Notice the space name in the my theme). I would like to activate fr_FR. I created fr_FR.po and .mo and changed the wp-config to add the locale for fr_FR. However, I am still not getting the french to work. I saw many sites telling you to add a load_theme_textdomain at the top of functions.php, but I do not know what would my textdomain be. Any help will be appreciated.
Youssef

To get theme localization working, you're going to need to go through your theme and add a domain to every _e() and __() function call. this:
_e('some text');
__('some other text');
Will have to become this:
_e('some text', 'your-domain');
__('some other text', 'your-domain');
Next you'll need to add this bit of code at the top of your functions.php file:
load_theme_textdomain( 'your-domain', TEMPLATEPATH.'/localization' );
$locale = get_locale();
$locale_file = TEMPLATEPATH."/localization/$locale.php";
if (is_readable($locale_file))
require_once($locale_file);
You can read more about it in this post.

Add your own text domain. I did this recently to a theme which was not designed for localization, so I'm posting what I did.
Add this to functions.php
load_theme_textdomain( 'your-domain', TEMPLATEPATH.'/languages' );
where your-domain can be any name, but keep it uniform throughout all theme files.
Now go through all the theme PHP files, and do the following:
If you see _e('some text') then change it to _e('some text', 'your-domain');
If you see __('some text') then change it to __('some text', 'your-domain');
If you see "some text" without __() or _e() then,
If "some text" is used in a function call, then make it __() like above, including the text domain
If "some text" is just printed and not part of any function call, surrround it with a _e() like shown above, and don't forget the text domain.
Read the Wordpress internationalization and localization guide for more information.

After an unbelievably long string of forums going through the same steps of how to set it up when everything is working correctly, I finally found what was causing the issue for me.
If the server sets the global $locale before wordpress has a bash at it, then wordpress uses the server's locale settings (in the wp-includes/l10n.php file, the function get_locale).
The solution I used, is to set the global $locale right next to defining WPLANG...
global $locale;
$locale = 'am_AM';
define('WPLANG', $locale);

Related

How to make custom Wordpress plugin aware of .po and .mo files?

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.

Translate plugin and wordpress and the same time

I have seen many questions and answers but none that fit the bill. So here is my issue.
I have a plugin that I have localized which works fine on it's own when you add
define('WPLANG', 'my-plugin-name-de_DE');
to the wp-config file. But then when someone try's to translate the rest of the wp site it will not show the languange.
What am I missing here? I even tried naming the files I added the the wp-content folder to be the same name as my plugin... for example my-plugin-name-de_DE.po and .mo and no luck.
ANSWER
Ok so in wordpress 4.0 you can now select your language from the settings /general page.
My plugin was localized using the code
function plugin_action_init()
{
// Localization
load_plugin_textdomain('my-plugin-name', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
// Add actions
add_action('init', 'plugin_action_init');
Example of a string that would get translated.
_e('Simple text example', 'my-plugin-name');
SO all that needed to be done to get my plugin's and wordpress's translation to work at the same time was to upload the wp language that I want to the wp-content/languages folder. Making sure the file name was simply de_DE.mo and de_DE.po
Such a simple mistake :)

how do i change the woocommerce_sale_flash part on a multilanguage site?

I'm building a multilingual site with WP using Woocommerce.
The client asked me to change the Sale Flash text, after some research i found this code
add_filter('woocommerce_sale_flash', 'avia_change_sale_content', 10, 3);
function avia_change_sale_content($content, $post, $product){
$content = '<span class="onsale">'.__( 'Sale custom text!', 'woocommerce' ).'</span>';
return $content;
}
It works just fine, but it also changes the german and spanish text - replacing it with the english one.
I checked at the WPML strings (hoping to translate it through that system), but couldn't find it there.
Does anyone know what filter i would have to set to create custom flash sale text in english, spanish and german
Thanks
You are replacing the text correctly, however the issue is that the new string is not found in the language files provided by WooCommerce. The plugin will load a domain called "woocommerce" which is then used to locate strings and their translations if necessary. You are passing the text into __() (more info on __()', '_e(), _x(), _n()), but because your text is new, it isn't found in the dictionary provided by WooCommerce so it defaults to English.
// the __() function can't find 'Sale custom text' in the 'woocommerce' text domain,
// so it does not translate.
__( 'Sale custom text!', 'woocommerce' )
To have this text translate, you will need to load your own language files and text domain and pass the text through __() using your custom domain.
The short version of how to do this:
Use a custom text domain
// load text from 'my_text_domain'
__( 'Sale custom text!', 'my_text_domain' )
Download and install PoEdit to create *.po language files. It has options to scan PHP files for translations entries and will add them to your dictionary. There are tutorials on this on the Internet.
Create one translation file per language that you want to support
Create a folder called languages under your theme directory (this should be a Child Theme so that updates don't overwrite your work), or plugin directory if you are doing your customizations that way.
Update your functions.php (or other plugin, etc) to load your custom text domain. Make sure to use the appropriate path, this assumes it's in the theme.
function load_textdomain_my_text_domain() {
$language_dir = get_template_directory() . '/languages/';
load_plugin_textdomain( 'my_text_domain', false, $language_dir );
}
add_action( 'plugins_loaded', 'load_textdomain_my_text_domain' );
An example of doing this in a plugin using one I wrote:
Spanish language files create by PoEdit under the /languages directory.
Loading the language files under a custom text domain.
Text being passed through __() using the custom text domain.

Trouble Translating a WordPress theme

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

Wordpress gettext bilingual language switching for a theme

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]

Resources