Ugettext 未在视图中提供正确的值(“缓存”旧语言值)

发布于 2024-12-29 03:17:48 字数 1725 浏览 2 评论 0原文

我有一个相当简单的信息网站,由 Django 运行。

它的页面(视图)由 render_to_response 快捷方式呈现,使用方式如下:

def index(request):  
    return render_to_response(**create_render_args('Index.html', request))

def somepage(request):
    return render_to_response(**create_render_args('Somepage.html', request, _(u'Some page title'), 'somepage.css'))

# other pages are also rendered in that way

其中 create_render_args 函数是:

def create_render_args(html, request, title = _(u'Default title'), page_css = ''):
    return {
        'template_name' : html,
        'dictionary' :
            {
                'title' : title,
                'page_css' :  '' if page_css == '' else '<link rel="stylesheet" href="/css/{}" />'.format(page_css),
                # other template variables...
            }, 
        'context_instance' : RequestContext(request)
    }

本质上,这是我用来避免额外代码、避免创建额外模板块并且仍然能够指定唯一 CSS 和网站翻译成的每种语言的本地化页面标题(已本地化为 4 种)。也许有更好的方法可以做到这一点,但我必须承认我的 Django 经验在这件事上仍然相当有限 - 我想听到任何客观的批评。

问题是,当我使用 ugettext 作为 _ 时,并且我导航/切换语言/重新加载页面时,似乎有时模板中使用的“标题”变量会被获取即使页面本身已经翻译成新语言,仍“卡在”旧语言中。

例如,如果该网站是意大利语,我打开主页,标题是正确的。 然后我切换到英语(我处理语言切换的 JavaScript 重新加载页面),标题仍然是意大利语,尽管其他所有内容都是英语。我导航到另一个页面并再次导航到主页,重新加载页面几次 - 标题是意大利语,页面是英语。我转到服务器控制台,触摸 wsgi 文件(手动重新加载 Django) - 下次重新加载时主页是全英文的。但随后,我再次将语言切换为意大利语 - 页面是意大利语,标题是英语。

有时行为甚至更奇怪 - 如果我继续按 F5 重新加载页面,我可能会连续两次从前一种语言中获取其“标题”,然后一次出现正确的标题,然后我得到“旧的'标题在以下重新加载时。

现在最困扰我的是 - 如果我使用 ugettext_lazy 作为 _,我完全没有问题。标题始终与页面其余部分使用相同的语言。

据我所知,视图在每个页面视图上被调用,所以我相信我遇到了 - create_render_args() 每个给定页面的输出被缓存在某个地方并始终返回相同的字典- 应该不可能。

这个问题的根源可能是什么?我缺少什么?

I have a rather simple informational website that's being run by Django.

Its' pages(views) are rendered by a render_to_response shortcut that is used like this:

def index(request):  
    return render_to_response(**create_render_args('Index.html', request))

def somepage(request):
    return render_to_response(**create_render_args('Somepage.html', request, _(u'Some page title'), 'somepage.css'))

# other pages are also rendered in that way

Where create_render_args function is:

def create_render_args(html, request, title = _(u'Default title'), page_css = ''):
    return {
        'template_name' : html,
        'dictionary' :
            {
                'title' : title,
                'page_css' :  '' if page_css == '' else '<link rel="stylesheet" href="/css/{}" />'.format(page_css),
                # other template variables...
            }, 
        'context_instance' : RequestContext(request)
    }

Essentialy it's a simple way I use to avoid extra code, avoid creating extra template blocks and still be able to specify unique CSS and a localized page title for each language the site is translated into (it's localized into 4). Maybe there are better ways to do this but I must admit that my Django experience is still rather limited on this matter - I would like to hear any objective criticism.

The problem is that when I use ugettext as _, and I navigate / switch languages / reload pages, it seems that sometimes 'title' variable that is being used in template gets 'stuck' in old language even though the page itself is already translated into the new language.

For example, if the site is in italian, I open the main page, the title is proper.
Then I switch to english (my javascript that handles the language switch reloads the page), the title is still italian even though everything else is in english. I navigate to another page and navigate to main page again, reload the page a few times - title is italian, page is in english. I go to the server console, touch the wsgi file (to reload Django manually) - main page is fully english on the next reload. But then, I switch the language to italian again - the page is in italian, the title is in english.

Sometimes the behavior is even more odd — if I keep reloading the page by pressing F5, I might get its' title from the previous language 2 times in a row, then the correct title gets out for a single time, and then I get 'old' title on the following reloads.

Now what troubles me the most - if I use ugettext_lazy as _, I have no problems at all. The title is always in the same language as the rest of the page.

As far as I know, views are being called on each page view, so something I believe I encounter - create_render_args()'s output for each given page being cached somewhere and returning the same dictionary all the time - shouldn't be possible.

What could be the source of this problem? What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ι不睡觉的鱼゛ 2025-01-05 03:17:48

这又是旧的 Python 默认参数的问题。

create_render_args 函数的默认值在函数首次导入时评估,这可能是在服务器进程启动时。 WSGI 中的进程持续多个请求,这解释了为什么值在视图之间缓存,但通常有多个进程同时运行,这解释了为什么有时您会看到正确的版本。

我不确定为什么使用 ugettext_lazy 不是你的答案 - 这似乎正是它的用例 - 但否则你可以只使用原始文本本身作为默认值并执行 _ 在函数内调用。

This is the old Python default arguments gotcha once again.

The default values for the create_render_args function are evaluated when the function is first imported, which is probably when the server process starts up. Processes in WSGI last for multiple requests, which explains why the value is cached between views, but there are usually multiple processes running at once, which explains why sometimes you see the correct version.

I'm not sure why using ugettext_lazy isn't the answer for you - that seems to be exactly its use case - but otherwise you could just use the raw text itself as the default and do the _ call within the function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文