|
| 1 | +# python の多言語対応 |
| 2 | + |
| 3 | +## 言語リソース化手法 |
| 4 | + |
| 5 | +### 言語リソースの定義 |
| 6 | + |
| 7 | +#### .py の場合 |
| 8 | + |
| 9 | +flask_babelexのlazy_gettextを利用する。 |
| 10 | +WEKO3では _ に置き換えて利用している。 |
| 11 | + |
| 12 | +例: |
| 13 | +``` |
| 14 | +from flask_babelex import lazy_gettext as _ |
| 15 | +
|
| 16 | +flash(_("ERROR"), category='error') |
| 17 | +``` |
| 18 | + |
| 19 | + |
| 20 | +#### jinja2 (.html)の場合 |
| 21 | + |
| 22 | +weko-accounts/weko_accounts/templates/.html |
| 23 | + |
| 24 | +``` |
| 25 | +{{ _('Welcome!') }} |
| 26 | +``` |
| 27 | + |
| 28 | +NOTE: |
| 29 | +実装にあたっては、i18nスコープを意識すること |
| 30 | + |
| 31 | +(1)リソース化できる実装パターン |
| 32 | + |
| 33 | +``` |
| 34 | +{% macro tabs_selector(tab_value='top') %} |
| 35 | + <li role="presentation" {% if tab_value=='top' %}class="active"{% endif %}><a href="/">{{ _('Top') }}</a></li> |
| 36 | + <li role="presentation" {% if tab_value=='item' %}class="active"{% endif %}><a href="{{url_for('weko_items_ui.index')}}">{{ _('Item Registration') }}</a></li> |
| 37 | + <li role="presentation" {% if tab_value=='author' %}class="active"{% endif %}><a href="{{url_for('weko_authors.index')}}">{{ _('Author Management') }}</a></li> |
| 38 | +{% endmacro %} |
| 39 | +``` |
| 40 | + |
| 41 | +(2)リソース化できない実装パターン |
| 42 | + |
| 43 | +``` |
| 44 | +{% macro tabs_selector(tab_value='top') %} |
| 45 | +{% set tabs_list = [('top', 'Top', '/'), |
| 46 | +('item', 'Item Registration', url_for('weko_items_ui.index')), |
| 47 | +('author', 'Author Management', url_for('weko_authors.index'))] %} |
| 48 | +{% for (value, name, href) in tabs_list %} |
| 49 | +<li role="presentation" {% if tab_value == value %}class="active"{% endif %}><a href="{{href}}">{{_(name)}}</a></li> |
| 50 | +{% endfor %} |
| 51 | +{% endmacro %} |
| 52 | +``` |
| 53 | + |
| 54 | + |
| 55 | +### 言語リソースの取得準備 |
| 56 | + |
| 57 | +setup.pyに以下のrequireを追加する。 |
| 58 | + |
| 59 | +``` |
| 60 | +setup_requires = [ |
| 61 | + 'Babel>=1.3', |
| 62 | +] |
| 63 | +``` |
| 64 | + |
| 65 | +setup.cfgに以下のコンフィグを追加する。 |
| 66 | + |
| 67 | +weko-accountsの場合: |
| 68 | +``` |
| 69 | +[extract_messages] |
| 70 | +copyright_holder = National Institute of Informatics |
| 71 | +msgid_bugs_address = wekosoftware@nii.ac.jp |
| 72 | +mapping-file = babel.ini |
| 73 | +output-file = weko_accounts/translations/messages.pot |
| 74 | +add-comments = NOTE |
| 75 | +
|
| 76 | +[init_catalog] |
| 77 | +input-file = weko_accounts/translations/messages.pot |
| 78 | +output-dir = weko_accounts/translations/ |
| 79 | +
|
| 80 | +[compile_catalog] |
| 81 | +directory = weko_accounts/translations/ |
| 82 | +``` |
| 83 | + |
| 84 | +weko_accounts/babel.ini |
| 85 | + |
| 86 | +jinja2のプラグインを追加 |
| 87 | + |
| 88 | +``` |
| 89 | +[python: **.py] |
| 90 | +encoding = utf-8 |
| 91 | +
|
| 92 | +# Extraction from Jinja2 templates |
| 93 | +
|
| 94 | +[jinja2: **/templates/**.html] |
| 95 | +encoding = utf-8 |
| 96 | +extensions = jinja2.ext.autoescape, jinja2.ext.with_, webassets.ext.jinja2.AssetsExtension, jinja2.ext.loopcontrols, jinja2.ext.i18n |
| 97 | +
|
| 98 | +# Extraction from JavaScript files |
| 99 | +
|
| 100 | +[javascript: **.js] |
| 101 | +encoding = utf-8 |
| 102 | +extract_messages = $._, jQuery._ |
| 103 | +
|
| 104 | +# 説明: |
| 105 | +# 例えば、webassets.ext.jinja2.AssetsExtensionがないと、jinja2テンプレートの {% assets %}{% endassets %}で囲まれた翻訳文が取れなくなる。 |
| 106 | +``` |
| 107 | + |
| 108 | +### 言語リソースの抽出 |
| 109 | + |
| 110 | +以下コマンドを実行する。 |
| 111 | + |
| 112 | +``` |
| 113 | +python setup.py extract_messages |
| 114 | +``` |
| 115 | + |
| 116 | +実行すると、翻訳用テキストが translations/messages.pot に出力される。 |
| 117 | + |
| 118 | +translations/messages.pot の例 |
| 119 | + |
| 120 | +``` |
| 121 | +#. NOTE: This is a note to a translator. |
| 122 | +#: weko_accounts/ext.py:41 |
| 123 | +msgid "Hello World!" |
| 124 | +msgstr "" |
| 125 | +
|
| 126 | +#: weko_accounts/templates/weko_accounts/index.html:24 |
| 127 | +msgid "Welcome!" |
| 128 | +msgstr "" |
| 129 | +``` |
| 130 | + |
| 131 | +### 言語ファイルの作成 |
| 132 | + |
| 133 | +messages.potから言語別のカタログファイルを作成する。 |
| 134 | + |
| 135 | +新規作成時は以下コマンドを実行する。 |
| 136 | + |
| 137 | +``` |
| 138 | +python setup.py init_catalog -l 言語コード |
| 139 | +``` |
| 140 | + |
| 141 | +カタログ更新時は以下を実行する。 |
| 142 | + |
| 143 | +``` |
| 144 | +python setup.py update_catalog -l 言語コード |
| 145 | +``` |
| 146 | + |
| 147 | +実行すると、translations/言語コード/LC_MESSAGES/message.poが作成される。 |
| 148 | + |
| 149 | +message.poの内容はmessages.potと同一となる。 |
| 150 | + |
| 151 | +[言語コード一覧](https://ja.wikipedia.org/wiki/ISO_639-1%E3%82%B3%E3%83%BC%E3%83%89%E4%B8%80%E8%A6%A7) |
| 152 | + |
| 153 | + |
| 154 | +### 翻訳の追加 |
| 155 | + |
| 156 | +ja/LC_MESSAGES/messages.po は基本的に以下構造となる。 |
| 157 | + |
| 158 | +``` |
| 159 | +#: 言語リソースの位置 |
| 160 | +msgid "言語リソースID" |
| 161 | +msgstr "翻訳" |
| 162 | +``` |
| 163 | + |
| 164 | +実際の例は以下の通り。 |
| 165 | + |
| 166 | +``` |
| 167 | +#. NOTE: This is a note to a translator. |
| 168 | +#: weko_accounts/ext.py:41 |
| 169 | +msgid "Hello World!" |
| 170 | +msgstr "はじめまして、世界!" |
| 171 | +
|
| 172 | +#: weko_accounts/templates/weko_accounts/index.html:24 |
| 173 | +msgid "Welcome!" |
| 174 | +msgstr "ようこそ!" |
| 175 | +``` |
| 176 | + |
| 177 | +### カタログのコンパイル |
| 178 | + |
| 179 | +以下コマンドを実行し、各言語のmessages.poをコンパイルする。 |
| 180 | + |
| 181 | +``` |
| 182 | +python setup.py compile_catalog |
| 183 | +``` |
| 184 | + |
| 185 | +translations/言語コード/LC_MESSAGES/message.moが作成される。 |
| 186 | + |
| 187 | +### 言語の追加 |
| 188 | + |
| 189 | +invenio.cfg に言語を追加する。 |
| 190 | + |
| 191 | +``` |
| 192 | +I18N_LANGUAGES = [('en', 'English'),('ja', 'Japanese'),('言語コード', '言語名')] |
| 193 | +``` |
| 194 | + |
| 195 | +populate-instance.sh に以下を追加する。 |
| 196 | + |
| 197 | +``` |
| 198 | +${INVENIO_WEB_INSTANCE} language create --active --registered "言語コード" "言語名" 順番 |
| 199 | +``` |
| 200 | + |
| 201 | +または直接invneioコマンドを実行する。 |
| 202 | + |
| 203 | +``` |
| 204 | +invenio language create --active --registered "言語コード" "言語名" 順番 |
| 205 | +``` |
| 206 | + |
| 207 | +例: |
| 208 | + |
| 209 | +``` |
| 210 | +${INVENIO_WEB_INSTANCE} language create --active --registered "fr" "Franch" 003 |
| 211 | +``` |
| 212 | + |
| 213 | +### 追加言語の確認 |
| 214 | + |
| 215 | +追加言語の確認を行うために、モジュールの再インストールとコンテナのリスタートを行う。 |
| 216 | + |
| 217 | + |
| 218 | +``` |
| 219 | +docker-compose -f docker-compose2.yml exec web |
| 220 | +cd modules/weko-accounts |
| 221 | +pip install -e . |
| 222 | +exit |
| 223 | +``` |
| 224 | + |
| 225 | +``` |
| 226 | +docker-compose -f docker-compose2.yml restart web |
| 227 | +``` |
| 228 | + |
0 commit comments