0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPTALのi18n:構文でサイトを多言語化する

Posted at

翻訳クラス本体を作る

  • 例としてpoファイルの読み込みクラスを作成
  • GetTextライブラリを使用( GitHub )
class Translator {
	private $basePath;
	private $files;
	private $lang = 'ja';
	private static $resources = array();

	public function __construct($basePath = __DIR__ . "/../../lang", $files = ["{{lang}}.po"]) {
		$this->basePath = rtrim($basePath, '/\\') ;
		$this->files = $files;
	}

	public function setBasePath($path) {
		$this->basePath = $path;
	}

	public function setLang($lang) {
		$this->lang = $lang;
		$this->initResource($lang);
	}

	public function translate($key) {
		$this->initResource($this->lang);
		foreach(self::$resources[$this->lang] as $resource) {
			if ( $found = $resource->find(null, $key) ) {
				$translation = $found->getTranslation();
				return strlen($translation) ? $translation : $key;
			}
		}
		return $key;
	}

	protected function initResource($lang) {
		foreach($this->files as $file) {
			$file = u::mustache($file, ['lang' => $lang]);
			$file = $this->basePath . DIRECTORY_SEPARATOR . ltrim($file, '/\\');

			if ( isset(self::$resources[$lang][$file]) ) continue;

			if( !file_exists($file) ) {
				throw new \InvalidArgumentException("resource file not found: {$file}");
			}

			self::$resources[$lang][$file] = \Gettext\Translations::fromPoFile($file);
		}
	}
}

PHPTALTranslationServiceインターフェースを実装する

  • さしあたって必要なところのみ実装
  • 一応分けておかないとPHPTAL以外で翻訳したいときに困る

class PHPTALTranslationService implements \PHPTAL_TranslationService
{
	private $translator;

	public function __construct(Translator $translator) {
        $this->translator = $translator;
	}

    function setLanguage(/***/) {
        $this->translator->setLang(func_get_args()[0]);
    }

    function setEncoding($encoding) {
    	// UTF-8 only
    }

    function useDomain($domain) {
    	// not supported
    }

    function setVar($key, $value_escaped) {
    	// not supported
    }

    function translate($key, $htmlescape=true) {
    	$msg = $this->translator->translate($key);
    	return $htmlescape ? htmlspecialchars($msg, ENT_QUOTES) : $msg;
    }
}

PHPTAL初期化時にTranslatorを設定する

        $lang = $_GET['lang']; // 例
        $translator = new Translator($path);
        $translator->setLang($lang);
        $tal = new \PHPTAL();
        $tal->setTranslator(new \PHPTALTranslationService($tranalstor));

テンプレートに埋め込む

  • 何故 "structure string: "にしたか思い出せず、、何かがうまくいかなかった
<h1 
      i18n:translate="structure string:poファイルの項目名"
>翻訳したい タイトル</h1>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?