gpt4 book ai didi

Java 8 - 语言环境查找行为

转载 作者:太空狗 更新时间:2023-10-29 22:55:42 29 4
gpt4 key购买 nike

在 Java 8 中引入,Locale.lookup(),基于 RFC 4647,允许用户根据优先级列表为 Locale 列表找到最佳匹配LocaleRange。现在我不明白这种方法的每一个极端情况。下面是一个特例,我想解释一下:

// Create a collection of Locale objects to search
Collection<Locale> locales = new ArrayList<>();
locales.add(Locale.forLanguageTag("en-GB"));
locales.add(Locale.forLanguageTag("en"));

// Express the user's preferences with a Language Priority List
String ranges = "en-US;q=1.0,en-GB;q=1.0";
List<Locale.LanguageRange> languageRanges = Locale.LanguageRange.parse(ranges);

// Find the BEST match, and return just one result
Locale result = Locale.lookup(languageRanges,locales);
System.out.println(result.toString());

这会打印出 en,我直觉上会期望 en-GB

注意:

  • 如果您的范围是 "en-GB;q=1.0,en-US;q=1.0"(GB 和 US 颠倒),这将打印 en-GB,
  • 如果你的范围是 "en-US;q=0.9,en-GB;q=1.0"(GB 的优先级高于 US),这将打印 zh-CN.

有人可以解释这种行为背后的基本原理吗?

最佳答案

如果您提供具有相同优先级的备选语言,列表顺序就变得很重要。当您检查 "en-US;q=1.0,en-GB;q=1.0" 的解析列表时,这一点会变得很明显。它包含两个条目,代表 "en-US;q=1.0",后跟 "en-GB;q=1.0"

参见 https://www.ietf.org/rfc/rfc4647.txt

3.4. Lookup

Lookup is used to select the single language tag that best matches the language priority list for a given request. When performing lookup, each language range in the language priority list is considered in turn, according to priority. … The first matching tag found, according to the user's priority, is considered the closest match and is the item returned. For example, if the language range is "de-ch", a lookup operation can produce content with the tags "de" or "de-CH" but never content with the tag "de-CH-1996". If no language tag matches the request, the "default" value is returned.

In the lookup scheme, the language range is progressively truncated from the end until a matching language tag is located. …

最后一句描述了第一段中已经举例说明的内容,即 de-CH 的语言范围可能匹配 de-CH。对列表中的每个项目执行此回退查找,并在找到匹配项的第一个项目处停止。

换句话说,指定 "en-US;q=1.0,en-GB;q=1.0" 就像指定 "en-US,en,en-GB,en ".


也许你想要的是过滤,看

3.3. Filtering

Filtering is used to select the set of language tags that matches a given language priority list. …

In filtering, each language range represents the least specific language tag (that is, the language tag with fewest number of subtags) that is an acceptable match.

因此,给定您的原始可选语言环境列表

List<Locale> filtered = Locale.filter(
Locale.LanguageRange.parse("en-US;q=1.0,en-GB;q=1.0"), locales);
System.out.println("filtered: "+filtered);

生成 [en_GB]

鉴于

Collection<Locale> locales = Arrays.asList(Locale.forLanguageTag("en"),
Locale.forLanguageTag("en-GB"), Locale.forLanguageTag("en-US"));
List<Locale> filtered = Locale.filter(
Locale.LanguageRange.parse("en-US;q=1.0,en-GB;q=1.0"), locales);
System.out.println("filtered: "+filtered);

生成 [en_US, en_GB](注意优先顺序并且没有 en 回退)。因此,根据上下文,您可能会首先尝试从过滤列表中进行选择,并且仅在过滤列表为空时才求助于查找

至少,Java实现的行为是符合规范的。正如您已经注意到的,更改优先级或更改顺序(当优先级相同时)会根据规范更改结果。

关于Java 8 - 语言环境查找行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30754287/

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