During the last few weeks, I learned about internationalization in bash and did a lot of refactoring/decomposing of the UI code, making the code a lot more DRY.
Instead of hardcoding strings into the whiptail commands, like this:
I set up a Message Catalog in a Portable Object file for English: en.po. The .po file contains msgid-msgstr key-value pairs:
To make these .po files usable in a script, use the Linux utility msgfmt to turn .po files into Message Object files:
Now that we have a Message Object file, we can access the key value pairs in a script using the gettext command. Pass the name of the msgid as a string to get back the localized natural language string:
In order for this to work, though, it’s important to export some environmental variables before running gettext. Mine look like:
gpg-helper-scripts/ ├─ gpg/ │ ├─ create_keys.sh │ ├─ add_uids.py ├─ whiptail/ │ ├─ main_menu.sh │ ├─ gen_keys.sh │ ├─ smartcard_init.sh │ ├─ whiptail_helper.sh ├─ po/ │ ├─ en.po ├─ locale/ │ ├─ en_US/LC_MESSAGES/ui-strings.mo ├─ details/ │ ├─ gpg-algos │ ├─ smartcard-workflow ├─ init.sh ├─ README.md
to store user input as variables while keeping the code DRY.
I also looked a little bit at Google’s Shell Style Guide, gave my files better names and organized the directory structure.
I was curious about the correct abbreviations, key lengths and usages for all of the gpg2.1.16 supported algorithms so I created a reference in details/gpg-algos that will come in handy for validating the user’s input for different types of keys.
Now that the code is much more readable, I can think more about the UI and the options that I want to include:
Lots more options could be added here.. Let me know your ideas.
Thanks for reading!