I’ve been working on a WordPress project involving WooCommerce and language translation. One of the requirements of the project, is for the client to have translated text for any specific elements. To start with, we used a basic full-page translation through Google translate, but this didn’t provide the individual translations the client prefers. Additionally, the translation provided through Google, was not high enough in accuracy.
So, I decided to look into some available plugins to handle the operation (rather than having to code an entire, from-scratch translation mechanism). I came across qTranslate. It’s quite fantastic, and works just as expected. It does precisely what I need it to do…translate individual segments of text, in fields such as “title”, “content”, as well as menu items, widget titles, etc.
The primary problem I had with this plugin, is the way in which it has to be used. The client would be required to type out <!–:en–>Some text<!–:–> around each piece of text they want to translate. This seems a bit cumbersome to expect from a non-savvy user. So, I came across the IM8 qTranslate Woocommerce WordPress plugin, which converts the requirement for <!–:en–> to simply [:en]. This seems much cleaner and easier for a client to manage.
The only problem with any of this, is that there is no built-in consideration for “subtitle”. In fact, WordPress itself does not have this feature, even after all this time. In my opinion, I would assume that this is a pretty standard request (I get it at least 75% of the time), and that WordPress would add this to the core, but alas, I suppose they do not see it as useful. At any rate, many great plugins have been introduced to handle the “subtitle” concept. I decided to roll with WP-Subtitle, because I’ve used it successfully many times without issue.
Granted, WP-Subtitle does not integrate with WooCommerce out of the package, it’s very simple to add the function. Just go to your functions.php file and add the following:
add_post_type_support( 'product', 'wps_subtitle' )
Now, this will provide us with the subtitle. However, you will find that if you attempt to use the translation tags [:en] (or otherwise) they do not work within the subtitle field. There is a solution to this. Ordinarily you would echo your subtitle in your theme file like this:
However, this is not going to work. What you will need to instead is this:
$s = __(get_post_meta(get_the_ID(), 'wps_subtitle', true)); echo $s;
It’s just that simple. The __() allows the string to be localized, which then allows the qTranslate plugin to interpret the data and provide the translated string. Using “get_post_meta” allows the data to be returned in a sanitized format, so that only the text itself is utilized, and no extra markup is appended that might confuse the translation. Once we have our variable retrieving the correct translated string, we echo it with echo $s.