概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:48:40 | 174 次阅读 | 评论: 0 | 来源: 网络整理

Sometimes, you need to deal with custom formats for translation files. The Translation component is flexible enough to support this. Just create a loader (to load translations) and, optionally, a dumper (to dump translations).

Imagine that you have a custom format where translation messages are defined using one line for each translation and parentheses to wrap the key and the message. A translation file would look like this:

(welcome)(accueil)
(goodbye)(au revoir)
(hello)(bonjour)

Creating a Custom Loader

To define a custom loader that is able to read these kinds of files, you must create a new class that implements the LoaderInterface. The load() method will get a filename and parse it into an array. Then, it will create the catalog that will be returned:

use SymfonyComponentTranslationMessageCatalogue;
use SymfonyComponentTranslationLoaderLoaderInterface;

class MyFormatLoader implements LoaderInterface
{
    public function load($resource, $locale, $domain = 'messages')
    {
        $messages = array();
        $lines = file($resource);

        foreach ($lines as $line) {
            if (preg_match('/(([^)]+))(([^)]+))/', $line, $matches)) {
                $messages[$matches[1]] = $matches[2];
            }
        }

        $catalogue = new MessageCatalogue($locale);
        $catalogue->add($messages, $domain);

        return $catalogue;
    }

}

Once created, it can be used as any other loader:

use SymfonyComponentTranslationTranslator;

$translator = new Translator('fr_FR');
$translator->addLoader('my_format', new MyFormatLoader());

$translator->addResource('my_format', __DIR__.'/translations/messages.txt', 'fr_FR');

echo $translator->trans('welcome');

It will print “accueil”.

Creating a Custom Dumper

It is also possible to create a custom dumper for your format, which is useful when using the extraction commands. To do so, a new class implementing the DumperInterface must be created. To write the dump contents into a file, extending the FileDumper class will save a few lines:

use SymfonyComponentTranslationMessageCatalogue;
use SymfonyComponentTranslationDumperFileDumper;

class MyFormatDumper extends FileDumper
{
    protected function format(MessageCatalogue $messages, $domain = 'messages')
    {
        $output = '';

        foreach ($messages->all($domain) as $source => $target) {
            $output .= sprintf("(%s)(%s)n", $source, $target);
        }

        return $output;
    }

    protected function getExtension()
    {
        return 'txt';
    }
}

The format() method creates the output string, that will be used by the dump() method of the FileDumper class to create the file. The dumper can be used like any other built-in dumper. In the following example, the translation messages defined in the YAML file are dumped into a text file with the custom format:

use SymfonyComponentTranslationLoaderYamlFileLoader;

$loader = new YamlFileLoader();
$catalogue = $loader->load(__DIR__ . '/translations/messages.fr_FR.yml' , 'fr_FR');

$dumper = new MyFormatDumper();
$dumper->dump($catalogue, array('path' => __DIR__.'/dumps'));
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务