How to exclude certain string from showing up in missing translations using KnpMenuBundle and Symfony3

人走茶凉 提交于 2019-12-24 06:04:57

问题


Introduction

In my personal project I am using:

  • XAMPP with PHP v7.1.6
  • Symfony v3.3.8
  • KnpMnenuBundle dev-master / 2.2.x-dev (dev version because it is compatible with current Symfony 3.3.x version, earlier versions did not install via composer.) [link 1], [link 2], [link 3] in order to manage Menus.
  • Bootstrap v3.3.7
  • Bootstrap and KnpMenuBundle integration [link 4]

Setting up

To setup i used documentation in [2], [3] and code samples [4]. My menu is working, integration between Bootstrap and KnpMenuBundle also works.

Problem

I am using Logged in as myTestUserName in user profile part of the menu. And i would like to exclude this item from translation as i am getting full translated string manually (code example 1).

At the moment even with translation_domain explicitly set to false i get said menu item in the missing section of Translation Messages section in the profiler.

Images

Question

What is the correct way of making sure specific translations are not included in missing section in case of using KnpMenuBundle?

CODE: sample 1

sample of my MenuBuilder

$profile->setChildrenAttribute("class", "dropdown-menu")
    ->addChild('Logged in as', array('label' => $getTranslatedLoggedInAs))
    ->setExtra('divider_append', true)
    ->setExtra('translation_domain', false);

CODE: sample 2

getting loggedInAs translated string in MenuBuilder

public function getTranslatedLoggedInAs()
{
    $user = $this->tokenStorage->getToken()->getUser();
    //dump($user);

    $translated_logged_in_as = '';

    $anonymous_user = 'anon.';
    if ($user->getUsername() !== $anonymous_user)
    {
        $translated_logged_in_as = $this->translator->trans(
            'layout.logged_in_as', ['%username%' => $user->getUsername()], 'FOSUserBundle'
        );
    }
    elseif ($user->getUsername() === $anonymous_user)
    {
        $translated_logged_in_as = $this->translator->trans(
            'layout.logged_in_as', ['%username%' => $anonymous_user], 'FOSUserBundle'
        );
    }

    return $translated_logged_in_as;
}

Conclusion

Please advise.

Thank you for your time and knowledge.


回答1:


I suppose you have error in [link 4] in block label. translation_domain is not checked for false and label is translated in any way.

{% block label %}{{ item.label|trans(
    item.getExtra('translation_params', {}),
    item.getExtra('translation_domain')
) }}{% endblock %}

Look how it works in KnpMenuBundle https://github.com/KnpLabs/KnpMenuBundle/blob/master/Resources/views/menu.html.twig

{% block label %}
    {%- set translation_domain = item.extra('translation_domain', 'messages') -%}
    {%- set label = item.label -%}
    {%- if translation_domain is not same as(false) -%}
        {%- set label = label|trans(item.extra('translation_params', {}), translation_domain) -%}
    {%- endif -%}
    {%- if options.allow_safe_labels and item.extra('safe_label', false) %}{{ label|raw }}{% else %}{{ label }}{% endif -%}
{% endblock %}


来源:https://stackoverflow.com/questions/46007289/how-to-exclude-certain-string-from-showing-up-in-missing-translations-using-knpm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!