Support » Developing with WordPress » Translation in selfcoded Plugin doesn’t work

  • Hello my fellow WordPress Devs,

    i’m new in WordPress coding and try to code a free plugin for animal shelters.

    I want to create a new post type “Pets” this works absolute fine.

    But when i want to translate the post type it doesn’t work and i dont know what i’m doing wrong.

    Here is the code, i cut off the parts that dont matter:

    
    * Text Domain: easypetmanager
     * Domain Path: /lang/
    
    add_action('init', 'easypetmanager_load_textdomain');
    function easypetmanager_load_textdomain() {
        load_plugin_textdomain( 'easypetmanager', false, dirname( plugin_basename(__FILE__) ) . '/lang' );
    }
    
    add_action( 'init', 'create_post_type_pets');
    function create_post_type_pets() {
        $labels = array(
            'name'               => _x( 'Pets', 'post type singular name', 'easypetmanager' ),
            'singular_name'      => _x( 'Pet', 'post type singular name', 'easypetmanager' ),
            'menu_name'          => _x( 'Pets', 'post type singular name', 'easypetmanager' ),
        );
    
        $args = array(
            'labels'             => $labels,
            'description'        => __( 'Description.', 'easypetmanager' ),
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'rewrite'            => array( 'slug' => 'pets' ),
            'capability_type'    => 'post',
            'has_archive'        => true,
            'hierarchical'       => false,
            'menu_position'      => null,
            'menu_icon'           => 'dashicons-portfolio',
            'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
        );
    
        register_post_type( 'pets', $args );
    
    }
    

    I got the folder structure:

    \wp-content\plugins\easypetmanager
    \wp-content\plugins\easypetmanager\easypetmanager.php
    \wp-content\plugins\easypetmanager\lang
    \wp-content\plugins\easypetmanager\lang\easypetmanager-de_DE.mo

    The new post type works just fine, but it doesn’t use my translation.

    I hope you can help me with my problem.

    Kind regards!

Viewing 6 replies - 1 through 6 (of 6 total)
  • To use languages supplied with the plugin, you must load the text domain.
    https://developer.wordpress.org/reference/functions/load_textdomain/
    When you submit your plugin to the repository, it will have a place in glotpress where translations are stored. When the translation is 100%, it is loaded from glotpress. (This is since WP 4.6.)
    The documentation is a little confusing, as it was written at different times. You can go into glotpress and provide translations for your plugin instead of bundling them with your plugin.

    https://make.wordpress.org/polyglots/handbook/tools/glotpress-translate-wordpress-org/

    You have to pick a language and then a plugin, so here is the French locale:
    https://translate.wordpress.org/locale/fr/default/wp-plugins

    But you can go to a plugin page and click on the link to help translate it. (but it has to be in the repo first)

    Hello @joyously,

    thanks for your Answer. Right now i dont want to put my plugin in the repo because i’m only in a testing environment.

    So the old way, to ship my translation with the plugin won’t work anymore?

    Yes, it works. It’s just that the docs are a bit confusing because it used to be the only way and now it is the rare case.
    If you aren’t using glotpress, you need to load_textdomain.

    Thats what i’m doing, as you can see in my code but the name etc. doesn’t get translated.

    I allready test a debug plugin if my .mo gets loaded correct:

    Domain:    easypetmanager
    File:      /homepages/19/xxxx/htdocs/easypetmanager.app/wp-content/languages/plugins/easypetmanager-de_DE.mo (0.49kb)
    Called in: /homepages/19/xxxx/htdocs/easypetmanager.app/wp-content/plugins/easypetmanager/easypetmanager.php line 16 load_plugin_textdomain

    Seems fine to me but the translation dont work.

    Is your test WordPress installed in the other language? It will only load the language file if it matches the language of the WordPress installation.

    Yes, i code the plugin in english and have my test install in german.

    I have allready made 2 test installations etc.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.