git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] http: Add Accept-Language header if possible

From
Peter Krefting <peter@softwolves.pp.se>
Date
Jul 9, 2014, 10:40 UTC
Message-ID
<alpine.DEB.2.00.1407091127490.22132@ds9.cixit.se>
In-Reply-To
<1404834846-11812-1-git-send-email-eungjun.yi@navercorp.com>
Yi EungJun:
> Example:
>  LANGUAGE= -> ""
>  LANGUAGE=ko -> "Accept-Language: ko; q=1.000, *; q=0.001"
>  LANGUAGE=ko:en -> "Accept-Language: ko; q=1.000, en; q=0.999, *; q=0.001"

Avoid adding "q=1.000". It is redundant (the default for any unqualified language names is 1.0, and additionally there has historically been some buggy servers that failed if it was included.

> +	p1 = getenv("LANGUAGE");

You need a fallback mechanism here to parse all the possible language variables. I would use the first one I find of these:

  1. LANGUAGE
  2. LC_ALL
  3. LC_MESSAGES
  4. LANG

Only "LANGUAGE" holds a colon-separated list, but the same code can parse all of them, just yielding a single entry for the others.

> +				strbuf_add(buf, p1, p2 - p1);

The tokens are on the form language_COUNTRY.encoding@identifier, whereas Accept-Language wants language-COUNTRY, so you need to a) replace "_" with "-", and b) chop off anything following a "." or "@".

> +				strbuf_addf(buf, "; q=%.3f", q);
> +				q -= 0.001;

Three decimals seems a bit overkill, but some experimentation might be necessary.

> +		strbuf_addstr(buf, "*; q=0.001\r\n");

You should probably also add an explicit "en" here, if none was already included. I've seen some servers break horribly if "en" isn't included.

For reference, I have my LANGUAGE variable set to "sv_SE.utf8:sv:nb_NO.utf8:nb:da_DK.utf8:da:nn_NO.utf8:nn:en_GB.utf8:en_US.utf8:en"

-- 
\\// Peter - http://www.softwolves.pp.se/
Previous: Peter KreftingNext: Yi, EungJun
Message 10 of 12 in “http: Add Accept-Language header if possible”
  1. http: Add Accept-Language header if possibleYi EungJun, Jul 8, 2014
  2. Eric SunshineJul 8, 2014
  3. Jeff KingJul 9, 2014
  4. Yi, EungJunJul 9, 2014
  5. Jeff KingJul 9, 2014
  6. Peter KreftingJul 9, 2014
  7. Jeff KingJul 10, 2014
  8. Yi, EungJunJul 11, 2014
  9. Peter KreftingJul 12, 2014
  10. Peter KreftingJul 9, 2014
  11. Yi, EungJunJul 11, 2014
  12. Yi, EungJunJul 11, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.