tl;dr Copy this in your Atom init.coffee file to quickly change the current spell-checking-language.

Setting Atom spell language using commands
Setting Atom spell language using commands

About

Atom is a brilliant editor. Not just because of all the things it can do but especially because of the things it could do. It’s easy to add functionality by installing a package. However, what I really learn to love is adding things into init.coffee.

Let me show you my code to quickly switch between spell-checking languages.

Preparation

Basically we just need to download and install Atom and make sure we have the package Spell Check installed. Further we need at least two spell-checker languages installed. In my case it’s de_DE and en_US.

Solution I: Switch language manually

Naturally the spell-checker package offers a way to change the language. To do so we can simply change the strings in the package’s config. Like so:

Setting Atom spell language manually
Setting Atom spell language manually

That's too much, man. Also I don't like the option to use two languages at the same time.

Solution II: Add a command in init.coffee

The very same thing we do in Solution I we can automate by adding a few lines to the init.coffee file.

atom.commands.add 'atom-workspace', 'Spell-DE', ->
  atom.config.set('spell-check.locales', ['de-DE']);
  atom.notifications.addSuccess('Switched spell check to: de-DE')
  
atom.commands.add 'atom-workspace', 'Spell-EN', ->
  atom.config.set('spell-check.locales', ['en-US']);
  atom.notifications.addSuccess('Switched spell check to: en-US')

With this we can just hit CMD+SHIFT+P or CTRL+SHIFT+P and type spell-en. The result looks like this:

Setting Atom spell language using commands
Setting Atom spell language using commands

Solution II++: Add a command in init.coffee with feedback

Solution II is handy. A thing that makes it bad is that there is only a short message-box after the change that tells you which spell-checker-language is set.

To improve that we can simply add the following lines:

# Add a label to the status bar
initLangLabel = ->
  selectedLang = atom.config.get('spell-check.locales');
  bar = document.querySelector('status-bar .status-bar-right')
  spellSelect = document.createElement('a');
  labelNode = document.createTextNode('-');
  spellSelect.appendChild(labelNode);
  spellSelect.setAttribute('class', 'line-ending-tile inline-block')
  bar.insertBefore(spellSelect, bar.firstChild);
  atom.config.observe('spell-check.locales', {}, ->
    selectedLang = atom.config.get('spell-check.locales');
    if selectedLang.length > 0
      labelNode.nodeValue = selectedLang[0];
    else
      labelNode.nodeValue = '-'
  );
  
initLangLabel()

# Adding short cuts
atom.commands.add 'atom-workspace', 'Spell-DE', ->
  atom.config.set('spell-check.locales', ['de-DE']);
  atom.notifications.addSuccess('Switched spell check to: de-DE')
  
atom.commands.add 'atom-workspace', 'Spell-EN', ->
  atom.config.set('spell-check.locales', ['en-US']);
  atom.notifications.addSuccess('Switched spell check to: en-US')

Spell-checking-language-label
Spell-checking-language-label

That’s it. Now we have a small label in the status bar to always know for sure which spell-checking-language is set.

Final thoughts

If there is time this could be modified into a Atom package. That would allow me to use more advance UI features like switching the spell-language when clicking on the label.