3

Working with Magento Translations today I got very annoyed by the fact that I had to constantly repeat myself.

In our installation we currently have 4 languages:

en_US for English
nl_NL for Dutch
de_DE for German
es_ES for Spanish 

For each of these languages I have to maintain separate app/design/frontend/theme/package/locale/xx_XX/translate.csv files, where the first column - the original language - is the same.

For instance, to translate / modify the original Magento word "Buy" into "Buy now" and the correct translation in these different languages I have to make the following edits in these four files:

locale/en_EN/translate.csv
"Buy", "Buy now"

locale/nl_NL/translate.csv
"Buy", "Nu kopen"

locale/de_DE/translate.csv
"Buy", "Jetzt Kaufen"

locale/es_ES/translate.csv
"Buy", "Comprar ahora"

Is there a smarter way to do this? Maybe working on just one theme_translate.csv that automatically splits into the correct locale/xx_XX/translate.csv's ??

Example theme_translate.csv file: 
"Buy","Buy now","Nu Kopen","Jetzt Kaufen","Comprar ahora"
3

Well yes, there is a smarter way to do this! And it's so easy you start wondering why you didn't think of this 160*4 translated sentences earlier (...)

Using AWK or GNU AWK (gawk) you can quickly select the correct columns and output a new CSV file with just the first (original) column and the correct language column.

It works like this:

awk -F "," '{print $1 "," $3}' source.csv > output.csv

-F "," = use "," as field separator.
{print $1 "," $3} = output the first field $1, then "," followed by the 3rd field $3.
source.csv > output.csv = your source and your output files.

In my case I use these lines as a shell script in my build tool to generate the correct translation files:

awk -F "," '{print $1 "," $2}' ./src/locale/theme_translations.csv > ./dist/app/design/frontend/foundation/default/locale/en_EN/translate.csv
awk -F "," '{print $1 "," $3}' ./src/locale/theme_translations.csv > ./dist/app/design/frontend/foundation/default/locale/nl_NL/translate.csv 
awk -F "," '{print $1 "," $4}' ./src/locale/theme_translations.csv > ./dist/app/design/frontend/foundation/default/locale/de_DE/translate.csv
awk -F "," '{print $1 "," $5}' ./src/locale/theme_translations.csv > ./dist/app/design/frontend/foundation/default/locale/es_ES/translate.csv

HOWEVER If you have fields with commas in your translations ("Order today, enjoy tomorrow") you'll be out of luck with regular awk. You'll need GNU awk as this version is able to use smarter regex patterns as field separator.

If you're on a mac like I am, installation of GNU awk is easy if you already have homebrew installed on your mac: brew install gawk should do the trick.

instead of -F "," your field separator will look like -vFPAT='[^,]*|"[^"]*"' so the command looks like this:

gawk -vFPAT='[^,]*|"[^"]*"' '{print $1 "," $3}' source.csv > output.csv

As my translations contained commas in the fields I ended up using gawk and it looks like this:

gawk -vFPAT='[^,]*|"[^"]*"' '{print $1 "," $2}' ./src/locale/theme_translations.csv > ./dist/app/design/frontend/foundation/default/locale/en_EN/translate.csv
gawk -vFPAT='[^,]*|"[^"]*"' '{print $1 "," $3}' ./src/locale/theme_translations.csv > ./dist/app/design/frontend/foundation/default/locale/nl_NL/translate.csv
gawk -vFPAT='[^,]*|"[^"]*"' '{print $1 "," $4}' ./src/locale/theme_translations.csv > ./dist/app/design/frontend/foundation/default/locale/de_DE/translate.csv
gawk -vFPAT='[^,]*|"[^"]*"' '{print $1 "," $5}' ./src/locale/theme_translations.csv > ./dist/app/design/frontend/foundation/default/locale/es_ES/translate.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.