gpt4 book ai didi

sails.js - 获取当前语言环境

转载 作者:行者123 更新时间:2023-12-01 13:50:07 25 4
gpt4 key购买 nike

问题很简单,但是我在文档中找不到任何相关信息:

如何获取请求的当前语言环境? (用于翻译的语言环境 req.__('to_translate');)

有没有像 req.getLocale(); 这样的东西?

最佳答案

确实,正如您所说,您必须使用 req.getLocale()

引用 sails docs 中的一段

By default, node-i18n will detect the desired language of a request by examining its language headers. Language headers are set in your users' browser settings, and while they're correct most of the time, you may need the flexibility to override this detected locale and provide your own.

所以你应该得到你的语言环境。例如:"en",如果访问您端点的用户将英语配置为其浏览器语言。

现在,如果您在 sails 策略或 Controller 本身中重置区域设置值,如果设置不正确,您可能会得到错误数据,因为我假设您已经尝试过 req.getLocale 方法没有成功,但由于您没有提供更多数据,我只能猜测。

顺便说一句,您可以通过直接访问 locale 变量(从发送到 View 的 locals.locale 对象求值,至少在 jade View 中...)来访问 View 中的当前语言环境...)

关于sails.js - 获取当前语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32480691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com