gpt4 book ai didi

c# - 需要一个关于如何从 Accept-Language 请求 header 中获取首选语言的示例

转载 作者:搜寻专家 更新时间:2023-10-30 21:14:49 24 4
gpt4 key购买 nike

我需要一个代码示例或库来解析 Accept-Language header 并返回我首选的语言。 RFC2616指出:

The Accept-Language request-header field is similar to Accept, but restricts the set of natural languages that are preferred as a response to the request. Language tags are defined in section 3.10.

   Accept-Language = "Accept-Language" ":"
1#( language-range [ ";" "q" "=" qvalue ] )
language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )

Each language-range MAY be given an associated quality value which represents an estimate of the user's preference for the languages specified by that range. The quality value defaults to "q=1".

进一步阅读表明有太多的“可选”、“应该”、“可能”和其他轮词阻止我重新发明轮子——我只想知道用户喜欢什么语言,任何浏览器都会回答这个问题每天提问十亿次。

任何语言的任何代码片段(Lisp 和汇编程序除外)都会有所帮助。

非常感谢!

最佳答案

解决方案:

namespace ConsoleApplication
{
using System;
using System.Linq;
using System.Net.Http.Headers;

class Program
{
static void Main(string[] args)
{
string header = "en-ca,en;q=0.8,en-us;q=0.6,de-de;q=0.4,de;q=0.2";
var languages = header.Split(',')
.Select(StringWithQualityHeaderValue.Parse)
.OrderByDescending(s => s.Quality.GetValueOrDefault(1));
}
}
}

结果:

enter image description here

关于c# - 需要一个关于如何从 Accept-Language 请求 header 中获取首选语言的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9927871/

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