Beyond English: Supporting Multiple Languages in Puppet Modules
Puppet was built to output English, but that's about to change. Take a look at how you can have your modules support multiple languages.
Join the DZone community and get the full member experience.Join For Free
If your daily language isn't English, you've probably noticed that the output from supported Puppet modules is displayed in English — and only in English. That's about to change. We are now making changes so that other languages can be available on the Forge. This is part of our internationalization effort to support end users whose first language is not English.
If you're writing a module and you want to make it available in multiple languages, you need to decorate strings that are externalized — that is, displayed to the person using the module. I will illustrate how this works with the MySQL module, by using a solution based on RuboCop.
RuboCop is a static code analyzer that is used to enforce best-practice style guidelines documented in the Ruby Style Guide. It helps to keep code consistent and readable while supporting contributions from multiple authors. It can also be used to help developers identify syntactic errors. RuboCop runs against the Ruby code in a module — that is, against code in the
lib/ folder. (Note that it does not run against Puppet DSL.) It is already enabled for Vox Pupuli modules and the RuboCop template was developed by the Puppet developer experience team for use in the Puppet Development Kit (PDK), and will become the default recommendation for new modules.
The various checks that RuboCop performs on code are known as “cops.” RuboCop allows custom “cops” to be defined. We are going to create a new custom cop that will identify strings that should be marked for translation in the module and decorate those strings, so they can be extracted for translation. The goal is to decorate all the warning and error strings that are displayed to the end user by the module.
How to Run RuboCop
RuboCop is run by typing the command the
bundle exec rubocop command, as illustrated in the screenshot below, which runs the command for the contents of the ‘lib’ folder. This identifies strings that need to be decorated.
Is it also possible for RuboCop to auto-correct the strings that it finds. This can be performed by passing in the -a parameter, as illustrated below. This should be used with caution, as it’s noted as experimental.
How This Affects You
We are starting to externalize strings in the MySQL module, with the aim to add Japanese translations in the near future. Contributors to the MySQL module will notice that it conforms to the RuboCop configuration, as demonstrated by this pull request. RuboCop will now be run by Travis and available as a Rake task in the Puppet Spec Helper.
RuboCop with our custom cop will be enabled on other supported modules in the future in order to have their user-visible strings decorated for translation.
Published at DZone with permission of Davin Hanlon, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.