Search code examples
oracle-databaseinternationalizationcgienvironment-variablesnls

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


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?


Solution

  • 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