1

I'm developing on Symfony2 in PHP and I'd like to find all my translation keys in my different twig views to easily list them in my message file.

Basically, in my twig templates, a translated key / text looks like:

{{ "my text" | trans }} or {{ "my_key" | trans }} or {{"my_key"|trans}} or {{"my text"|trans}} 

I'd like to run a .sh script that could list all these keys/texts to be translated in my different template files.

On another project, I had to identify something like lang('MY_KEY') and a command that worked pretty much is the following one:

find . -type f -name '*.php' -execdir egrep -o -- "->lang\('[^']*" {} \; | sed -e "s/^.*->lang('//g" 

Maybe with this same base, how could I easily find my different translations in my files?

Thx a lot for your help!

1 Answer 1

1

I would suggest something to the tune of find . -type f -name '*.php' | xargs egrep -Ho '\{\{[^|]*\|[^}]*\}\}'. This will allow you to find the occurences of your key/text pairs in particular files. Once you've got this list, you can further process it if you want to e.g. normalize the uses and somehow match e.g. {{ "my text" | trans }} and {{ "my_key" | trans }} together as different uses of the same key (I don't know symphony so I don't know if this really is equivalent or not and if such 'collapsing' of keys would make sense).

Sign up to request clarification or add additional context in comments.

4 Comments

Yay, seems nice! How could I just get the {{ }} content, getting the file doesn't really matter and force me to do more treatment :) #thx!
Nice, I've found, using my previous command and your rightfull pattern. Thx dude!
hum, now with my command: find src/ -type f -name '*.twig' -execdir egrep -o -- "\{\{[^|(){]*\|[(trans) ^}]*\}\}" {} \; | sed 's/([a-z ]*).*/\1/' I got {{ "my whith ' quotes' inside trans" | trans }} or {{ 'my other trans' | trans }}. How to get only what's insinde '' or "" ? thx!
Try to pipe the result through egrep -o '"[^"]+"|'"'[^']+'" | sed -r 's@^.(.*).$@\1@' (note the nesting of quotes, it's tricky)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.