Alex Rock
Alex Rock

Reputation: 830

Symfony2: check translation, if not, force translator to write it

I am transforming an old app into a new one using Symfony2.

A little part of the text was translated in the old app, where I used a personal library for translation.

The goal is: when the trans filter or {% trans %} block is called in Twig, I wish Symfony2 to check if translation exists. If it does, then it returns the right translation using user's locale. Nothing foreign here, this is how Symfony2 works.

But what I am facing is that my old library works a little bit better: if translation does not exists, then it writes it into a file for translation to be noticed to translators. I was using a little backoffice which we could see all missing translations, so the translators could work with a free mind, and as a developer, I could also develop and show text to be translated with a free mind, as I knew I wouldn't have to add the text myself into the translation file. And the people using the backoffice and adding text in the database could also work with a free mind, knowing that their texts will be automatically written into the files and the translators can see it in their interface.

I know that the Symfony2 console can dump all these translations for it to be written in some files, but there are two problems for me with this system:

  1. First, the missing translations are written the same as the original text with a prefix we can use or not. This is heavy, because it forces me to check whether the prefix is inside the translation to check if it is missing. And also, the shown text on the website would be with this prefix, which I do not want to use.
    In fact, I want the original text to be returned if there is no translation, so there should be an empty string inside the xml or xliff tags. Better for me to check when the string is empty.
  2. Second, most of the text is in the database, and recovered dynamically by my controllers, and I don't want to extract the whole database to check translations. What I need is to write in my files when a data is to be translated. I will navigate myself on the website to "generate" all the translations files, this is easy as any robot can navigate inside a website (sitemaps generator, for example, can help me get all the websites' urls to load them and then force Symfony2 to use the trans filter or block, and then write missing datas into the translation file). Then, when all the pages are loaded, I can expect all the text to be sent and written into the files.
  3. And then, of course, I need a little backoffice which will allow me to check all translations. But I guess friends of Symfony already made a bundle for that (in fact, for Symfony2, there's a bundle for almost everything). This is not a huge point, as I can make it myself (the old app has its system, I can transfer it in the new app).

I really wish to find a solution for this.

Of course, I could implement my old library into Symfony2, and create my own twig translation filter (already done that) but it would be slightly better for me to use native Symfony2 system, or even install another bundle for that inside the vendors directory, as I don't want to handle development depending on bundles and so on (as translation is saved for each bundle).

Do you have a native solution for this? (if there is none, I'll give up and use my own library, but it's not gonna be as pretty as Symfony2 translation system)

EDIT (February 8, 2014) :

I created my own library with a really simple system which is available on my Github page. It simply adds the translation into the files when the translation is not available.

However, I decided to make another library, inspired by this one, but relying on the database. The module is currently in development, but its system is simple:

It loads translation with the native translator, and if the translation is not available (i.e. the native translator returns an empty string), it loads all translations in the database, depending of the translation_domain value. Then, a service (launched either in a controller and command line tool) will write all database translations in files, for the native translator to load them faster, and for them to be stored in the SF2 cache.

Upvotes: 1

Views: 1547

Answers (2)

Alex Rock
Alex Rock

Reputation: 830

⚠️ EDIT: The library I created was Orbitale/TranslationBundle and it had serious flaws, so I stopped maintaining it, and do not recommend using it. Instead, the Symfony translator must be overriden and you will have to deal with translation formats etc. by yourself.

For consistency and maintainability, I changed my expectations and accepted the native Symfony behavior (which is to use fallback locales, and if none has a translation, display the initial text).

Contact me if you'd like advices on how one could achieve this override.


Original answer:

Finally, thanks for helping.

I tried JMSTranslationBundle, and it was breaking all Symfony2's cache.

Finally, I decided to create my own library, and I put it on both Github and Packagist, you can find it on packagist.org it's named orbitale/translation-bundle.

It's brand new, so don't blame me if it's not complete ! :)

Upvotes: 2

aderuwe
aderuwe

Reputation: 1013

I think you may like https://github.com/schmittjoh/JMSTranslationBundle to achieve your goal. It even has the little backoffice thing you want.

Upvotes: 1

Related Questions