gpt4 book ai didi

交响乐 2 : Static function [JMSTranslation]

转载 作者:行者123 更新时间:2023-12-02 04:53:22 26 4
gpt4 key购买 nike

我目前正在处理一个项目,之前的开发人员将其与 JMSTranslationBundle 集成在一起。此刻,我对应用程序做了一些修改,其中之一是将菜单变成高度动态的。 (基本上,应用程序的用户逻辑有 3 层,每一层都有自己的菜单)。

菜单存储在数据库中,可通过学说实体访问。为了显示标签,我将 JMSTranslationBundle 使用的“标签代码”存储到数据库中作为识别它的键。 desc 默认情况下为空,直到设置到翻译文件中。 (可使用 _trans 路线进行编辑)。

在 JMS 的文档中,提到可以实现 TranslationContainerInterface,因此当完成翻译文件(目前是 XLIFF 文件)的编译时,将调用实现它的每个类以返回消息对象列表。这是我的问题:

要实现的功能是静态的,这意味着在调用时,我的模型菜单(处理通过 Doctrine 存储库获取逻辑的)不会通过服务管理器加载。这意味着我没有收到存储库对象(因为它是由服务加载并通过 Controller 传递的):

public function __construct(MenuRepository $objMenuRepo)...

我实现的函数的定义是:

static function getTranslationMessages(){ ... }

我的问题是:如何在该静态函数中获取原则(管理器或存储库)。 (因为这只会在翻译初始生成时调用,而不是由站点本身调用,所以性能不是我担心的问题)。

另外:如果有人有更好的替代建议(这不涉及摆脱这个翻译包,相信我,现在需要相当多的时间),我愿意听取他们的意见。

谢谢 :-)

最佳答案

如果你们中的一些人感兴趣,我不得不使用替代解决方案。

虽然它没有回答有关如何在静态上下文中使用服务的问题,但它会帮助那些遇到与我在尝试使用 JMSTranslation 时遇到的相同问题的人。

要实现解决方案(从数据库中提取翻译 key ),我必须使用 JMS\TranslationBundle\Translation\ExtractorInterface。我已经按照这种格式实现了它:

class TranslationRepositoriesExtractor implements ExtractorInterface{
//Loaded through the service container
public function __construct(EntityRepository $objRepositoryNeeded);

// Implementation of the interface ExtractorInterface.
// Within this function, I've used the EntityRepository received in the
// constructor to fetch the list of keys that would be use for translating
/**
* @return \JMS\TranslationBundle\Model\Message[]
*/
public function extract()
}

正如您所注意到的,提取函数返回一个\JMS\TranslationBundle\Model\Message 数组。实现此功能后,您必须将您的对象添加为服务,并使其作为提取器可被 JMSTranslationBundle 识别。为此:

 <!-- Replace the id of the service, the class path, the id of the argument and the alias 
named you want by the value you need in your application -->
<service id="idOrYourService" class="Path\Of\Class\TranslationRepositoriesExtractor">
<argument type="service" id="repository.needed" />
<tag name="jms_translation.extractor" alias="NameOfAlias" />
</service>

别名标记在 JMSTranslationBundle 中用于将您的类识别为提取器。

最后,在生成文件时,我不得不启用提取器。这可以通过配置完成,但在我的例子中,是通过命令行手动完成的

php app/console translation:extract --enable-extractor=NameOfAlias en
// NameOfAlias is the same name as the one defined in the tag of your service

我希望我没有忘记任何步骤(如果是,请随时在评论中回复,我会更新答案)。

快乐编码:-)

关于交响乐 2 : Static function [JMSTranslation],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25731504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com