---
title: Sphinxのi18n機能を試す sphinxjp
tags: []
categories: ["Dev", "Documentation", "Sphinx"]
date: 2012-08-19T05:52:13Z
updated: 2012-08-19T05:52:13Z
---

何気に [http://sphinx.pocoo.org/latest/intl.html][1] のドキュメントの通りでした。

gettextのインストールがまだの場合はインストールbrewを使っている場合は

    $ brew install gettext

で。

    $ which msgfmt

で何も返ってなかったらインストールされてません。

とりあえずquickstartでプロジェクトのひな形を作ります

    $ sphinx-quickstart 
    Welcome to the Sphinx 1.1.3 quickstart utility.
    
    Please enter values for the following settings (just press Enter to
    accept a default value, if one is given in brackets).
    
    Enter the root path for documentation.
    > Root path for the documentation [.]: helloworld
    
    You have two options for placing the build directory for Sphinx output.
    Either, you use a directory "_build" within the root path, or you separate
    "source" and "build" directories within the root path.
    > Separate source and build directories (y/N) [n]: y
    
    Inside the root directory, two more directories will be created; "_templates"
    for custom HTML templates and "_static" for custom stylesheets and other static
    files. You can enter another prefix (such as ".") to replace the underscore.
    > Name prefix for templates and static dir [_]: 
    
    The project name will occur in several places in the built documentation.
    > Project name: Hello World
    > Author name(s): making
    
    Sphinx has the notion of a "version" and a "release" for the
    software. Each version can have multiple releases. For example, for
    Python the version is something like 2.5 or 3.0, while the release is
    something like 2.5.1 or 3.0a1.  If you don't need this dual structure,
    just set both to the same value.
    > Project version: 1.0.0
    > Project release [1.0.0]: 
    
    The file name suffix for source files. Commonly, this is either ".txt"
    or ".rst".  Only files with this suffix are considered documents.
    > Source file suffix [.rst]: 
    
    One document is special in that it is considered the top node of the
    "contents tree", that is, it is the root of the hierarchical structure
    of the documents. Normally, this is "index", but if your "index"
    document is a custom template, you can also set this to another filename.
    > Name of your master document (without suffix) [index]: 
    
    Sphinx can also add configuration for epub output:
    > Do you want to use the epub builder (y/N) [n]: 
    
    Please indicate if you want to use one of the following Sphinx extensions:
    > autodoc: automatically insert docstrings from modules (y/N) [n]: 
    > doctest: automatically test code snippets in doctest blocks (y/N) [n]: 
    > intersphinx: link between Sphinx documentation of different projects (y/N) [n]: 
    > todo: write "todo" entries that can be shown or hidden on build (y/N) [n]: 
    > coverage: checks for documentation coverage (y/N) [n]: 
    > pngmath: include math, rendered as PNG images (y/N) [n]: 
    > mathjax: include math, rendered in the browser by MathJax (y/N) [n]: 
    > ifconfig: conditional inclusion of content based on config values (y/N) [n]: 
    > viewcode: include links to the source code of documented Python objects (y/N) [n]: 
    
    A Makefile and a Windows command file can be generated for you so that you
    only have to run e.g. `make html' instead of invoking sphinx-build
    directly.
    > Create Makefile? (Y/n) [y]: 
    > Create Windows command file? (Y/n) [y]: 
    
    Creating file helloworld/source/conf.py.
    Creating file helloworld/source/index.rst.
    Creating file helloworld/Makefile.
    Creating file helloworld/make.bat.
    
    Finished: An initial directory structure has been created.
    
    You should now populate your master file helloworld/source/index.rst and create other documentation
    source files. Use the Makefile to build the docs, like so:
       make builder
    where "builder" is one of the supported builders, e.g. html, latex or linkcheck.

翻訳対象のrstファイルはこんな感じ

    .. Hello World documentation master file, created by
       sphinx-quickstart on Sun Aug 19 15:01:28 2012.
       You can adapt this file completely to your liking, but it should at least
       contain the root `toctree` directive.
    
    Welcome to Hello World's documentation!
    =======================================
    
    Contents:
    
    .. toctree::
       :maxdepth: 2
    
    
    
    Indices and tables
    ==================
    
    * :ref:`genindex`
    * :ref:`modindex`
    * :ref:`search`

とりあえず`make html`で英語版を作る

<a href='/api/v1/files/00064/before.png'><img src='/api/v1/files/00064/before.png' /></a>

次に`make gettext`で"メッセージテンプレート"(potファイル)を作成。`build/locale/index.pot`ができます。

これをコピーして日本語用の"メッセージカタログ"(poファイル)を作成します。

    $ source
    $ mkdir ja
    $ cp ../build/locale/index.pot ja/index.po

index.poに日本語訳を書きます。

    # SOME DESCRIPTIVE TITLE.
    # Copyright (C) 2012, making
    # This file is distributed under the same license as the Hello World package.
    # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
    #
    #, fuzzy
    msgid ""
    msgstr ""
    "Project-Id-Version: Hello World 1.0.0\n"
    "Report-Msgid-Bugs-To: \n"
    "POT-Creation-Date: 2012-08-19 15:06\n"
    "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
    "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
    "Language-Team: LANGUAGE <LL@li.org>\n"
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    
    #: ../../source/index.rst:7
    # d8f24b0eec964ab1830ed172527587ab
    msgid "Welcome to Hello World's documentation!"
    msgstr "こんにちは世界のドキュメントへようこそ！"
    
    #: ../../source/index.rst:9
    # a2dfb65b49a24819ab3a1d53184c86f5
    msgid "Contents:"
    msgstr "内容:"
    
    #: ../../source/index.rst:17
    # 609fe2b716da4d1aa795d91e4849b4ea
    msgid "Indices and tables"
    msgstr "目次"
    
    #: ../../source/index.rst:19
    # 02bab84bfb6f4b468819c28bb73809f3
    msgid ":ref:`genindex`"
    msgstr ":ref:`genindex`"
    
    #: ../../source/index.rst:20
    # 0c31da7a12d84472936132f4dba52db6
    msgid ":ref:`modindex`"
    msgstr ":ref:`modindex`"
    
    #: ../../source/index.rst:21
    # 0aeafecfd9434a878d0ac48dc21537b2
    msgid ":ref:`search`"
    msgstr ":ref:`search`"

バイナリ形式にコンパイルします。翻訳ディレクトリtranslatedを作成して、直下の"language"/LC_MESSAGESなフォルダに出力します。

    $ mkdir -p translated/ja/LC_MESSAGES
    $ msgfmt ja/index.po -o translated/ja/LC_MESSAGES/index.mo

conf.pyに翻訳ディレクトリを追加

    locale_dirs = ["translated/"]

`-Dlanguage=ja`を付けてビルドすればOK(conf.pyに`laguage="ja"`)をつけてもOK

    $ cd ..
    $ make html SPHINXOPTS=-Dlanguage=ja
    sphinx-build -b html -d build/doctrees  -Dlanguage=ja source build/html

できた！

<a href='/api/v1/files/00065/after.png'><img src='/api/v1/files/00065/after.png' /></a>

[これ][2]を使うと楽らしい。


  [1]: http://sphinx.pocoo.org/latest/intl.html
  [2]: http://d.hatena.ne.jp/tk0miya/20111204
