How to generate translation file (.po, .xliff, .yml,...) from a Symfony2/Silex project?
Asked Answered
C

3

6

Im going to build a Silex/Symfony2 project and I have been looking around for a method to generate XLIFF/PO/YAML translation files based on texts-to-be-translated inside the project but not found any instruction or documentation on it.

My question is: Is there an automated way to generate translation file(s) in specific format for a Symfony2/Silex project?

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

If no, please tell me how to create translation file(s) then adding up more text for my project? I am looking for an editor desktop based or web-based instead of using normal editor such as Transifex, GetLocalization (but they dont have option to create a new file or add more text)

Concerned answered 22/4, 2013 at 5:18 Comment(0)
C
11

After a long time searching the internet, I found a good one:

https://github.com/schmittjoh/JMSTranslationBundle

Concerned answered 22/4, 2013 at 6:40 Comment(2)
I've got an .xlxs file that needs changing into .xliff - do you know how this might be achieved? Thanks.Minor
This does not work with Twig 2.0, until JMS 1.3.1Electrotonus
G
7

I see you've found a converter, but to answer your first question about generating your initial translation file -

If you have Gettext installed on your system you could generate a PO file from your "texts-to-be-translated inside the project". The command line program xgettext will scan the source files looking for whatever function you're using.

Example:
To scan PHP files for instances of the trans method call as shown here you could use the following command -

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

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

I see you've already found a converter tool. You may also like to try the one I wrote for Loco. It supports PO to YAML, and PO to XLIFF

Glomeration answered 9/5, 2013 at 10:22 Comment(2)
I think it is not possible to handle symfonys pluralisation syntax with xgettext.Textual
This is true. Gettext's limited plural syntax isn't compatible with Symfony's more expressive syntax. You'd have to embed the special syntax in the actual strings. Although that goes for any format you use with Symfony including the recommended XLIFF formatGlomeration
G
1

Workaround for busy people (UNIX)

You can run the following command in the Terminal:

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

This will output the list of 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 from here...

Obviously you must tweak the command to your liking (transchoice, double-quotes instead of single...).

Not ideal but 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 part of a matching line that matches PATTERN.
  • grep -h, --no-filename: Suppress the prefixing of filenames on output when multiple files are searched.

(source)

Greave answered 8/6, 2015 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.