mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-08 10:49:08 +00:00
perf: 优化activity log
This commit is contained in:
@@ -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))
|
||||
|
Reference in New Issue
Block a user