How to spell check your Android app easily and automatically?

When developing an app, it’s extremely important to have your app spell checked. Any mistake (obvious or not) will make your app look unprofessional. A misspelled word shows a lack of excellence. Users will be led to believe the entire app is of minor quality. In this blog post, I will show how to easily and automatically spell check your app.

In Android, any text appearing in the user interface should be placed in the file res/values/strings.xml:

When developing an app with international support, the strings for other languages must be put into /res/values-$LANG$/strings.xml. For example, to add French translations, place your strings in the file /res/values-fr/strings.xml. Android will take care of choosing the right file. You only have to write the following bit inside your code:

You can actually even define dynamic text:

Once you have externalised all your strings you will need to use a spell checker to make sure everything is correct. I would like to propose Hunspell as the tool to go with. It is based on MySpell, under active development and used in a variety of software packages (Open/LibreOffice, Firefox, PhotoShop, …). Hunspell uses annotated dictionaries: it doesn’t simply compare a target word with a list of allowed words. It can also reason about words. For example, Hunspell’s English dictionary contains the word “work” with specific flags which will make Hunspell understand that the words “rework”, “worked” and “reworked” are also correct without specifically naming them. These flags are defined in an *.aff file. Thus, a Hunspell dictionary consists of two files: The annotated word list and the so called “affix” file. You can download a wide range of dictionaries from OOo’s extension site.

Hunspell itself can be installed via the packet manager of your Linux distribution/Unix flavour. For Windows, you can find binaries here.

Hunspell has a big range of command line options which will help us for our specific use case:

-d [,d2,...]  use d (d2 etc.) dictionaries
-H            HTML input file format
-i enc        input encoding
-l            print misspelled words
-p            dict set dict custom dictionary

The -d switch allows us to run Hunspell against a specific dictionary. Given that our input files are XML, we can use -H to make Hunspell ignore tags. Furthermore, having translations in multiple languages, one is likely to use -i utf8 (or similar) to define the input encoding. Providing a custom dictionary (-p) is handy, too. Specific words like the app’s name should be put into this file (it can be a simple txt file with one word per line without annotations). Using -l prints misspelled words on the screen instead of opening an interactive environment. This is helpful for batch executions.

The basic command will look like this:

Then you only need to run this for every language you support:

It is best to write a script which loops over a list of input files with the associated dictionaries. You can then run this script either in your pre-commit script, before publishing a new version of your app, or whenever else it suits your workflow.


Further links:
Hunspell website
Hunspell dictionary format

Cover image taken from Jeanier Hoade.



Have something to add?

Loading Facebook Comments ...

Leave a Reply