简体   繁体   English

Symfony 6:如何导入bundle translations?

[英]Symfony 6 : how to import bundle translations?

I'm using Symfony 6 and don't understand how I should enable translation for dependency messages.我正在使用 Symfony 6 并且不明白我应该如何为依赖消息启用翻译。 For example: I just installed SymfonyCasts/verify-email-bundle which provides translations in its directory (src/Resources/translations)例如:我刚刚安装了 SymfonyCasts/verify-email-bundle,它在其目录 (src/Resources/translations) 中提供翻译

To enable them, I have:为了启用它们,我有:

  • installed the translation bundle with: composer require symfony/translation安装翻译包: composer require symfony/translation
  • set the default_locale to fr in my config/packages/translation.yaml在我的 config/packages/translation.yaml 中将default_locale设置为fr
  • cleared the cache with bin/console cache:clear使用bin/console cache:clear
  • also tried to manually clear translation cache as stated in other related posts: rm -rf var/log/translations还尝试手动清除翻译缓存,如其他相关帖子所述: rm -rf var/log/translations

Then, all messages that should be handled by the provided translations are still in English.然后,所有应该由提供的翻译处理的消息仍然是英文的。

I have also tried to force translation by calling myself the $translator->trans() method on the string returned by the bundle.我还尝试通过在包返回的字符串上调用自己的$translator->trans()方法来强制进行翻译。 The profiler then says the translation is missing and fallbacks to en as configured.然后探查器说翻译丢失并回退到配置的en

I have tried to copy the bundle VerifyEmailBundle.fr.xlf file into my own /translations directory but got the same error.我试图将捆绑包 VerifyEmailBundle.fr.xlf 文件复制到我自己的 /translations 目录中,但遇到了同样的错误。 bin/console debug:translation fr shows me the needed translations but all are marked as unused . bin/console debug:translation fr向我显示了所需的翻译,但都标记为unused

I encounter the same issue with multiple bundles and don't see anything in the offical documentation about this.我遇到了多个捆绑包的相同问题,并且在官方文档中没有看到任何关于此的内容。

What am I missing?我错过了什么?

try this code in your main Controller class:在您的主 Controller class 中尝试此代码:

private RequestStack $requestStack;

public function __construct(RequestStack $requestStack)
{
    $this->requestStack = $requestStack;
}

private function getRequest(): ?Request
{
    return $this->requestStack->getCurrentRequest();
}

#[Route('/changeLanguage/{lang}', name: 'changeLanguage', requirements: ['lang' => 'fr|en'])]
public function changeLanguage(?string $lang): RedirectResponse
{
    if ($lang)
        $this->getRequest()->getSession()->set('_locale', $lang);

    return $this->redirect(
        $this->getRequest()->headers->get('referer') ??
            $this->generateUrl('main_index')
    );
}

then go to your route with translation然后 go 到你的路线翻译

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

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