Constructing WP themes or plugins that take into account future gender translations (2)

Few days ago I began a serie of articles about how to design a WordPress Theme that facilitates that in the future it is possible to make a translation that takes into account the gender. In the first article I suggested a little technique to mark the Post Type (and for extension Taxonomy) objects including a new property that keeps the gender of the object and that depends on the translation today I’m going to extend this technique also for the labels of the Post type inside the Admin interface.

The #superhero bathroom at  @CommonDesk #fordtx #digitaldallas

As we saw, the core of the first proposal consists of adding one more line inside the array that is used to register any post type. See the code.

<?php
	$args = array(
		'labels'             => $labels,
                'description'        => __( 'Description.', 'your-plugin-textdomain' ),
		'public'             => true,

                // some more vars... etc.

		'menu_position'      => null,
		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),

                // this is our addition
                'gender'             => _x( 'male', 'gender of books', 'your-plugin-texdomain' ) 
	);

	register_post_type( 'book', $args );

}?>

Doing this, the translator has got the possibility to exchange this initial string for ‘female’, ‘neutral’, or ‘whatever’ in his/her local language. Principaly, this solution is oriented to the Front End –the WordPress theme– but, what does it happen in the Back End? Depending on the gender of the Post Type that you are working on, in a lot of languages, the interface ought change all its strings. As we saw, for example wine, cats and recipes has two (or three) different genders depending on the local language.

Of course, as a developer, you can decide to use a different array of labels for each one of the Post Types that your plugin or theme uses but I think that perhaps I could have a better solution using, precisely, the gender element that I already introduced in the last article.

In this proposal, firstly we have to declare three arrays of post type labels: one for male string, one for female strings and a third one for neutral strings. Secondly, we have to create a little function that returns one of these three arrays depending on the gender. Afterwards, for registering a new post types, we’ll use this function for constructing the labels. See the code.

<?php
function get_i18n_post_type_labels ( $gender = NULL, $singular = NULL, $plural = NULL ) {
	
   if ( empty( $gender ) 	) $gender   = 'male';
   if ( empty( $singular ) ) $singular = __( 'NoNamePostType', 'textdomain' );
   if ( empty( $plural ) 	) $plural   = $singular;
	
   switch ( $gender ) {
   case 'male' :
      return array( 
         'name' 	=> sprintf( _x( '%s', 'male gender post type in plural', 'textdomain' ), $plural ),
         'singular_name' => sprintf( _x( '%s', 'male gender post type in singular', 'textdomain' ), $singular ),
         'add_new' 	=> _x( 'Add new', 'male gender post type', 'textdomain' ),
         'add_new_item'	=> sprintf( _x( 'Add new %s', 'male gender post type', 'textdomain' ), $singular ),
         'edit_item'	=> sprintf( _x( 'Edit %s', 'male gender post type', 'textdomain' ), $singular ),
         'new_item'	=> sprintf( _x( 'New %s', 'male gender post type', 'textdomain' ), $singular ),
         // the rest of the strings
      );
      break;
   case 'female' :
      return array( 
         'name' 	=> sprintf( _x( '%s', 'female gender post type in plural', 'textdomain' ), $plural ),
         'singular_name' => sprintf( _x( '%s', 'female gender post type in singular', 'textdomain' ), $singular ),
         'add_new' 	=> _x( 'Add new', 'female gender post type', 'textdomain' ),
         'add_new_item'	=> sprintf( _x( 'Add new %s', 'female gender post type', 'textdomain' ), $singular ),
         'edit_item'	=> sprintf( _x( 'Edit %s', 'female gender post type', 'textdomain' ), $singular ),
         'new_item'	=> sprintf( _x( 'New %s', 'female gender post type', 'textdomain' ), $singular ),
         // the rest of the strings
      );
      break;
   case 'neutral' :
      return array( 
         'name' 	=> sprintf( _x( '%s', 'neutral gender post type in plural', 'textdomain' ), $plural ),
         'singular_name' => sprintf( _x( '%s', 'neutral gender post type in singular', 'textdomain' ), $singular ),
         'add_new' 	=> _x( 'Add new', 'neutral gender post type', 'textdomain' ),
         'add_new_item'	=> sprintf( _x( 'Add new %s', 'neutral gender post type', 'textdomain' ), $singular ),
         'edit_item'	=> sprintf( _x( 'Edit %s', 'neutral gender post type', 'textdomain' ), $singular ),
         'new_item'	=> sprintf( _x( 'New %s', 'male gender post type', 'textdomain' ), $singular ),
         // the rest of the strings
      );
      break;
   }
}?>

This strategy gives the translator the possibility to modify completely the sentences used for male, female and neutral genders whether for the singular as for the plural form of the words. Now, in our themes or plugins, we’ll use this function to declare three post types.

<?php

$gender   = _x( 'male', 'gender of cats', 'textdomain' );
$singular = _x( 'Cat', 'singular of cat', 'textdomain' );
$plural   = _x( 'Cats', 'plural of cat', 'textdomain' );
$labels   = get_i18n_post_type_labels ( $gender, $singular, $plural );
$args   = array( 
    'labels'             	=> $labels,
    'public'			=> TRUE,
    'exclude_from_search'	=> FALSE,

    // other parameters
    'gender'                    => $gender;
);
register_post_type ( 'cat', $args );


$gender   = _x( 'male', 'gender of wines', 'textdomain' );
$singular = _x( 'Wine', 'singular of wine', 'textdomain' );
$plural   = _x( 'Wines', 'plural of wine', 'textdomain' );
$labels   = get_i18n_post_type_labels ( $gender, $singular, $plural );
$args   = array( 
    'labels'             	=> $labels,
    'public'			=> TRUE,
    'exclude_from_search'	=> FALSE,

    // other parameters
    'gender'                    => $gender;
);
register_post_type ( 'cat', $args );

// ... and so on with all post types

?>

The translator will have just to translate once the three arrays of labels (male, female and neutral) and these translations will serve for all post type. From this point, for each Post Type, the translator will have just to translate the gender of the word in his/her local language and, of course, the name of the post type in singular and plural… Just three words.

With only one translation of the labels it can be changed and adapted all the WordPress admin interface for take into account the gender for all strings for all different post types in any language… It looks well, I guess.

I hope this second article will be useful for you. In the next articles we will see strategies for individual post, users, and terms.

See the first article of this serie.

Have a nice WordPressing!

Leave a Comment

   Mandatory field
You can use these HTML tags inside the commment.
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>