0

I am looking for a tutorial on the preferred method of handling localization in Zend 2, but so far I haven't been able to find any. The best I could find is this page, which doesn't explain the practical process of implementing localization (specifically, application messages) in detail, or this question, which was asked before the release of Zend 2 and is now outdated.

If given a choice presented on that page, say I pick GNU Gettext as a translation format. Is there any tutorial on localizing a ZF2 application in that case?

Or, say I store the text of the pages on my site in a database table, for example

CREATE TABLE `page` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT NULL,
  `body` blob,
  `locale` int(11) NOT NULL,
  `creator` int(11) NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `pagecreatorfk_idx` (`creator`),
CONSTRAINT `pagecreatorfk` FOREIGN KEY (`creator`)
REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8

How would I go with providing localized messages then?

4

1 に答える 1

5

さて、初心者にはhttp://www.poedit.net/などの翻訳プログラムが必要です。

これが私のセットアップ方法です:

Moduleフォルダーに、「language」という別のフォルダーを作成します。

module.config.phpを開き、以下を追加します。

'translator' => array(
    'translation_file_patterns' => array(
        array(
            'type'        => 'gettext',
            'base_dir'    => __DIR__ . '/../language',
            'pattern'     => '%s.mo',
            'text_domain' => 'account'
        )
    )
)

私にとって物事を簡単にするのは、language.phpファイルを作成し、次のような行を追加することです。

echo _('text_to_translate_here');

PoEditを設定するときに、このファイルをスキャンして、翻訳IDをPoEditに追加します。これで、表示したい実際のテキストを追加できます。保存すると、account.moとaccount.poのような2つのファイルが出力されます。言語フォルダにアップロードする必要がある唯一のファイルはaccount.moです

アプリケーション全体で翻訳を使用しているので、global.phpファイルにファクトリを追加しました。

return array(
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        )
    )
);

私のview.phtmlファイルでは、次のように翻訳できるようになります。

<?php echo $this->translate('text_to_translate_here', 'account'); ?>

ここで、module.config.phpの例で示したようにテキストドメインを使用したくない場合は、すべての翻訳でデフォルトで「デフォルト」のテキストドメインが使用されます。したがって、代わりにこれを行うことができます:

<?php echo $this->translate('text_to_translate_here'); ?>

これはFormLabelsでも機能します:(これにより、ラベル名としてPriroityが出力されます)

<?php echo $this->formLabel($this->form->get('prio')); ?>

「デフォルト」以外のテキストドメインを使用してフォームオブジェクトを翻訳する場合は、これをview.phtmlに追加できます。

$this->formSubmit()->setTranslatorTextDomain('account');

これで、Submit FormLabelsは、デフォルトのテキストドメインの代わりにそのテキストドメインを使用します。他のタイプのフォームオブジェクトについても同じことが言えます。formSubmitを要素タイプに置き換えるだけです。

これが役立つかどうか、または何かが足りない場合はお知らせください。

于 2013-03-02T20:43:43.190 に答える