简体   繁体   English

遗留项目中的 Symfony 翻译组件

[英]Symfony translation component in a legacy project

Recently I was working on a very large project, with code mainly written in PHP.最近我在做一个非常大的项目,代码主要是用 PHP 编写的。 I was about to refactor some code.我正要重构一些代码。 Before I started refactoring I decided to write some unit tests first to have some confidence about not breaking anything (although this would probably happen anyway...).在我开始重构之前,我决定先编写一些单元测试,以确保不会破坏任何东西(尽管无论如何这可能会发生......)。 At the time I was trying to set up PHPUnit I realised the code I was about to test was full of function registered in the global namespace.在我尝试设置 PHPUnit 时,我意识到我将要测试的代码充满了在全局命名空间中注册的函数。 I decided to ignore this for the time being and think about a solution for mocking these global functions along the way.我决定暂时忽略这一点,并在此过程中考虑模拟这些全局函数的解决方案。

Due to the fact that this project is using PSR-4 in some places and no namespacing in other places made it very hard to circumvent these thightly coupled global functions.由于该项目在某些地方使用 PSR-4 而在其他地方没有命名空间,因此很难绕过这些紧密耦合的全局函数。

The longer I was thinking about a solution the more I realised I needed to rewrite these global functions into are more object-oriented way.我思考解决方案的时间越长,我就越意识到我需要将这些全局函数重写为更面向对象的方式。 I did some research about existing third-party libraries to replace the functionality these global functions provided.我对现有的第三方库进行了一些研究,以替换这些全局函数提供的功能。

One of these global functions is some kind of a translation service.这些全局功能之一是某种翻译服务。 My idea is to replace this with the Symfony's Translation component.我的想法是用 Symfony 的 Translation 组件替换它。 The main problem I have to overcome is to make this service globally accessible.我必须克服的主要问题是使这项服务全球可访问。 I came up with the idea of implementing it as a Singleton.我想出了将它实现为单例的想法。 This would just act as a wrapper around Symfony's Translation component returning the Translator instance.这只是作为 Symfony 的 Translation 组件返回 Translator 实例的包装器。 I could easily access it from anywhere throughout the code and so far I could not come up with a better idea.我可以在整个代码中从任何地方轻松访问它,到目前为止我想不出更好的主意。

I am not convinced if this is the way to go, so I am very much in the need of other suggestions and ideas.我不相信这是要走的路,所以我非常需要其他建议和想法。 So my question is:所以我的问题是:

How can I make the Symfony Translation component globally accessible in a non-Symfony project lacking consistent namespacing without generating too much overhead?如何在缺乏一致命名空间的非 Symfony 项目中全局访问 Symfony Translation 组件而不会产生太多开销?

   namespace App\Translation;

   use Symfony\Component\Translation as Translation;

   class Translate extends Translation\Translator implements TranslateInterface
   {

    private static $singleton;

    public static function getInstance($locale = null): Translate
    {
        if (static::$singleton === null) {
            static::$singleton = new static($locale ?: 'de');
        }

        return static::$singleton;
    }

    public function __construct($locale, Translation\Formatter\MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false)
    {
        $this->addLoader('json', new Translation\Loader\JsonFileLoader());
        $this->addResource('json',  'translations\messages.de.json', 'de');
        parent::__construct($locale, $formatter, $cacheDir, $debug);
    }

And then use it like this:然后像这样使用它:

\App\Translation\Translate::getInstance()->trans('msg');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM