跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Odoo 开发与实施交流
  4. Openerp Web Client 中文日期时间 unicode strformat 问题解决方法

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

已定时 已固定 已锁定 已移动 Odoo 开发与实施交流
5 帖子 3 发布者 6.9k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • W 离线
    W 离线
    wangbuke
    写于 最后由 编辑
    #1

        默认情况下,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 测试通过。

    1 条回复 最后回复
    0
    • wjfonhandW 离线
      wjfonhandW 离线
      wjfonhand
      写于 最后由 编辑
      #2

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

      GoodERP -- Odoo China fork

      1 条回复 最后回复
      0
      • W 离线
        W 离线
        wangbuke
        写于 最后由 编辑
        #3

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

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

        1 条回复 最后回复
        0
        • L 离线
          L 离线
          lindongy
          写于 最后由 编辑
          #4

          终于有个正解了,呵呵!

          1 条回复 最后回复
          0

          • 登录

          • 没有帐号? 注册

          • 登录或注册以进行搜索。
          • 第一个帖子
            最后一个帖子
          0
          • 版块
          • 标签
          • 热门
          • 用户
          • 群组