在没有重启的情况下为django中的所有进程/线程重新加载.mo文件

dig*_*604 5 python django

我们正在为翻译人员编写.po文件编辑器.翻译人员需要了解他们在实时网站上所做的更改.

我们设法重新加载当前进程/线程的.mo文件.但不是每个进程/线程.

如果没有更大的性能问题,是否有可能实现这一目标?

小智 5

我们有同样的问题。用户必须直接在网站上编写翻译。我找到了 django 1.1 的中间件,可以清除翻译缓存,并尝试在 django 1.4 中使用它。命令:

  1. 用户提交翻译
  2. 方法解析表单数据并更改 *.po
  3. -subrocess.Popen(["python", "manage.py", "compilemessages"], stderr=PIPE, stdout=PIPE) (编译更改的 *.po)
  4. 下面的函数可以清除缓存

    from django.utils import translation
    from django.utils.translation import trans_real, get_language
    from django.conf import settings
    import gettext
    
    if settings.USE_I18N:
    
        try:
    
            # Reset gettext.GNUTranslation cache.
            gettext._translations = {}
    
            # Reset Django by-language translation cache.
           trans_real._translations = {}
    
        # Delete Django current language translation cache.
        trans_real._default = None
    
        # Delete translation cache for the current thread,
        # and re-activate the currently selected language (if any)
        translation.activate(get_language())
    except AttributeError:
        pass
    
    Run Code Online (Sandbox Code Playgroud)


Van*_*ale 2

我检查了django-rosetta,正如我怀疑的那样,它们依赖于mod_wsgi AutoReload 机制。这就是我的建议。有关更多详细信息,请参阅重新加载源代码