As a build step of my C++ application (using CMake as a build system) I need to create some template files that should include localized strings.
The strings are available from translators already in the form of gettext .po files (the same that will be used for translation of the application itself).
I therefore need a way to extract the translation to a given English source string from a .po file (either via Bash/shell or via CMake)
What I came up with so far is the following
translated_string=$(
msggrep --msgid -e "^${untranslated_string}$" ${po_file} \
| msgattrib --no-fuzzy \
| grep -A 1 'msgid "${untranslated_string}$"' \
| sed -n 's/msgstr "\(.*\)"/\1/p'
)
Obviously those are a lot of calls for a "simple" function:
msggrep
outputs a .po file that only has the string I wantmsgattrib
makes sure the translation is not "fuzzy" (i.e. needs updating) as I can't use those- Then I manually extract the translation using
grep
andsed
I imagine there has to be a better approach to this? After all gettext does make it easy to translate my application at runtime, but it seems somewhat unflexible at build time...