可以用取自 cookie 的语言显示页面吗?

Is it okay to display a page in a language taken from a cookie?

Rails documentation 说:

You may be tempted to store the chosen locale in a session or a cookie. However, do not do this. The locale should be transparent and a part of the URL. This way you won't break people's basic assumptions about the web itself: if you send a URL to a friend, they should see the same page and content as you. A fancy word for this would be that you're being RESTful. Read more about the RESTful approach in Stefan Tilkov's articles. Sometimes there are exceptions to this rule and those are discussed below.

但是 couple of screens above,它教导如何根据用户首选项设置语言环境。这等于从 cookie 中设置它。

我在 Django 文档中没有看到这样的警告。虽然您可能可以避免它,但 storing it in a cookie 似乎是推荐的方法。

可以用从 cookie 中获取的语言显示页面吗?要 cookie 还是不要?

如果没问题,我该如何处理 url?即使语言被 cookie 覆盖,我是否仍然需要针对不同语言使用不同的 url?

UPD 为了清楚起见,我并没有暗示哪个更好,Rails 或 Django。只是那些是我检查以找到答案的 2 个框架。而 Django 建议将其存储在 cookie 中的事实是我的猜测。现在想想,应该不会吧。它只是支持这两个选项。

此外,“不同的 url 用于不同的语言”我的意思是:

我认为这里有几个问题。首先,locale cookie 问题。

要问的主要问题是,您正在使用的 URL 是否特定于某个用户,或者可能由多个用户共享或查看的内容。如果它特定于某个用户(他们的设置页面,或者他们只能在登录后才能看到的其他一些私人页面),那么区域设置是他们设置的一部分是完全合理的。 Cookie在这里很合理

但是,如果语言环境是多个用户会看到的页面的一部分,那么您希望将语言环境作为 URL 的一部分。这样一来,如果我发送给您 https://blah.blah.foo/check/this/out?response=foo&lang=EN,您就会看到我所看到的。

就处理不同语言的不同 URL 而言,您通常保持相同的 routes/views 并依靠 rails 的 I18N 来处理多种语言。但就像其他所有事情一样,您可以按照对您的应用程序最有意义的方式进行操作:)