• yotsugi

    (@yotsugi)


    Hi all, I need your help

    I created custom post types function on my theme.
    My question is, how can I translate string for my custom post types label shown on admin page using text domain and without any plugin? Is it possible?

    register_post_type( 'training',
    		array(
    			'labels' => array(
    				'name' => __( 'Training', 'mydomain' ),
    				'singular_name' => __( 'Training', 'mydomain' )
    			),
    			'public' => true
    		)
    	);
Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter yotsugi

    (@yotsugi)

    No one?

    Same problem here.

    If I’m understanding your question correctly, you would like to use some kind of translation file instead of directly changing the label strings?

    I have been searching for a solution for hours on the web.

    So, let’s ask more generally:
    How does internationalization/translation work for custom post types or custom taxonomies?

    How are the non-custom parts being translated? Is there a .mo or a .po file that’s being used? Can you add your strings to it?

    Don’t know if that’ll work. I just thought someone ought to say something after 8 months.

    I use a plugin, myself. Much less work.

    Cheers

    PAE

    Okay – more details… in my case:

    I’m using an event custom post type which mostly consists of Noel Tock’s excellent code (see the link – it’s worth a view).

    This is a code snippet:

    $labels = array(
    	'name' => _x('Events', 'post type general name'),
    	'singular_name' => _x('Event', 'post type singular name'),
    	'add_new' => _x('Add New', 'events'),
    	'add_new_item' => __('Add New Event'),
    	'edit_item' => __('Edit Event'),
    	'new_item' => __('New Event'),
    	…

    I would like the editors of the site to see those labels in their own language (in my case: German) without editing the original English labels.

    I created a .po file by parsing the code above and already translated parts of it for testing purposes but I have no clue how to tell WordPress to use those translations on the custom post type. There are methods for plugins and themes to point to translation files with load_theme_textdomain() and load_plugin_textdomain() but if I use load_theme_textdomain() I don’t see any changes in the backend so I am unsure whether it has any impact on it at all. And I still don’t like the idea of creating a plugin for the only purpose of being able to translate the custom post type with .po/.mo files.

    Hoping for an answer…
    Florian

    You need to create a language file for the theme and, if necessary, amend the theme’s functions.php file to load the appropriate language file.

    Yeah, that’s what I did (I think, correctly):

    add_action('init', 'my_theme_setup');
    function my_theme_setup(){
        load_theme_textdomain('my_theme', get_template_directory() . '/languages');
    }

    Within the languages directory of my theme lays a de_DE.po file.

    Can you post a code example?

    My theme setup functions tends to have a great deal more in them but the code for the language loading is usually:

    load_theme_textdomain( 'theme_name', TEMPLATEPATH . '/langs' );
    $locale = get_locale();
    $locale_file = TEMPLATEPATH . "/langs/$locale.php";
    if ( is_readable( $locale_file ) ) require_once( $locale_file );

    I also use a different hook: add_action( 'after_setup_theme', 'my_theme_setup' );

    @esmi: As far as I know, loading a $locale.php is only required when setting up the language of the whole WordPress system. I already have a working localized version running.

    The text domain on the other hand seems to be required when translating via .po files which have nothing to do with a $locale.php.

    I’m only guessing at the moment because I have found no sources that explain the use of either load_theme_textdomain() or $locale.php (even the codex isn’t very helpful here).

    Can you post an example of a phrase which is translated via .po file?

    loading a $locale.php is only required when setting up the language of the whole WordPress system

    No – the whole point about a translation-ready theme or plugin is that it hooks into the localized version of WordPress correctly.

    Can you post an example of a phrase which is translated via .po file?

    <p class="protected-text">' . __('This post is password protected. To view it, please enter your password below:', 'zenlite') . '</p>

    Thank you for your efforts esmi. I finally solved the issue by narrowing down my possible error sources and it turned out to be the .po file not being generated accurately for wordpress. As I only translated a few phrases for testing purposes it happened that all those had comments like in

    'name' => _x('Events', 'post type general name', 'mytextdomain')

    where “post type general name” is a comment for the translators.

    Here comes the problem: This comment – if applied – seems to be essential for identifying the phrase when wordpress tries to match it with a translation. This means that the comment has to be in the .po file as msgctxt “post type general name” together with the msgid “Events”. If not, wordpress will not match the “Events” in the source file with the “Events” in the .po file and therefore not translate the phrase. Actually that wouldn’t be a problem if poedit which is recommended a lot for doing translations with .po/.mo files would care about that. But in fact it doesn’t. There is already a four year old ticket concerning that issue. To avoid this happening one could avoid putting comments in the source files but that would of course cause trouble for the translators as there would be no hints for disambiguation anymore. Or one could use another program than poedit for creating the .po files. The CodeStyling Localization plugin does a good job there. When you don’t need it anymore you can delete it and your translations will stay. But I hope that somebody will solve this issue with poedit soon as it caused me a lot of trouble and probably will do the same for a lot of other people coming across that problem.

    Concerning the load_theme_textdomain function it was fully sufficient for my purposes to load the .mo file with that following one line in my functions.php:

    load_theme_textdomain('mytextdomain', get_template_directory() . '/languages');

    No need for a locale.php in the theme directory.

    Now my custom post types are finally being translated :).

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Need Help] Translate custom post types label’ is closed to new replies.