Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何本地化人性化的时间格式_Python_Google App Engine_Jinja2_Gettext_Python Babel - Fatal编程技术网

Python 如何本地化人性化的时间格式

Python 如何本地化人性化的时间格式,python,google-app-engine,jinja2,gettext,python-babel,Python,Google App Engine,Jinja2,Gettext,Python Babel,我该如何本地化才能为这样的文章获得良好的人性化时间输出 今天 16:48 或 昨天 17:10 或 12月13日 11:20 但是我如何本地化字符串“今天”和“昨天”,如何从巴别塔获得缩写形式,比如一月而不是一月,二月而不是二月,以及当我知道实体在修改时所在的国家时,如何向该项目添加时区?我有一个变量entity.url告诉我们它是哪个域,因为我的应用程序服务于几个域,所以它基本上只是时区,取决于entity.url(巴西时区和印度时区)我的基本本地化主要是巴西葡萄牙语和英语,但它应该适用于更多

我该如何本地化才能为这样的文章获得良好的人性化时间输出

今天

16:48

昨天

17:10

12月13日

11:20

但是我如何本地化字符串“今天”和“昨天”,如何从巴别塔获得缩写形式,比如一月而不是一月,二月而不是二月,以及当我知道实体在修改时所在的国家时,如何向该项目添加时区?我有一个变量entity.url告诉我们它是哪个域,因为我的应用程序服务于几个域,所以它基本上只是时区,取决于entity.url(巴西时区和印度时区)我的基本本地化主要是巴西葡萄牙语和英语,但它应该适用于更多ie,它应该很容易添加语言和切换到一种语言

谢谢你的任何评论

使现代化 它现在渲染正确,但我不能直接从django的timesince中获得它,因为我还不能使时区工作。但现在只剩下时区了,我想:

def datetimeformat_jinja(value, format='%H:%M / %d-%m-%Y', locale='en'):
    now= datetime.now()
    info = None
    if datetime.date(value) == datetime.date(now):
      info= _('Today')
    elif (now - value).days < 2:
      info= _('Yesterday')
    else:
      month = value.month
      if month == 1:
        info = str(value.day)+' '+_('Jan.')
      elif month == 2:
        info = str(value.day)+' '+_('Feb.')
      elif month == 3:
        info = str(value.day)+' '+_('Mar.')
      elif month == 4:
        info = str(value.day)+' '+_('April')
      elif month == 5:
        info = str(value.day)+' '+_('May')
      elif month == 6:
        info = str(value.day)+' '+_('June')
      elif month == 7:
        info = str(value.day)+' '+_('July')
      elif month == 8:
        info = str(value.day)+' '+_('Aug.')
      elif month == 9:
        info = str(value.day)+' '+_('Sep.')
      elif month == 10:
        info = str(value.day)+' '+_('Oct.')
      elif month == 11:
        info = str(value.day)+' '+_('Nov.')
      else:
        info = str(value.day)+' '+_('Dec.')
    return info+'<br>'+format_time(value, 'hh:mm', locale=locale)
def datetimeformat_jinja(值,格式='%H:%M/%d-%M-%Y',locale='en'):
now=datetime.now()
信息=无
如果datetime.date(值)=datetime.date(现在):
信息=(今天)
elif(现在-值)。天数<2:
信息=(昨天)
其他:
月=value.month
如果月份==1:
info=str(value.day)+''+.'一月'.)
elif月==2:
info=str(value.day)+''+.'二月'.)
elif月==3:
info=str(value.day)+''+.'三月'.)
elif月==4:
info=str(value.day)+''+.'四月')
elif月==5:
info=str(value.day)+''+.'五月')
elif月==6:
info=str(value.day)+''+(('June'))
elif月==7:
info=str(value.day)+''+.'七月')
elif月==8:
info=str(value.day)+''+(八月)
elif月==9:
info=str(value.day)+''+.'九月'.)
elif月==10:
info=str(value.day)+''+.'十月'.)
elif月==11:
info=str(value.day)+''+.'Nov.'
其他:
info=str(value.day)+'+(('Dec'))
返回信息+'
'+格式时间(值'hh:mm',locale=locale)
我必须像上面那样得到翻译后的月的简短形式,例如Dez。对于巴西葡萄牙语中的Dezember),现在它的本地化效果更好:

以上是巴西葡萄牙语的本地化版本

Django有一个很好的timesince功能:-您可以根据需要调整它(可能需要删除翻译部分)。Django有一个很好的timesince功能:-您可以根据需要调整它(可能需要删除翻译部分)。
{{ entity.modified|datetimeformat_jinja(locale='pt_BR') }}
def datetimeformat_jinja(value, format='%H:%M / %d-%m-%Y', locale='en'):
    now= datetime.now()
    info = None
    if datetime.date(value) == datetime.date(now):
      info= _('Today')
    elif (now - value).days < 2:
      info= _('Yesterday')
    else:
      month = value.month
      if month == 1:
        info = str(value.day)+' '+_('Jan.')
      elif month == 2:
        info = str(value.day)+' '+_('Feb.')
      elif month == 3:
        info = str(value.day)+' '+_('Mar.')
      elif month == 4:
        info = str(value.day)+' '+_('April')
      elif month == 5:
        info = str(value.day)+' '+_('May')
      elif month == 6:
        info = str(value.day)+' '+_('June')
      elif month == 7:
        info = str(value.day)+' '+_('July')
      elif month == 8:
        info = str(value.day)+' '+_('Aug.')
      elif month == 9:
        info = str(value.day)+' '+_('Sep.')
      elif month == 10:
        info = str(value.day)+' '+_('Oct.')
      elif month == 11:
        info = str(value.day)+' '+_('Nov.')
      else:
        info = str(value.day)+' '+_('Dec.')
    return info+'<br>'+format_time(value, 'hh:mm', locale=locale)