qTranslate – Localize language names

I’ve used qTranslate in quite some projects already. It’s not perfect but it’s surely most user-friedly multilingual plugin for WordPress. I give you that qTranslate.

Every now and then a client is irritated by the fact language names appear in their native language and are not localized properly. An example would be:

Sorry, this entry is only available in Български.

My first encounter with this problem ended up hard-coding some lines for code into the plugin inself. After some time I needed to update the core and the plugin and totally forgot I did something there. Of course I lost my changes. With a better understanding of WordPress and the concepts it’s based on I once again went for that task. I was able to hook to qTranslate loading proccess at the point when it determines the language of the loaded page and apply its own localization methods to accomplish my goal.

 

First thing you need to do is to change your language name so it uses the qTranslate Quicktags.

qtranslate-edit-language
qTranslate – Edit language screen

 

The following piece of code will set filter to the qtranslate_language hook and kinda use it as an action rather than a filter. Tricky part here is that qtranslate_language is very early hook. It fires upon plugins_loaded action. This means theme’s functions.php is no good in this case. To be able to hook to it we need to place this code into a plugin.

While creating a plugin may be an overkill for a several lines of code, a MU plugin seems far more suitable for the purpose. It loads before any other regular plugin and guarantees the availability of every single one action and filter set in plugins.

  1. Go to your /wp-content/mu-plugins/ directory. If it doesn’t exists on your install create it.
  2. Create a qtranslate.mu.php file for example (may be any name) and place the code below into that file.
<?php
function qtrans_translate_lang_names( $language ) {

 if ( is_admin() && $_GET['page'] == 'qtranslate' )
 return $language;

 global $q_config;
 $q_config['language_name'] = array_map( 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage', $q_config['language_name'] );

 return $language;
}
add_filter('qtranslate_language', 'qtrans_translate_lang_names' );

Now your language names should be localized in every page even admin screens, except qTranslate Settings pages.

One Response to “qTranslate – Localize language names”

  1. Vladan

    Thank you! <3 :D

    Reply

Leave a Reply