简体   繁体   English

Symfony 2-双向翻译

[英]Symfony 2 - Bidirectionnal translation

I have to translate a website where the page content can be writen in English or in French. 我必须翻译一个网站,该页面的内容可以用英语或法语书写。
So I made two files in my translations repository: 因此,我在翻译存储库中制作了两个文件:

  • message.en.yml (to translate french to english when the locale is en_us or en_gb ) message.en.yml(当语言环境为en_us或en_gb时将法语翻译为英语
  • message.fr.yml (to translate english to french when the locale is fr ) message.fr.yml(当语言环境为fr时将英语翻译为法语

To explain my issue I will take an exemple, the pair of word Hardware | Matériel 为了说明我的问题,我将举一个例子,这两个词是“ Hardware | Matériel Hardware | Matériel . Hardware | Matériel

When the page is viewed with fr locale Hardware need to be translated by Matériel . 当页面与FR现场查看Hardware需要通过翻译Matériel
So I put : Hardware: Matériel in my message.fr.yml . 所以我在我的message.fr.yml放入了: Hardware: Matériel

When the page is viewed with en_us or en_gb locale Matériel need to be translated by Hardware . 使用en_us或en_gb语言环境查看页面时,需要通过Hardware翻译Matériel
So I put : Matériel: Hardware in my message.en.yml . 所以我把: Matériel: Hardware放在了message.en.yml

At this point the translation behaviour is: 此时的翻译行为是:
When the page is viewed with the fr locale Hardware is correctly translated. 使用fr语言环境查看页面时, Hardware已正确翻译。
When the page is viewed with the en_gb locale Hardware is correctly not translated. 使用en_gb语言环境查看页面时, Hardware未正确翻译。
When the page is viewed with the en_us locale Hardware is incorrectly translated by Matériel . 使用en_us语言环境查看页面时, Matériel错误地翻译了Hardware

To add to the fun, if I clear the Symfony cache, the behaviour change, sometimes everything get translated by Matériel , sometimes nothing does... 更有趣的是,如果我清除了Symfony缓存,则行为发生了变化,有时所有内容都由Matériel翻译,有时则无济于事。
A week ago I only needed to translate French to English and it worked perfectly by just using the message.en.yml file, maybe there is something to do that I didn't noticed when you use several files. 一周前,我只需要将法语翻译为英语,仅通过使用message.en.yml文件,它就可以很好地工作,也许当您使用多个文件时,我没有注意到某些事情。
Can someone explain me how to handle this two way translation? 有人可以解释一下如何处理这两种翻译吗?

Use variables. 使用变量。

I'm using translation like this: 我正在使用这样的翻译:

messages.en.yml messages.en.yml

string.hardware: Hardware

messages.fr.yml messages.fr.yml

string.hardware: Matériel

In twig: 在树枝上:

{{ "string.hardware"|trans }}

Using with dynamic variables: 与动态变量一起使用:

string.hardware: %variable% Hardware

Usage in twig: 树枝中的用法:

{{ "string.hardware"|trans({"%variable%": your_variable_or_string}) }}

Okay, I understood what I was doing wrong. 好吧,我知道我做错了。 For this kind of situation you have to: 对于这种情况,您必须:

  • Put : Hardware: Matériel in my message.fr.yml. 放置: Hardware: Matériel在我的message.fr.yml中。 (if fr locale translate Hardware by Matériel ) (如果语言翻译为Matériel Hardware
  • Put : Matériel: Hardware in my message.en.yml. 放置: Matériel: Hardware我的message.en.yml中为Matériel: Hardware (if en locale translate Matériel by Hardware ) (如果在本地通过Hardware翻译Matériel
  • Put : Hardware: Hardware in my message.en.yml. 输入: Hardware: Hardware我的message.en.yml中的Hardware: Hardware (if en locale don't translate Hardware ) (如果在本地环境中不翻译 Hardware

I think all of this happend because of the translation fallback, I ask for the translation of Hardware so symfony look for one in message.en.yml.first (because the locale is en), if it doens't find any, it looks for it in message.fr.yml, here it found Hardware: Matériel so it use it. 我认为所有这些都是由于翻译回退而发生的,我要求对Hardware进行翻译,因此symfony在message.en.yml.first中查找一个(因为语言环境为en),如果找不到,它看起来它在message.fr.yml中找到了Hardware: Matériel因此它可以使用它。
I still don't get why sometime, depending on the cache, the translation worked fine on en_gb locale... 我仍然不明白为什么有时候,根据缓存的不同,翻译在en_gb语言环境中效果很好...

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

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