Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

Openerp Web Client 中文日期时间 unicode strformat 问题解决方法



  •     默认情况下,Openerp Web Client 使用中文日期时间会报以下错误:

    <br />return format.format_datetime(self.value, kind=self.attrs.get(&#039;type&#039;, &#039;datetime&#039;))<br />...<br />&lt;type &#039;exceptions.UnicodeEncodeError&#039;&gt;: &#039;ascii&#039; codec can&#039;t encode character u&#039;\u5e74&#039; in position 2: ordinal not in range(128) <br />&nbsp; &nbsp; &nbsp; args = (&#039;ascii&#039;, u&#039;%Y\u5e74%m\u6708%d\u65e5&#039;, 2, 3, &#039;ordinal not in range(128)&#039;) <br />&nbsp; &nbsp; &nbsp; encoding = &#039;ascii&#039; <br />&nbsp; &nbsp; &nbsp; end = 3 <br />&nbsp; &nbsp; &nbsp; message = &#039;&#039; <br />&nbsp; &nbsp; &nbsp; object = u&#039;%Y\u5e74%m\u6708%d\u65e5&#039; <br />&nbsp; &nbsp; &nbsp; reason = &#039;ordinal not in range(128)&#039; <br />&nbsp; &nbsp; &nbsp; start = 2<br />
    



        翻了下论坛的帖子,解决方法大多均为在系统管理->翻译->语言->中文,把时间日期格式中的中文去掉。此方法并没有真正解决中文时间日期格式问题,只能算权宜之计吧。

    看了报错信息,根据经验大多就是str 和 unicode 的问题。跟踪代码后,问题定位在 openerp-web-6.0.3/openobject/i18n/format.py 上。主要原因是 strftime 和 strptime 函数参数的编码不一致,form 提交上来的 value 是str 类型,而timeformat 是unicode 类型。因此就抛出异常,报错。

    解决方法:
    1、修改 DT_SERVER_FORMATS 常量 (openerp-web-6.0.3/openobject/i18n/format.py ):

    <br />DT_SERVER_FORMATS = {<br />&nbsp; &#039;datetime&#039; : &#039;%Y-%m-%d %H:%M:%S&#039;.encode(&#039;utf-8&#039;), <br />&nbsp; &#039;date&#039; : &#039;%Y-%m-%d&#039;.encode(&#039;utf-8&#039;), <br />&nbsp; &#039;time&#039; : &#039;%H:%M:%S&#039;.encode(&#039;utf-8&#039;) <br />}<br />
    



    2、修改get_datetime_format 函数 (openerp-web-6.0.3/openobject/i18n/format.py ):

    <br />def get_datetime_format(kind=&quot;datetime&quot;):<br />&nbsp; &nbsp; &quot;&quot;&quot;Get local datetime format.<br /><br />&nbsp; &nbsp; @param kind: type (date, time or datetime)<br />&nbsp; &nbsp; @return: string<br /><br />&nbsp; &nbsp; @todo: cache formats to improve performance.<br />&nbsp; &nbsp; &quot;&quot;&quot;<br />&nbsp; &nbsp; if &#039;lang&#039; in cherrypy.session:<br />&nbsp; &nbsp; &nbsp; &nbsp; # server-defined formatting<br />&nbsp; &nbsp; &nbsp; &nbsp; if kind == &#039;time&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return cherrypy.session&#91;&#039;lang&#039;]&#91;&#039;time_format&#039;].encode(&#039;utf-8&#039;) <br />&nbsp; &nbsp; &nbsp; &nbsp; elif kind == &#039;date&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return cherrypy.session&#91;&#039;lang&#039;]&#91;&#039;date_format&#039;].encode(&#039;utf-8&#039;) <br />&nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (&quot;%(date_format)s %(time_format)s&quot;% cherrypy.session&#91;&#039;lang&#039;]).encode(&#039;utf-8&#039;) <br /><br />&nbsp; &nbsp; # TODO: correctly convert from LDML to POSIX datetime formatting<br />&nbsp; &nbsp; # current converter is trivial and lame and probably very easy to break<br />&nbsp; &nbsp; date_format = _to_posix_format(dates.get_date_format(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; format=&#039;short&#039;, locale=get_locale())).format<br />&nbsp; &nbsp; if kind == &#039;time&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; # Should use dates.get_time_format(locale=get_locale())<br />&nbsp; &nbsp; &nbsp; &nbsp; return &#039;%H:%M:%S&#039;.encode(&#039;utf-8&#039;) <br />&nbsp; &nbsp; elif kind == &#039;date&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; return date_format.encode(&#039;utf-8&#039;) <br />&nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; # Should use dates.get_datetime_format, but that one returns<br />&nbsp; &nbsp; &nbsp; &nbsp; # a 2.6-style formats<br />&nbsp; &nbsp; &nbsp; &nbsp; return (&quot;%s %s&quot; % (date_format, &#039;%H:%M:%S&#039;)).encode(&#039;utf-8&#039;) <br />
    



    修改完成后,重启oe server 和 oe web client ,即可正常使用中文日期时间。如果你懒的修改上面文件,也可以下载附件,覆盖openerp-web-6.0.3/openobject/i18n/format.py 皆可。

    以上在 openerp server 6.03 和 openerp web client 6.0.3 , on debian 6.1 测试通过。



  • 我记得GTK上也遇到过这个问题。



  • [quote author=Jeff link=topic=2634.msg8779#msg8779 date=1318767366]
    我记得GTK上也遇到过这个问题。
    [/quote]

    刚才测试了下 openerp-client-6.0.3 on debian 6.1 ,保存和显示都没有问题啊。



  • 终于有个正解了,呵呵!