symfony2: how to manage your translations

1.1k views Asked by At

In my symfony2 application, i have set up what's necessary to change the locale. Now I can use the trans features of the bundle and generate the language files under app/resources/translations

That works fine but I feel it's not efficient to edit yml files by hand, and it's advised to use xliff which is absolutely not user friendly.

Plus, in a collaborative environment, I don't see how this could properly work. I'm not sure translators would like git commands.

To those who have already implemented a translation process : how did you do ? how do you organize it ?

Thanks a lot

3

There are 3 answers

0
AudioBubble On

I'm using only YAML files, but they are located in each bundle, not in the app folder. This is the better way if you want to get clean code. If your translation files are in the app folder, you need to extract the content for a bundle you want to use in another project.

If you use a good IDE like PhpStorm, YAML is no problem and the easiest way to edit YAML. Don't forget the symfony plugin for PhpStorm!

EDIT: You can also use this bundle. It provides a web interface for translations: http://jmsyst.com/bundles/JMSTranslationBundle#documentation

0
M Khalid Junaid On

I have implemented manageable translations for one of my project you can refer to My answer similar to this topic what i have done i have created a service which loads all the translations from my repository.I have 2 entities for this

  1. Language

    Language entity holds all the languages which has 2 properties locale and name

    • locale (en,fr,ar ...)
    • name (english,french ...)
  2. LanguageTranslation

    LanguageTranslation entity holds all the translations with properties

    • catalogue
    • translation
    • language (translation language )
    • languageToken (key for translation)

Now all your translations are cached by symfony in prod/dev folder so each time when user updates or adds new translation i have cleared translations cache programmatically i was using sonata admin so i added a call to this function in prePersist and preUpdate you can also use doctrine event listeners prePersist/preUpdate for this

 private function clearLanguageCache(){
        $cacheDir = __DIR__ . "cache folder path"; /** path to app/cache*/
        $finder = new \Symfony\Component\Finder\Finder();
        $finder->in(array($cacheDir . "/dev/translations", $cacheDir . "/prod/translations"))->files();        
        foreach($finder as $file){
            unlink($file->getRealpath());
        }
    } 

This part is missing in my previous answer for clearing cache

0
Joe Yahchouchi On

I used JMSTranslationBundle https://github.com/schmittjoh/JMSTranslationBundle to manage my translations on a project. You use the usual |trans on the variables you want to translate. And then you use a command with app/console for this bundle, and it builds your yml files for you (you have the option to pick the output file type). It also has a translation UI. And if you add new words, no problem, you just rerun the command, it adds the new words to your existing words without losing the original translation.