2

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

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
xXNukem
  • 21
  • 1
  • 6
  • 1) The default name of the translation folder is **translations** and NOT _translation_. 2) if you want the **es** locale you need to make a **messages.es.xlf** file and NOT _messages.fr.xlf_ – gp_sflover Jun 07 '16 at 10:22
  • i know, im just saying that the default file is "messages.fr.xlf", the file that i created is "messages.es.xlf" and the folder name is "translations". But this dont work – xXNukem Jun 07 '16 at 10:26
  • What if you set `es` as default locale `translator: { fallbacks: es }` and put the file in `/app/resources/translations`? Is a fresh Symfony installation or you use some third part bundles? – gp_sflover Jun 07 '16 at 10:30
  • Also don't forget to clear the cache if you change the translations. – Alsatian Jun 07 '16 at 10:39
  • im only using the Paginator Bundle, i tried with the caché cleaning but it doesnt work – xXNukem Jun 07 '16 at 10:42
  • If you want to translate the paginator bundle options in `es` try to make a `PaginatorBundleNameHere.es.xlf` file and put it in `/app/resources/translations` clear the cache and hope to doesn't have some other error somewhere else :-). – gp_sflover Jun 07 '16 at 10:48

1 Answers1

3

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.

pogeybait
  • 3,065
  • 2
  • 21
  • 23