有没有一种简单的方法可以将 HTTP_ACCEPT_LANGUAGE 转换为 Oracle NLS_LANG 设置?

发布于 2024-07-21 03:51:29 字数 999 浏览 7 评论 0原文

当向 Oracle Web 应用程序(基于 mod_plsql 构建)添加国际化功能时,我想解释 HTTP_ACCEPT_LANGUAGE 参数并使用它在 Oracle 会话中设置各种 NLS_* 设置。

例如:

HTTP_ACCEPT_LANGUAGE=de

alter session set nls_territory=germany;
alter session set nls_lang=...

但是,我想你可能会遇到更复杂的事情......

HTTP_ACCEPT_LANGUAGE=en-us,en;q=0.5

人们以前是如何解决此类问题的?

编辑 - 以下是 Curt 的详细回答

感谢 Curt 的清晰详细的回复。 不过,我并没有真正说清楚,因为我实际上是在问是否有任何现有的 Oracle 小部件可以处理这个问题。

我已经开始手动解析 HTTP_ACCEPT_LANGUAGE 变量,并且 - 正如 Curt 在他的回答中指出的 - 有一些微妙的复杂区域。 这感觉就像之前一定已经做过很多次的事情。 当我编写越来越多的代码时,我有一种“我正在重新发明轮子”的感觉。 :)

必须有一个现有的 Oracle 方法来实现这一点 - 可能是 iAS 中的某些方法?

编辑 - 偶然发现了答案

在寻找其他东西时,我偶然发现了 UTL_I18N 包,它正是我所追求的:

是否有一种简单的方法可以将 HTTP_ACCEPT_LANGUAGE 转换为 Oracle NLS_LANG 设置?

When adding internationalisation capabilities to an Oracle web application (build on mod_plsql), I'd like to interpret the HTTP_ACCEPT_LANGUAGE parameter and use it to set various NLS_* settings in the Oracle session.

For example:

HTTP_ACCEPT_LANGUAGE=de

alter session set nls_territory=germany;
alter session set nls_lang=...

However, you could get something more complicated I suppose...

HTTP_ACCEPT_LANGUAGE=en-us,en;q=0.5

How have folks tackled this sort of thing before?

EDIT - following on from Curt's detailed answer below

Thanks for the clear and detailed reply Curt. I didn't really make myself clear though, as I was really asking if there were any existing Oracle widgets that handled this.

I'm already down the road of manually parsing the HTTP_ACCEPT_LANGUAGE variable and - as Curt indicated in his answer - there are a few subtle areas of complexity. It feels like something that must have been done many times before. As I wrote more and more code I had that sinking "I'm reinventing the wheel" feeling. :)

There must be an existing Oracle approach for this - probably something in iAS??

EDIT - stumbled across the answer

While looking for something else, I stumbled across the UTL_I18N package, which does exactly wham I'm after:

Is there an easy way to convert HTTP_ACCEPT_LANGUAGE to Oracle NLS_LANG settings?

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

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

发布评论

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

评论(2

强辩 2024-07-28 03:51:29

当然,如果你正确地分解问题并且一开始就不要雄心勃勃的话,这并不太难。

本质上,您需要两个函数:一个用于解析 HTTP_ACCEPT_LANGUAGE 并生成语言代码,另一个用于获取该代码并生成适当的 set 命令。

前者可能会变得相当复杂;后者可能会变得相当复杂。 如果只给出“en”,您可能想要生成“en-us”,当没有任何内容完全匹配时,您需要处理选择多个选项之一,您需要处理格式错误的标头值,等等。 不要试图一次性解决所有问题:一开始只做一些非常简单的事情,然后再扩展。

另一半或多或少也有同样的情况,生成 set 命令,但这本身就非常简单; 它实际上只是一个查找函数,尽管根据提供给它的内容,它可能会变得更加复杂。

真正决定你的编程体验的成败的因素是你的单元测试。 这是单元测试和测试驱动开发的理想问题。 单元测试将确保当您进行更改时,旧功能仍然有效,并使添加新功能和修复错误变得更容易,因为您只需添加另一个测试,然后就可以从那时起指导您。 (如果您发现在某个时候犯了严重错误,您还会发现更容易完全重写其中一个函数,因为您可以轻松确认新版本没有破坏任何内容。

)在您的环境中进行单元测试可能有点超出了这个问题的范围,但让我添加一些提示。 首先,如果有一个适用于您的环境的单元测试框架(“pl-sql-unit?”),那就太好了。 如果没有,请不要惊慌。 您不需要任何复杂的东西:只需一组输入和预期输出,以及一种通过函数运行它们的方法,然后要么说“一切都OK!” 或显示任何不正确的结果。 您可以编写一个简单的 PL/SQL 函数,从表中读取输入和预期输出,并为您完成此操作。

Sure, and it's not too tough, if you break up the problem properly and don't get to ambitious at first.

You need, essentially, two functions: one to parse the HTTP_ACCEPT_LANGUAGE and produce a language code, and one to take that and generate the appropriate set commands.

The former can get pretty sophisticated; if you're given only 'en', you probably want to generate 'en-us', you need to deal with chosing one of multiple choices when nothing matches perfectly, you need to deal with malformed header values, and so on. Don't try to tackle this all at once: just do something very simple at first, and extend it later.

The same more or less goes for the other half of it, generating the set commands, but this is pretty simple in and of itself anyway; it's really just a lookup function, though it may get a bit more sophisticated depending on what is provided to it.

What will really make or break your programming experience on something like this is your unit tests. This is an ideal problem for unit testing and test-driven development. Unit tests will make sure that when you change things, old functionality keeps working, and make it easier to add new functionality and fix bugs, because you just add another test and you have that to guide you from that point on. (You'll also find it easier to do a complete rewrite of one of the functions if you find out you've gone terribly wrong at some point, because you can easily confirm that the new version isn't breaking anything.)

How you do unit testing in your environment is probably a bit beyond the scope of this question, but let me add a few hints. First, if there's a unit test framework ("pl-sql-unit?") available for your environment, that's great. If not, don't panic. You don't need anything sophisticated: just a set of inputs and expected outputs, and a way to run them through the function and either say "all OK!" or show any incorrect results. You can probably write a single, simple PL/SQL function that reads the inputs and expected outputs from a table and does this for you.

把回忆走一遍 2024-07-28 03:51:29

终于偶然发现了答案。 Oracle 包 UTL_I18N 包含从浏览器语言代码映射到 Oracle NLS 设置的函数:

utl_i18n.map_language_from_iso;
utl_i18n.map_territory_from_iso;

该映射似乎不能很好地应对多语言设置,例如 en-us,en;q=0.5,但只要您只需使用前 5 个字符,该功能似乎就可以正常工作。

HTTP_ACCEPT_LANGUAGE: ar-lb,en-gb;q=0.5
v_language: 
v_territory: 

HTTP_ACCEPT_LANGUAGE: ar-lb
v_language: ARABIC
v_territory: LEBANON

Finally stumbled across the answer. The Oracle package UTL_I18N contains functions to map from the browser language codes to Oracle NLS settings:

utl_i18n.map_language_from_iso;
utl_i18n.map_territory_from_iso;

The mapping doesn't seem to cope very well with multi-language settings, e.g. en-us,en;q=0.5, but as long as you just use the first 5 characters the functions seem to work ok.

HTTP_ACCEPT_LANGUAGE: ar-lb,en-gb;q=0.5
v_language: 
v_territory: 

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