Hi everyone,
I’m maintaining an old Django project that uses translations and is split over ~20 apps. Currently, each app has their own distinct translations (in <app>/locale/...
).
I’m not too happy about this setup anymore and I’d like to use a single translation directory for the whole project.
If the project was brand new I could start with LOCALE_PATHS = ['/project/locale']
in my settings and be done with it.
But how can I convert all the existing translations into a single .po
file without losing them? If it helps, I only have translation for a single language (fr
).
If you are using a versioning system like Git or Mercurial there is nothing to fear, you can try using the path for a single translation and in the case it fails return to the previous state.
If you are not using version control it could be a good reason to start with.
Indeed I am using git for my project so I’m not worried about losing the translations in that sense.
My issue is more about how to consolidate all the app-level .po
files into a single project-level one. Once I phrased it that way, I realized that there was nothing django-specific about my problem and that it was more of a gettext question.
A few search queries later, I found out that I can use msgcat
to merge multiple .po
files into one.
So here’s what I did:
# create the project-level translation directory (and point to it in settings.LOCALE_PATHS)
mkdir myproject/locale
# merge all .po files into one (works because I only have one language)
git ls-files '*.po' | msgcat -f- -o myproject/locale/fr/LC_MESSAGES/django.po
# bonus: delete all app-level locale directories
git ls-files '*.po' | xargs dirname | xargs dirname | xargs dirname | xargs git rm -r
There’s still a bit of manual cleanup to do in the final .po
file (some headers are duplicated and some duplicated strings are marked as fuzzy) but overall it works.
1 Like