How to create translation ready shortcodes

I use many themes and plugins from other talented authors, but most of them are not French like me. That’s why each time i use a plugin or a theme from a non French developer, i have the same problem: shortcodes. Well, it’s not really a big problem for me because i speak English, but what about my French clients that don’t speak English at all? Imagine: they’re writing a post in French into their French version of the administration, and suddenly, when the time comes to insert a shortcode they have to insert it in English… Wow! That makes me feel bad. Their texts would then be similar to this:

“Il y a actuellement en ligne: [online_users] utilisateurs”.

Which means :

“At the moment [online_users] connected users”.

Imagine if you were having the same thing in English:

“At the moment [utilisateurs_connectes] connected users”.

I’m sorry, but writing the shortcode in another language isn’t user friendly at all because it’s not logic. So i decided to show you how to create a user friendly shortcode that can be used in any language. I’m talking about French, but that’s the same thing with every language!

Step 1

The first step is to create a really simple plugin. Create a new folder under wp-content/plugins and name it: “translation-ready-shortcodes”. Then open it and create a new “index.php” file. Place this code :

This code simply creates the plugin, nothing more.

Step 2

We then need to load language files using load_plugin_textdomain(). If you want to use this method with a theme use load_theme_textdomain() instead. So, add this code:

Step 3

Here we are! The interesting part! In this step we need to register the shortcode. In my sample, i’m going to use this shortcode : [link to=”http://wordpress.org”]WordPress[/link]. The aim is to echo a simple link.

The basic code to do this is:

But the problem here, is that if a French guy uses this shortcode he will have to write “link” instead of “lien”, the French translation. So, what i want to do is to give this poor French guy the ability to write: [lien vers=”http://wordpress.org”]WordPress[/lien]. That would be much better.

To do so, i need to modify on the fly the shortcode itself (the tag), and the shortcode attributes (in this case, the word “to”). For the first one it’s easy, we simply need to use the __() function, but for the second one it’s more complex. We need to use a variable having a variable name. The code becomes:

That’s it! I mean for the code that’s it, now if you have to language file you can try to switch from one language to another… that works! It means that a English guy will use:

[link to=”http://wordpress.org”]WordPress[/link]

and a French guy will use:

[lien vers=”http://wordpress.org”]WordPress[/lien]

while an Italian guy will do:

[collegamento a=”http://wordpress.org”]WordPress[/collegamento]

Isn’t it great?

Download the code

10 responses to “How to create translation ready shortcodes”

  1. FxB

    Thanks i added this piece (or i should write peace) of code to my translation snippet reminder. I will hunt GitHub repo of WordPress extensions with it.

    1. Thanks, to be honest i have never seen something similar anywhere, that’s why i wanted to write this post!

  2. thierrypigot

    Hi,
    Thank you for this article. In fact, I use this trick for several years. Strings to translate or adapt the parameterization based on the language.

    Thierry

  3. Steven Zahm

    I like the idea and I was going to implement it in my plugin but this could break the new shortcode_atts_{$shortcode} filter in WP 3.6, read here:
    http://markjaquith.wordpress.com/2013/04/04/wordpress-36-shortcode-attribute-filter/

    Basically translating the shortcode tag, another plugin couldn’t reliable hook into this filter.

    And.

    Although not impossible to work around, using the $tag option in the shortcode callback would be more difficult. Here’s a link to a tutorial that simplifies shortcode by using the $tag option.
    http://wp.tutsplus.com/tutorials/plugins/multiple-shortcodes-with-a-single-function-3-killer-examples/

    Thoughts?

    1. Hi Steven. I also read these two excellent posts, and to be honest i haven’t tested the new shortcode_atts_{$shortcode} filter in the beta, so for now i don’t know how it can interact with what i describe in this article. But i will definitely give it a try!

  4. Julien

    It’s a very interesting topic. I have actually never thought about it but it makes a lot of sense. Thanks for sharing this tip!

  5. SEO Translator

    As an IT professional, I must say that I like the idea and your code, but as a translator with 36 years of experience I can only smile – any real professional translator worth his salt will be able to spot short codes and will obviously NOT translate them!

    1. Maybe not everyone will translate in all languages, but at least it’s a working solution for those who want to provide a minimal language support.

  6. alexandreb

    Hi Rémi,

    I managed to make it work but I had to wrap my add_shortcode functions into a function and hook it to init.

    I hope that snippet could help someone stuck ;)

    function coco_shortcodes_register(){
    add_shortcode(__(‘cocorico_column’, ‘cocoshortcodes’), ‘coco_shortcodes_column’);
    }
    add_action( ‘init’, ‘coco_shortcodes_register’);

  7. MikeiLL

    Thanks for this insightful post. I did get some other advice recommending against translating shortcodes, though: on SO.

Leave a Reply