如何使用 KnpMenuBundle 和 Symfony3 从缺少的翻译中排除某些字符串 [英] How to exclude certain string from showing up in missing translations using KnpMenuBundle and Symfony3

查看:26
本文介绍了如何使用 KnpMenuBundle 和 Symfony3 从缺少的翻译中排除某些字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介

在我的个人项目中我使用:

  • XAMPP 与 PHP v7.1.6
  • Symfony v3.3.8
  • KnpMnenuBundle dev-master/2.2.x-dev(dev 版本,因为它与当前的 Symfony 3.3.x 版本兼容,更早版本未通过 composer 安装.)

    问题

    在使用 KnpMenuBundle 的情况下,确保特定翻译不包含在缺失部分中的正确方法是什么?

    代码:示例 1

    我的 MenuBuilder

    示例

    $profile->setChildrenAttribute("class", "dropdown-menu")->addChild('以身份登录', array('label' => $getTranslatedLoggedInAs))->setExtra('divider_append', true)-> setExtra('translation_domain', false);

    代码:示例 2

    MenuBuilder

    中获取 loggedInAs 翻译后的字符串

    公共函数 getTranslatedLoggedInAs(){$user = $this->tokenStorage->getToken()->getUser();//转储($用户);$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');}返回 $translated_logged_in_as;}

    结论

    请指教.

    感谢您的时间和知识.

    解决方案

    我想您在 label 块中的 [link 4] 中有错误.translation_domain 未检查 false 并且标签以任何方式翻译.

    {% 块标签 %}{{ item.label|trans(item.getExtra('translation_params', {}),item.getExtra('translation_domain')) }}{% 结束块 %}

    看看它在 KnpMenuBundle 中的工作原理 https://github.com/KnpLabs/KnpMenuBundle/blob/master/Resources/views/menu.html.twig

    {% 块标签 %}{%- set translation_domain = item.extra('translation_domain', 'messages') -%}{%- 设置标签 = item.label -%}{%- 如果 translation_domain 与 (false) 不同 -%}{%- set label = label|trans(item.extra('translation_params', {}), translation_domain) -%}{%- 万一 -%}{%- if options.allow_safe_labels 和 item.extra('safe_label', false) %}{{ label|raw }}{% else %}{{ label }}{% endif -%}{% 结束块 %}

    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.

    解决方案

    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 %}
    

    这篇关于如何使用 KnpMenuBundle 和 Symfony3 从缺少的翻译中排除某些字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆