简体   繁体   中英

Translation file in Symfony doesn't exist

I'm using Symfony 3 and I want to make a translation of somethings in my app to Spanish.

I enabled the translator by this way:

framework:
#esi:             ~
#translator:      { fallbacks: ["%locale%"] }
translator:      { fallbacks: en }
secret:          "%secret%"
router:
    resource: "%kernel.root_dir%/config/routing.yml"
    strict_requirements: ~
form:            ~
csrf_protection: ~
validation:      { enable_annotations: true }
#serializer:      { enable_annotations: true }
templating:
    engines: ['twig']
#default_locale:  "%locale%"
default_locale:  es
trusted_hosts:   ~
trusted_proxies: ~
session:

A file called by default "messages.fr.xlf" is supposed to appear in /app/resources/translation or /src/BundleName/resources/translation, but after search in all the folder structure, there is no "messages.fr.xlf" file and there is no "translation" folder, so I decided to create it by myself, and I tried it in all the folders in which this file is supposed to exist.

So my "messages" file contains the following code:

<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="en" datatype="plaintext" original="file.ext">
        <body>
            <trans-unit id="1">
                <source>Next</source>
                <target>Siguiente</target>
            </trans-unit>
            <trans-unit id="2">
                <source>Previous</source>
                <target>Anterior</target>
            </trans-unit>
            <trans-unit id="3">
                <source>client</source>
                <target>Cliente</target>
            </trans-unit>
            <trans-unit id="4">
                <source>user</source>
                <target>Usuario</target>
            </trans-unit>
        </body>
    </file>
</xliff>

But it doesn't work, it seems that symfony can't find the translation file because I get this error:

"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."

Message files need to exist in the app/Resources/translations folder. The files should be named [bundle].[language_code].[format] . Assuming you are using the default bundle "AppBundle", the Spanish language code of "es", and the "xlf" format, your file would be named "AppBundle.es.xlf".

To test your translations create an action in a controller to do something like this:

/**
 * @Route("/controller/test/{_locale}", name="cont_test")
 */
public function testAction(Request $request, $_locale="en")
{
    return $this->render('sometemplate.html.twig'));
}

Then you'd just go to http://domain.com/controller/test/es to test your translation (see twig template below first).

The next thing I noticed about your example is that your trans-unit id's are incorrect. You are using id's that are kind of like auto-generated mysql id's. The Symfony manual suggests using a keyword method for id's. So for example if you have a "name" field on a form you might use the keyword "form.name". So your message file would look something like this:

<trans-unit id="form.name">
    <source>name</source>
    <target>Nombre</target>

Unless you are coding for a large company who may potentially hire translators, the XLF format isn't as desirable as using the YAML version which is much more abbreviated and has many advantages of using that I wont go into here. The YAML version might look like this:

form:
    name: 'Nombre'
    price: 'Precio'

In your twig template you can translate the id something like this:

{% trans_default_domain 'AppBundle' %}
Please provide your {{'form.name'|trans}}

Note the first line in the template is telling the translator to default to looking in your bundle for the translations first before falling back.

Another thing to mention about using translations is that you will need to make sure to have a message translation file created for the language that you default to (for most it is English (en) ). Otherwise when choosing a different language your id's will get translated fine but when coming back to your default language, they will not. So if your default language is english, my example template would translate my sentence something like this:

Please provide your form.name

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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