This looks way too complex and hard to maintain for the future, the whole check can be written as:

    var lang = (userLanguage.indexOf(keyboardName) == 0) ? userLanguage : keyboardName;

exactly the way I wrote it for pull request #9774: if `keyboardName` is 'en' then `userLanguage` which is 'en-US' will be used and for all other language codes the `keyboardName` will be formatted properly (even Portuguese whose keyboard layout although being called `pt_BR`, is set using `pt-BR` (see [keyboard.js#L454](https://github.com/mozilla-b2g/gaia/blob/v1-train/apps/keyboard/js/keyboard.js#L454) in `v1-train`).

This way you also don't need the `langAlias` variable (which, if needed, can be generated from the `keyboardAlias` one).