• Multilingualization with qTranslate

    * Created from a part of the post (Web Site Redesigned) that I wrote before.

    qTranslate

    Generally there are four methods to multilingualize web site.

    1. Install to different database
    2. Install to same database with different table prefix
    3. Creating a network of sites by using multisite feature
    4. Using qTranslate plugin

    I read the following post and decided to multilingualize web site with qTranslate plugin.

    Date/Time Format

    Default date/time format settings in qTranslate must be wrong. I changed them like below.

    English
    • Date Format: Replace %A %B %e%q, %Y with F j, Y
    • Time Format: Replace %I:%M %p with g:i A
    Japanese
    • Date Format: Replace %Y年%m月%d日 with Y年n月j日
    • Time Format: Replace %H:%M with H:i

    qTranslate description says as follows, but I think this is lie. You should enter WordPress Date/Time format instead of strftime or date format. You can see WordPress format in [Dashboard] – [General Settings] – [Date Format].

    Date Format
    Depending on your Date / Time Conversion Mode, you can either enter a strftime (use %q for day suffix (st,nd,rd,th)) or date format. This field is optional. (Example: %A %B %e%q, %Y)
    Time Format
    Depending on your Date / Time Conversion Mode, you can either enter a strftime or date format. This field is optional. (Example: %I:%M %p)

    Language Chooser Menu

    You can use “qTranslate Language Chooser” widget in sidebar and footer. But I wanted to switch language by using custom menu. So I wrote the following code in functions.php.

    function my_nav_menu_items($items) {
    	// Language Chooser
    	global $q_config;
    	$url = is_404() ? get_option('home') : '';
    	$lang = qtrans_getLanguage() == 'ja' ? 'en' : 'ja';
    	return $items . "<li id='menu-item-lang' class='lang-{$lang} menu-item menu-item-type-custom menu-item-object-lang'><a href='" . qtrans_convertURL($url, $lang) . "' hreflang='{$lang}'>{$q_config['language_name'][$lang]}</a></li>";
    }
    add_filter('wp_nav_menu_items', 'my_nav_menu_items');
    

    Here is a screenshot of Language Chooser Menu.

    Language Chooser Menu

    Link URL of Home Menu

    Home menu in custom menu links to top page of blog (i.e. [General Settings] – [Site Address (URL)]). I appended the following code in the function above to convert the top page URL according to current language. I think there is a better way but I have no idea for now.

    function my_nav_menu_items($items) {
    	// Home URL
    	$items = str_replace('"' . get_home_url() . '/"', '"' . qtrans_convertURL(get_home_url()) . '/"', $items);
    

    Translate Template Files

    The post I read introduces the translation code below.

    <?php _e("<!--:en-->english<!--:--><!--:ja-->Japanese<!--:-->"); ?>
    

    You can also use the simple code like below.

    <?php _e("[:en]english[:ja]Japanese"); ?>
    

    As you know, if you use __() function instead of _e() function, a value is not output but returns as return value.

    Support qTranslate in Your Theme

    There are useful qTranslate functions for developers. For instance, qtrans_getLanguage() function returns current language and qtrans_convertURL() function converts a URL according to current language. If you want to see more functions, visit the following page.

    Of course qTranslate is required by using these functions. If you want to support environments that has no qTranslate, use function_exists function.

    Post Tagged with

Leave a Reply

Your email address will not be published.