po4a を騙して ConTeXt 文章を翻訳する(ネタ)

前回、「初めての ConTeXt Mark IV」 [id:munepi:20120408:context] の最後に、ちらつかせていたネタを少し晒します。

po4a http://po4a.alioth.debian.org/ とは、何でも po を経由して翻訳しちゃえというツールです。これをつかって、ConTeXt 文章を翻訳できないかという、ネタですw 現時点で無理のある作業をしているので、あくまでもネタです。

というのも、現時点の po4a version 0.41 では、「po4a-gettextize -f context」ができません*1。そこで、po4a を騙して ConTeXt 文章をなんとかして po 化して、翻訳します。騙すなんて、ヒドイwww

po4a の騙し方は、以下のとおりです。

  1. 「po4a-gettextize -f latex」を実行する前に、「\startMARKUP ... \stopMARKUP」を「\begin{MARKUP} ... \end{MARKUP}」に置換します。
  2. po4a を実行します。
  3. 「\begin{MARKUP} ... \end{MARKUP}」を「\startMARKUP ... \stopMARKUP」に戻します。

ねっ、po4a を騙しているでしょ? :) まぁ、Locale::Po4a::ConTeXt.pm を書けば (ry :)

簡単に騙せるサンプル

たとえば、以下のような ConTeXt 文章を doc/thisisapen.tex として保存します。

\usemodule[simplefonts]
\setmainfont[hiraginominchopronw3]

\starttext

This is a pen. 

\stoptext

doc/thisisapen.tex を「\starttext ... \stoptext」を「\begin{text} ... \end{text}」に置換して、Work/thisisapen.tex として保存します。

$ mkdir Work
$ perl -p -e 's@\\start([A-Za-z]+)@\\begin\{\1\}@g; \
      s@\\stop([A-Za-z]+)@\\end\{\1\}@g' doc/thisisapen.tex > Work/thisisapen.tex

以下の内容を po4a.cfg として保存します。

[po_directory] po
[po4a_alias:latex] latex opt:"-f latex -M UTF-8 -L UTF-8 -o no_wrap=text -k 0"
[type:latex] Work/thisisapen.tex $lang:doc-$lang/thisisapen.tex

ja.po のひな形となる thisiaapen.pot を生成します。

$ po4a-gettextize -f latex -M UTF-8 -L UTF-8 -o no_wrap=text -m thisiaapen.tex -p po/thisiaapen.pot

po/thisiaapen.pot の中身はこんな感じです。

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR Free Software Foundation, Inc.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2012-04-09 22:56+0900\n"
"PO-Revision-Date: 2012-04-09 22:56+0900\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#. type: Plain text
#: Work/thisisapen.tex:3
msgid "\\usemodule[simplefonts] \\setmainfont[hiraginominchopronw3]"
msgstr ""

#. type: text
#: Work/thisisapen.tex:7
#, no-wrap
msgid "This is a pen."
msgstr ""

「po/thisiaapen.pot を po/ja.po としてコピーする」または、「$ touch po/ja.po してから $ po4a po4a.cfg する」のどちらかを実行します。

po/ja.po を適当なエディタで開いて、さっそく翻訳します。

#. type: text
#: Work/thisisapen.tex:7
#, no-wrap
msgid "This is a pen."
msgstr "これはペンです。"

po/ja.po を保存してから「$ po4a po4a.cfg」を実行すると、doc-ja/thisisapen.tex に翻訳された文章が生成されています。後処理として、「\begin{text} ... \end{text}」を「\starttext ... \stoptext」に置換します。

$ perl -pi -e 's@\\begin\{([A-Za-z]+)\}@\\start\1@g; \
               s@\\end\{([A-Za-z]+)\}@\\stop\1@g' doc-ja/thisisapen.tex

最終的に doc-ja/thisisapen.tex はこのようになります。

% This file was generated with po4a. Translate the source file.
%
\usemodule[simplefonts] \setmainfont[hiraginominchopronw3]

\starttext

これはペンです。 

\stoptext

できたぉ!

今日は、ここまで!

でも…

実際に ConTeXt 文章を parse するとなると、ConTeXt の表現は、LaTeX のそれと大きく違います。当たり前ですが(笑

試した ConTeXt 文章のうち、うまく po4a を騙せなかったものとして、いくつかあげます。

  • \starttyping ... \stoptyping
  • Lua コードが入った ConTeXt 文章。とくに、% が Lua コードで意味を持つ場合。

結局、Locale::Po4a::ConTeXt.pm を書かないと (ry :)

さて、次はー、何を書こうかしらん。「続・初めての ConTeXt MkIV」がよいかしらん? :)

*1:[Po4a-devel] Support for ConTeXt http://lists.alioth.debian.org/pipermail/po4a-devel/2010-December/002006.html No!!!!! See Locale::Po4a::LaTeX.pm and お前がヤレ!:)