The language code should be converted explicitly to IETF format at the beginning.  That will guarantee that no matter the input format, this will work.