static String canonicalize(String code, boolean caseSensitive)
    {
        if (code == null || code.length() == 0)
        {
            return null;
        }

        // Support legacy language codes. Map three obsolete language codes
        // { "iw", "ji", "in" } to new official ones { "he", "yi", "id" }.
        final String[] legacy = { "iw", "ji", "in" };
        final String[] official = { "he", "yi", "id" };

        for (int i = 0; i < legacy.length; ++i)
        {
            if (caseSensitive)
            {
                if (code.equals(legacy[i]))
                {
                    return official[i];
                }
            }
            else
            {
                if (code.equalsIgnoreCase(legacy[i]))
                {
                    return official[i];
                }
            }
        }

        if (caseSensitive)
        {
            return code;
        }
        else
        {
            return code.toLowerCase();
        }
    }