Support » Plugins » Hacks » Trouble to translate own plugin (add_menu_page/add_submenu_page)

  • Resolved Mika

    (@blaueantilope)


    Hi,

    I spend few hours to fix my problem – I’m giving up. I need help!

    Whats wrong with my code to translate an admin-menu? Everything else is nice translated (plugin header, error messages, buttons – perfect!), the menu doesn’t work. ­čÖü

    I use PoEdit Pro for the translation.

    text-domain: yet-another-blogroll

    add_action('plugins_loaded', 'yab_init');

    function yab_init() {
    	 load_plugin_textdomain( 'yab', false, plugin_dir_path( __FILE__ ) . 'languages/' );
    }

    add_action('admin_menu', 'yab_admin_menus');

    function yab_admin_menus() {
    	if ( current_user_can( 'manage_options' ) ) :
    		// Hauptmen├╝
    		$top_menu_item = 'yab_admin_page';
    
    		add_menu_page('', __( "Yet Another Blogroll", 'yet-another-blogroll' ), 'manage_options', 'yab_admin_page',
    						  'yab_admin_page', 'dashicons-rss' );
    
    		// Untermen├╝: Information (wie Hauptmen├╝)
    		add_submenu_page($top_menu_item, '', __( "Overview", 'yet-another-blogroll' ), 'manage_options', $top_menu_item, $top_menu_item );	
    
    		// Untermen├╝: Gruppen
    		add_submenu_page($top_menu_item, '', __( "Groups", 'yet-another-blogroll' ), 'manage_options', 'yab_groups_admin_page' );
    
    		// Untermen├╝: Webseiten
    		add_submenu_page($top_menu_item, '', __( "Websites", 'yet-another-blogroll' ), 'manage_options', 'yab_websites_admin_page' );
    
    		// Untermen├╝: Einstellungen
    		add_submenu_page($top_menu_item, '', __( "Settings", 'yet-another-blogroll' ), 'manage_options', 'yab_settings_admin_page' );
    
    		// Untermen├╝: Fehlerprotokoll
    		add_submenu_page($top_menu_item, '', __( "Error logs", 'yet-another-blogroll' ), 'manage_options', 'yab_errorlogs_admin_page' );	
    
    	endif;
    
    }

    Please! Help! Me! It’s a detail, yes, but a pain in the… ­čśë

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    This isn’t my area of expertise, but I believe the plugin header text domain line and the translation functions __() text domain parameter should all match the tag assigned when load_plugin_textdomain() is called. In your example, ‘yab’.

    Thus the plugin header line should be // Text Domain: yab
    And translation functions should be __( 'Overview', 'yab' )

    Alternately, you could change everything in your plugin to ‘yet-another-blogroll’. Whatever you decide to use, use it consistently. This includes how the .mo etc. files are named.

    Mika

    (@blaueantilope)

    Hi bcworkz,

    thanks for your answer.

    Yes, you are right – the textdomain must be identical. It is identical, but I have copied the load_plugin_textdomain code from an old file – upsi. ­čśë

    In my testcase the textdomain is all over the same and it works but not on the admin menu… ­čÖü

    Mika

    (@blaueantilope)

    Okay, it looks like a typo error – I create a new plugin just with the basics and everything is fine.

    Folder:
    /languages/
    test-plugin.php

    <?php
    /*
    Plugin Name: Test Plugin
    Description: Test Plugin
    Version:     4.2
    Author:      Mika
    Domain Path: /languages
    Text Domain: test-plugin
    */
    
    // Action
    add_action( 'init', 'tp_init' );
    add_action( 'admin_menu', 'add_my_custom_menu' );
    add_action( 'plugins_loaded', 'tp_load_plugin_textdomain' );
    
    // Hooks
    register_activation_hook( __FILE__, 'tp_activation' );
    register_deactivation_hook( __FILE__, 'tp_deactivation' );
    
    function tp_load_plugin_textdomain() {
        load_plugin_textdomain( 'test-plugin', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
    }
    
    function tp_init() {
    
    }
    
    function tp_activation() {
    
    }
    
    function tp_deactivation() {
    
    }
    
    function add_my_custom_menu() {
        //add an item to the menu
        add_menu_page (
            'Test Plugin Page-Title',
            __('Test Plugin EN', 'test-plugin'),
            'manage_options',
            'tp_page',
            'tp_admin_page_function',
            'dashicons-businessman'
        );
    
        add_submenu_page (
          	'tp_page',
          	'Settings',
          	__('Settings', 'test-plugin'),
          	'manage_options',
          	'tp_setting_page',
          	'tp_admin_setting_page'
        );
    }
    
    function register_submenu_page() {
    
    }
    
    function tp_admin_page_function() {
    	?>
    	 <div class="wrap">
            <h2>Test-Plugin</h2>
            Content...
        </div>
    	<?php
    
    }
    
    function tp_admin_setting_page() {
    	?>
    	<div class="wrap">
            <h2>Settings</h2>
            Content...
        </div>
    	<?php
    }

    I create with PoEdit a new pot file and the po/mo-files (filename must be “test-plugin-de_DE.mo/po” for german). And taadaaa, it works…

    Solved!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Trouble to translate own plugin (add_menu_page/add_submenu_page)’ is closed to new replies.