How to create a translation file (.po, .xliff, .yml, ...) from a Symfony2 / Silex project?

I am going to build a Silex / Symfony2 project, and I was looking for a way to generate XLIFF / PO / YAML translation files based on the texts to be translated within the project, but did not find any instructions or documentation for it.

My question is: Is there an automatic way to create translation files (s) in a specific format for a Symfony2 / Silex project?

If so, please tell me how to generate the file, and then update the translation after that.

If not, tell me how to create a translation file and then add more text for my project? I am looking for an editor on the desktop or website instead of the usual editor such as Transifex, GetLocalization (but they have no way to create a new file or add more text)

+6
source share
3 answers

After a long search on the Internet, I found a good one:

https://github.com/schmittjoh/JMSTranslationBundle

+11
source

I see that you found the converter, but to answer your first question about creating the source translation file -

If you have Gettext installed, you can generate a PO file from your “text to translate” within the project. ”The xgettext command-line xgettext will scan the source files that look for any function that you use.

Example:
To scan PHP files for instances of the trans method, it is called as shown here , you can use the following command -

 find . -name "*.php" | xargs xgettext --language=PHP --keyword=trans --output=messages.pot 

To your question about editors:
You can use any PO editor, such as POEdit , to manage your translations, but as you say, you ultimately need to convert the PO file to either XLIFF or the YAML language pack for Symfony.

I see you have already found the converter tool. You can also try the one I wrote for Loco. It supports PO in YAML and PO in XLIFF

+5
source

A workaround for busy people (UNIX)

You can execute the following command in the terminal:

 $ grep -rEo --no-filename "'.+'\|\btrans\b" templates/ > output.txt 

This will list the messages to translate:

 'Please provide your email'|trans 'Phone'|trans 'Please provide your phone number'|trans ... 

I mean almost ... But you can usually do some work here ...

Obviously, you should customize the command to your liking ( transchoice , double quotes instead of single ...).

Not perfect, but it can help!

grep options

  • grep -R, -r, --recursive : read all files under each directory, recursively this is equivalent to the -d recurse option.
  • grep -E, --extended-regexp : interpret PATTERN as an extended regular expression.
  • grep -o, --only-matching : Show only the portion of the corresponding string that matches PATTERN.
  • grep -h, --no-filename : Prevent the output file name prefix when searching for multiple files.

( source )

+1
source

All Articles