perf: 优化activity log

This commit is contained in:
ibuler
2023-02-14 20:01:29 +08:00
parent f147724447
commit fd38f5d89d
16 changed files with 412 additions and 363 deletions

View File

@@ -1,4 +1,4 @@
from django.utils.translation import gettext
from django.utils.translation import gettext, gettext_noop
def translate_value(value):
@@ -12,3 +12,32 @@ def translate_value(value):
tpl, data = value.split(sp, 1)
return gettext(tpl + sp) + data
def i18n_fmt(tpl, *args):
if '%' not in tpl:
raise ValueError('Invalid template, should contains %')
if not args:
return tpl
args = [str(arg) for arg in args]
try:
tpl % tuple(args)
except TypeError:
raise ValueError('Invalid template, args not match: {} {}'.format(tpl, args))
return tpl + ' % ' + ', '.join(args)
def i18n_trans(s):
if ' % ' not in s:
return gettext(s)
tpl, args = s.split(' % ', 1)
args = args.split(', ')
args = [gettext(arg) for arg in args]
return gettext(tpl) % tuple(args)
def hello():
log = i18n_fmt(gettext_noop('Hello %s'), 'world')
print(i18n_trans(log))