Mac下改变系统语言使powerline报错

今天把Mac系统语言改成了英文,打开终端时powerline就非常悲剧地出现了一大串报错

语言
poweline报错

根据图上的信息,powerline调用的python3在解析localename(语言环境)时失败了,也就是字符集的问题。

解析localename的源码如下。

def _parse_localename(localename):

    """ Parses the locale code for localename and returns the
        result as tuple (language code, encoding).

        The localename is normalized and passed through the locale
        alias engine. A ValueError is raised in case the locale name
        cannot be parsed.

        The language code corresponds to RFC 1766.  code and encoding
        can be None in case the values cannot be determined or are
        unknown to this implementation.

    """
    code = normalize(localename)
    if '@' in code:
        # Deal with locale modifiers
        code, modifier = code.split('@', 1)
        if modifier == 'euro' and '.' not in code:
            # Assume Latin-9 for @euro locales. This is bogus,
            # since some systems may use other encodings for these
            # locales. Also, we ignore other modifiers.
            return code, 'iso-8859-15'

    if '.' in code:
        return tuple(code.split('.')[:2])
    elif code == 'C':
        return None, None
    raise ValueError('unknown locale: %s' % localename)

如果不作任何处理,上述代码就会抛出ValueError的异常。在终端里抛出的localname为UTF-8。然而localename的命名是有地区+语言+字符集。很明显,获取的localname没有地区和语言参数。

并且可以推测,由于解析localname的包是python3自带的,大概在任何python项目里使用此方法也会出现这个问题。

解决:我查看了一下系统的地区为China为默认值。改成了US之后,就没有出现localname的解析问题了。

因为对localname并不是非常了解,了解其原理需参考其他对locale详解的文章。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 9.2 添加国际化和本地化 Django提供了完整的国际化和本地化支持。它允许你把应用翻译为多种语言,它会处理特定...
    lakerszhy阅读 4,993评论 0 1
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,805评论 18 399
  • Python的面向对象 类 Class 类变量 Class variable 数据成员 Data member 函...
    JasonJe阅读 4,924评论 0 3
  • 更新时间:2016/5/13 介绍 本文档所提供的编码规范,适用于主要的Python发行版中组成标准库的Pytho...
    超net阅读 11,146评论 0 15
  • 1.要明白知道一个东西和“真正懂得一个东西”的区别。 2.从小就该培养的品质:(1 留意观察,在生活中对于新鲜的事...
    幻无名阅读 10,365评论 0 4

友情链接更多精彩内容