Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to localize my plugin and with a plugin called CodeStyling Localization, I succeeded to create a .po file. However, the words used in my plugin remain in English although the default language is set to Japanese as defined in wp-configdefine('WPLANG', 'ja');

Other localized plugins are properly translated including, Yet Another Related Posts Plugin and WP Multibyte Patch.

This is a sample plugin I wrote to test plugin localization. I kept it as simple as possible.

wp-content\plugins\localizationsample\localizationsample.php

<?php
/*  
    Plugin Name: Localization Sample Plugin
    Version: 1.1
    Author: Teno
*/

// Localization
add_action('init', 'localizationsample_init');
function localizationsample_init() {
    $loaded = load_plugin_textdomain('localizationsample', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/');
} 

// Add Admin Menu 
add_action('admin_menu','localizationsample_menu');
function localizationsample_menu() { 
    add_options_page(
        'Localization Sample Plugin',   
        'Localization Sample Plugin',       
        'manage_options',       
        __FILE__,               
        'localizationsample_adminpanel'
    );
}
function localizationsample_adminpanel() {
    echo '<div class="wrap"><div id="icon-themes" class="icon32"></div>';
    echo '<h2>' . __('hi!', 'localizationsample') . '</h2>';    // "hi!" should be translated.
    echo '</div></div>';
}
?>

wp-content\plugins\localizationsample\lang\LSample-ja_JP.po

msgid ""
msgstr ""
"Project-Id-Version: Localization Sample Plugin v1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-08-17 20:23+0900\n"
"PO-Revision-Date: 2012-08-17 11:27:39+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Poedit-Language: \n"
"X-Poedit-Country: \n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-KeywordsList: __;_e;__ngettext:1,2;_n:1,2;__ngettext_noop:1,2;_n_noop:1,2;_c,_nc:4c,1,2;_x:1,2c;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;\n"
"X-Poedit-Basepath: .\n"
"X-Poedit-Bookmarks: \n"
"X-Poedit-SearchPath-0: Z:/xampp/htdocs/wptest/wp-content/plugins/localizationsample\n"
"X-Textdomain-Support: yes"

#: localizationsample.php:28
#@ LSample
msgid "hi!"
msgstr "やあ!"

Both files are saved as UTF-8 without BOM. What am I doing wrong?

*updated the code but the issue still persists.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

First make sure the language file is found. In your development environment (and only there!) add the following code:

$path   = plugin_dir_path( __FILE__ ) . 'lang';
$loaded = load_plugin_textdomain( 'LSample', false, dirname( $path );

if ( ! $loaded )
{
    print "File not found: $path"; 
    exit;
}

Then … I would use lowercase text domains only. Not sure if that’s a real issue, but there are some cases where WordPress is setting strings to lowercase automatically. Strings not visible to the end user should stay lowercase.

The po file will not be used, you have to create a mo file after translation. Make sure to click the button. :)

enter image description here

Then reload, the strings should be translated now.

share|improve this answer
    
Okey, I created a .mo file as well but it doesn't translate. I also tried lowercase text domain. It did not help either. Also there was an error in my code: add_action('int', 'localizationsample_init'); had to be corrected to add_action('init', 'localizationsample_init'); Also load_plugin_textdomain() does not return a value so checking if it is loaded with your suggested method does not seem to work. –  Teno Aug 17 '12 at 23:34
    
Was the correct file found? load_plugin_textdomain() returns a boolean value. –  toscho Aug 17 '12 at 23:44
    
load_plugin_textdomain() always seems to return false. According to the documentation of the function, "On failure returns null and also on success returns nothing." Does this mean that if it loads a language file then returns true? "returns nothing" mean returns true, not false? –  Teno Aug 17 '12 at 23:51
    
Don’t know where you read that. It returns TRUE on success and FALSE on failure. Look at the source. :) –  toscho Aug 17 '12 at 23:53
    
Ah, that's for load_textdomain() but the documentation of load_plugin_textdomain() also says "This function does not return a value." codex.wordpress.org/Function_Reference/load_plugin_textdomain –  Teno Aug 17 '12 at 23:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.