ブログの多言語化
2007年7月5日
ブログの一部を英語化した。これ用に、一つ簡単なプラグイン(NP_text)を製作した。ソースコードは、記事の続きに。
1)まず、english.php と japanese-euc.php を用意し、スキンディレクトリに配置(nucleus/language/japanese-euc.phpを参考のこと)。
2)スキンの必要な箇所をすべて、<%text(XXXXX)%>に置き換える。
3)head.inc の冒頭に、
4)HTML タグを、次のように記述。
5)META タグを、次のように記述。
6)英語で使用したいブログのindex.phpに、次の2行を挿入。
1)まず、english.php と japanese-euc.php を用意し、スキンディレクトリに配置(nucleus/language/japanese-euc.phpを参考のこと)。
2)スキンの必要な箇所をすべて、<%text(XXXXX)%>に置き換える。
3)head.inc の冒頭に、
<%plugin(text,header,text/html)%>を記述(HTTPヘッダ送信のための記述;将来、<%text%>タグがコアでサポートされるときのことを考え、<%plugin%>タグとして表記)。
4)HTML タグを、次のように記述。
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%text(_SKIN_LANG)%>" lang="<%text(_SKIN_LANG)%>">ただし、_SKIN_LANG として、en と ja-JP を用意。
5)META タグを、次のように記述。
<meta http-equiv="Content-Type" content="text/html; charset=<%text(_CHARSET)%>" />
6)英語で使用したいブログのindex.phpに、次の2行を挿入。
$text_language='english.php';
include('../../nucleus/nucleus/language/english.php');<?php
class NP_text extends NucleusPlugin {
function getName() { return 'NP_text'; }
function getMinNucleusVersion() { return 220; }
function getAuthor() { return 'Katsumi'; }
function getVersion() { return '0.1.2'; }
function getURL() {return 'http://www.rad51.net/nucleus/index.php?itemid=385';}
function getDescription() { return $this->getName().' plugin'; }
function supportsFeature($what) { return ($what=='SqlTablePrefix')?1:0; }
function getEventList() { return array('InitSkinParse'); }
var $skindir=false;
var $language=false;
var $included=false;
function event_InitSkinParse(&$data){
global $text_language,$DIR_SKINS;
$skin=&$data['skin'];
$this->skindir=$DIR_SKINS.$skin->getIncludePrefix();
$this->language=ereg_replace( '[\\|/]', '', getLanguageName()).'.php';
if (isset($text_language)) $this->includePHP($text_language);
}
function doIf($text='',$compare=''){
if (preg_match('/[^0-9a-zA-Z_]/',$text) || !defined($text)) exit('ERROR:'.__LINE__);
eval('$text='.$text.';');
return ($text==$compare);
}
function doSkinVar(&$skintype,$text,$prefix=0){
if ($text=='header' && is_string($prefix)) {
header('Content-Type: '.preg_replace('![^a-zA-Z0-9/\.]+!','',$prefix).'; charset='._CHARSET);
return;
} elseif ($text=='include' && is_string($prefix)) return $this->includePHP($prefix);
if (!$this->included) $this->includePHP('language');
if (preg_match('/[^0-9a-zA-Z_]/',$text) || !defined($text)) return;
eval('$text='.$text.';');
echo $text;
}
function includePHP($prefix){
$file=$this->_checkFile($prefix,$this->skindir,$this->language);
if ($file===false) return;
include_once($file);
$this->included=true;
}
function _checkFile($prefix,$skindir,$language){
if (substr($prefix,-4)=='.php' && file_exists($file=$skindir.$prefix)) return $file;
elseif ($prefix=='language' && file_exists($file=$skindir.$language)) return $file;
elseif (file_exists($file=$skindir.$prefix.$language)) return $file;
elseif ($prefix=='language' && file_exists($file=$skindir.'english.php')) return $file;
elseif (file_exists($file=$skindir.$prefix.'english.php')) return $file;
return false;
}
}
?>