Symfony : 这些消息对于给定的语言环境不可用,并且在后备语言环境中找不到 [英] Symfony : These messages are not available for the given locale and cannot be found in the fallback locales

查看:27
本文介绍了Symfony : 这些消息对于给定的语言环境不可用,并且在后备语言环境中找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 symfony 翻译.这是我的翻译.yml

框架:default_locale: 'en'翻译:路径:- '%kernel.project_dir%/translations'回退:['en']

我在 translations 目录中创建了 messages.en.yml.

## YAML 模板.首页:首页管理员:管理员仪表盘:仪表盘车辆:车辆身份证:身份证

但是当我尝试这样使用时,

{% trans %}仪表板{% endtrans %}

我收到此错误消息,

<块引用>

这些消息不适用于给定的语言环境,并且不能在后备语言环境中找到.将它们添加到翻译目录避免 Symfony 输出未翻译的内容.

解决方案

区域设置由 2 部分(语言和国家/地区)组成,并带有下划线:例如.it_IT 或 en_EN

例如瑞士有 3 种口语:

  • de_CH
  • ch_CH
  • fr_CH

翻译文件的文件名应该是:

  • messages.en_EN.yml
  • messages.it_IT.yml
  • ...

翻译键在每个翻译文件中应该是唯一的、点分隔的并且相等.首先是翻译键,冒号之后是翻译的内容.

# messages.it_IT.ymlnavigation.home: Clicca per casanavigation.help:按 aiuto 的 Clicca#messages.en_EN.ymlnavigation.home:点击首页navigation.help:点击帮助

现在您可以在应用程序中使用导航键,一个好的 IDE 将通过选择它们来帮助您.当对应用程序的不同部分使用不同的文件时,您必须像这样:

{{ 'navigation.home'|trans({}, 'application') }}{{ 'navigation.help'|trans({}, 'application') }}<div class="error-message">发生以下错误:{{ 'error.login'|trans({}, 'messages') }}

I'm trying to use symfony translations. Here is my translation.yml

framework:
    default_locale: 'en'
    translator:
        paths:
            - '%kernel.project_dir%/translations'
        fallbacks: ['en']

I have created messages.en.yml in translations directory.

## YAML Template.
Home: Home
Admin: Admin
Dashboard: Dashboard
Vehicles: Vehicles
Id: Id

But when I try to use like this,

{% trans %}Dashboard{% endtrans %}

I get this error message,

These messages are not available for the given locale and cannot be found in the fallback locales. Add them to the translation catalogue to avoid Symfony outputting untranslated contents.

解决方案

A locale consists of 2 parts (language and country) combined with an underscore: eg. it_IT or en_EN

For example switzerland has 3 spoken languages:

  • de_CH
  • ch_CH
  • fr_CH

The filenames of your translation-files should be like:

  • messages.en_EN.yml
  • messages.it_IT.yml
  • ...

The translation-keys should be unique, dot-separated and equal in each single translation-file. First comes translation-key and after the colon comes the translated content.

# messages.it_IT.yml
navigation.home: Clicca per casa
navigation.help: Clicca per aiuto

# messages.en_EN.yml
navigation.home: Click for home
navigation.help: Click for help

Now you can use the navigation-keys in your application and a good IDE will help you by selecting them. When using different files for the different parts of your application, you have to address them in twig like:

{{ 'navigation.home'|trans({}, 'application') }}
{{ 'navigation.help'|trans({}, 'application') }}

<div class="error-message">
    The following error occured: {{ 'error.login'|trans({}, 'messages') }}
</div>

这篇关于Symfony : 这些消息对于给定的语言环境不可用,并且在后备语言环境中找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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