web-dev-qa-db-ja.com

django-admin.py makemessagesが機能しない

文字列を翻訳しようとしています。

{% load i18n %}
{% trans "Well, Hello there, how are you?" %}

に...

Hola amigo, ¿que tal?

私のsettings.pyファイルにはこれがあります:

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'translations'),
)

そして、私はこれを取得しています:

(env)glitch:translations nathann$ Django-admin.py compilemessages
CommandError: Can't find msgfmt. Make sure you have GNU gettext tools 0.15 or newer installed.

私もこのエラーメッセージを理解していません。

(env)glitch:ipals nathann$ Django-admin.py makemessages -l es
CommandError:
This script should be run from the Django Git tree or your project or
app tree. If you did indeed run it from the Git checkout or your project
or application, maybe you are just missing the conf / locale(in the
Django tree) or locale(for project and application) directory? It is not
created automatically, you have to create it by hand if you want to
enable i18n for your project or application.

ドキュメント: https://docs.djangoproject.com/en/1.6/ref/Django-admin/#Django-admin-makemessages

また、ボーナスの賛成票については、関連する質問:インストール時にgettextがリンクされませんでした...これに関するヘルプはありますか?強制する必要がありますか?

glitch:translations nathann$ brew link gettext
Warning: gettext is keg-only and must be linked with --force
Note that doing so can interfere with building software.

ありがとう!


更新:

それ以来、翻訳の名前をロケールに変更し、それに応じてsettings.pyを更新しました。その後、私はこれを再び実行しましたが、それでもgettextについて不平を言っています:

(env)glitch:ipals nathann$ mv translations/ locale
(env)glitch:ipals nathann$ Django-admin.py makemessages -l es
CommandError: Can't find xgettext. Make sure you have GNU gettext tools 0.15 or newer installed.

私もこれを見つけました:

homebrewとkegのみの依存関係を理解する

これを読んだ後:

(env)glitch:ipals nathann$ brew install gettext
Warning: gettext-0.18.3.2 already installed
(env)glitch:ipals nathann$ brew link gettext
Warning: gettext is keg-only and must be linked with --force
Note that doing so can interfere with building software.
47
broinjc

設定でこれを確認した後:

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)
print(LOCALE_PATHS)

localeディレクトリが適切な場所にあり、名前のスペルが正しいことを再確認しました。

最終的にgettextをリンクしました( superuser でそれについて尋ねた後):

brew link gettext --force

manage.py compilemessages

Django-admin.py makemessages -l es

そしてBAM。 poファイルを取得しました。

しかし、医者は言います:

Warning: Some keg-only formula are linked into the Cellar.
Linking a keg-only formula, such as gettext, into the cellar with
`brew link <formula>` will cause other formulae to detect them during
the `./configure` step. This may cause problems when compiling those
other formulae.

Binaries provided by keg-only formulae may override system binaries
with other strange results.

You may wish to `brew unlink` these brews:

    gettext
71
broinjc

Ubuntuでこれを試してください

Sudo apt-get install gettext

そして、OSXでbrew install gettextを使用します

また、settings.pyファイルでローカルパスを設定してください。

53

Djangoで初めて翻訳に問題があるか、多言語サイトを初めて作成する場合の解決策を示します。ここに私がそれを行う方法があり、Django 1.4以降、1.7.1でテストされています:

Settings.pyで…

MIDDLEWEAR_CLASSES、ロケールに追加すると、リクエストに基づいて言語を選択できます。

'Django.middleware.locale.LocaleMiddleware',

LOCALE_PATHSを追加します。これは翻訳ファイルが保存される場所で、i18Nも有効にします。

USE_I18N = True

LOCALE_PATHS = (
    os.path.join(PROJECT_PATH, 'locale/'),
)

サイトを翻訳する言語を次のように設定します。

ugettext = lambda s: s
LANGUAGES = (
    ('en', ugettext('English')),
    ('fr', ugettext('French')),
    ('pl', ugettext('Polish')),
)

I18nテンプレートコンテキストプロセッサを追加すると、リクエストにLANGUAGESとLANGUAGE_CODEが含まれるようになりました。

TEMPLATE_CONTEXT_PROCESSORS = (
    'Django.contrib.auth.context_processors.auth',
    'Django.core.context_processors.debug',
    'Django.core.context_processors.i18n', # this one
    'Django.core.context_processors.request',
    'Django.core.context_processors.static',
    'Django.contrib.messages.context_processors.messages',  
)

ネスト、urls.py:

Url_patternsに以下を追加すると、設定された言語リダイレクトビューが有効になります。

url(r'^i18n/', include('Django.conf.urls.i18n')),

詳細については、「 翻訳 のその他」を参照してください。

次のインポートを追加し、翻訳したいURLをi18n_patternsでカプセル化します。これが私の外観です:

from Django.conf.urls.i18n import i18n_patterns
from Django.utils.translation import ugettext_lazy as _

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^i18n/', include('Django.conf.urls.i18n')),
)

urlpatterns += i18n_patterns('',
    (_(r'^dual-lang/'), include('duallang.urls')),
    (r'^', include('home.urls')),
)

注:管理者のURLをi18n_patternsにドロップすることもできます。

テキストを使用して変換し、lazytextをインポートし、_( 'text')のようにすべての文字列をラップしたい場合は、他のurls.pyファイルに移動して、次のようにURL変換を行うこともできます。

url(_(r'^dual_language/$'), landing, name='duallang_landing'),

Models.py、views.pyなど、他のファイルで翻訳したいテキストを折り返すことができます。ラベルとhelp_textの翻訳を含むモデルフィールドの例を次に示します。

name = models.CharField(_('name'), max_length=255, unique=True, help_text=_("Name of the FAQ Topic"))

Django翻訳ドキュメントはこれに最適です!

HTMLテンプレートで...

これで、テンプレートに移動してi18n templatetagをロードし、翻訳したい静的なものに対してtransおよびtransblockを使用できます。以下に例を示します。

{% load i18n %}

{% trans "This is a translation" %}<br><br>
{% blocktrans with book_t='book title'|title author_t='an author'|title %}
This is {{ book_t }} by {{ author_t }}. Block trans is powerful!
{% endblocktrans %}

次に、ロケールごとにmakemessagesを実行します。

./manage.py makemessages -l pl

そして、/ localesフォルダーに移動して、各.poファイルを編集するだけです。各msgstrのデータを入力します。そのような例の1つを次に示します。

msgid "English"
msgstr "Angielski"

そして最後にメッセージをコンパイルします:

./manage.py compilemessages

翻訳について学ぶべきことは他にもたくさんあり、 国際化 はこのトピックと密接に関連しているので、ドキュメントもチェックしてください。また、Django Django-rosetta 、および Django-linguo のような国際化パッケージを確認することをお勧めします。コンテンツでは、Django-rosettaはデータベースにこれに関する新しいエントリを作成しませんが、Django-linguoは作成します。

これに従えば、良いスタートを切るはずです。これは、サイトを複数の言語で実行するための最も標準化された方法だと思います。乾杯!

8
radtek

Brewがアドバイスしているように、Macユーザーにとってbrew link gettext --forceはリスクになる可能性があります。より良い回避策は、仮想環境に新しいPATH variableを設定することです。そのため、仮想環境フォルダーのbinフォルダーにあるpostactivateファイルに次のように入力します。

export TEMP_PATH=$PATH
export PATH=$PATH:/usr/local/Cellar/gettext/0.19.7/bin

0.19.7をマシンにインストールされているバージョンに置き換える必要があることに注意してください。

predeactivateファイルと同じフォルダーにあるpostactivateファイルに、次のように入力します。

export PATH=$TEMP_PATH
unset TEMP_PATH

これで、python manage.py makemessages -l <desired_language>を心配なく使用できます。 :)

乾杯。

6
reinaldoluckman

Sudo apt-getコマンドを使用して、ubuntu OSにgettextをインストールしてください

またはMac

brewコマンドを使用する

3
Abin Abraham

テンプレートの上部に{% load i18n %}を追加しましたか?

ボーナス:gettextをリンクする必要はありません。brew doctorからの出力は何ですか?

2
Gregg_1987

macOSの場合:

brew install gettext export PATH="/usr/local/opt/gettext/bin:$PATH"

2
valex

gettextをリンクしたくない場合(OS Xの内部をいじるのが悪いので、そうすべきではありません)、PATHコマンドにmakemessagesを設定できます。 。以下が動作するはずです(ただし、gettextのバージョン番号を調整する必要があります)。

PATH=/usr/local/Cellar/gettext/<installed version>/bin/:$PATH && \
Django-admin makemessages -l <language>

そうすれば、インストールされたgettextは樽のみのままであり、Django-adminは喜んで必要なプログラムをすべて見つけます。

0
jschrewe