qTranslate ハウツーとハマりどころ

Pocket

多言語対応サイトを作るために、qTranslateプラグインをインストールした。そのメモ。

インストールと使い方

プラグインからインストール。
qTranslateを日本語WPに入れると、デフォルトの言語が英語に切り替わってしまう。最初に入れた状態では、中国語、英語、ドイツ語が入っているので、Settings (設定) -> Languages の下の表から、中国語と英語をdisableし、日本語をenableする。
表示中の言語は

$lang = qtrans_getLanguage();

を使うと取れるので、これを使ってcustom fieldsも表示できるようにテーマを編集する。その他便利な関数はこちらで紹介されている:

ハマりどころ

正直、qTranslateを使ってみて、色々と不具合があるように思える。ある言語で編集していたら、他の言語の欄が空っぽになっていたり、保存したら全部空っぽになっていたり、他のCustom Fieldsプラグイン(Advanced Custom FieldsやCustom Field Template)とconflictしたり。。
WP3.4.1以降の開発も止まっているようなんだが、現状のqTranslateの機能(url書き換えなど)は有用なので使いたい。そんなわけで、とりあえずハマりどころを説明する。Custom Fieldをどうしても使いたいので、その対処方法は後日紹介する。

  • 自分のWPのバージョンに合ったものをインストールすること。2013年1月17日現在、WP3.5対応のものが出ていないため、インストールすると、

qtranslate_warning
と表示される。暫定的な回避策は、qtranslate.phpの90行目を

define('QT_SUPPORTED_WP_VERSION', '3.5')

とする。対応版が公開されたら、ちゃんとアップデートしよう。

  • 他のCustom Fieldsプラグイン(Advanced Custom FieldsやCustom Field Template)とconflictする。無効にすれば使えるのだが。。
  • contentおよびtitleは ちゃんと the_content()やthe_title(), get_the_title()を使う
  • カスタム投稿タイプで使うためには、register_post_type()のsupportsにeditorを入れておかないと使えないようだ。

じゃあ、どうする?

カスタム投稿タイプによって、エディタを隠したい場合は、こうする: functions.phpに

add_action('admin_head', 'hide_tinymce' );
function hide_tinymce()
{
 $post_type = get_current_post_type();
    // 隠したいcustom post typeのarray
    $hide_in = array('hoge', 'moge');
    if( in_array($post_type, $hide_in) )
    {
?>
<style type="text/css">
 #postdivrich { display:none; }
</style>
<?php
    }
}
function get_current_post_type() {
    global $post, $typenow, $current_screen;
//we have a post so we can just get the post type from that
    if ( $post && $post->post_type )
        return $post->post_type;
    //check the global $typenow - set in admin.php
    elseif( $typenow )
        return $typenow;
    //check the global $current_screen object - set in sceen.php
    elseif( $current_screen && $current_screen->post_type )
        return $current_screen->post_type;
    //lastly check the post_type querystring
    elseif( isset( $_REQUEST['post_type'] ) )
        return sanitize_key( $_REQUEST['post_type'] );
    //we do not know the post type!
    return null;
}

参考: http://ben.lobaugh.net/blog/49208/wordpress-how-to-find-the-post-type-of-the-current-page-in-wp-admin

To be continued..

>> qTranslateの使い方 私見