为什么空语言的地区是“US”?

huangapple go评论70阅读模式
英文:

Why is the region for an empty language "US"?

问题

我正在使用官方/非官方的语言包,并且有以下代码:

package main

import (
	"log"

	"golang.org/x/text/language"
)

func main() {
	reg, _ := language.Tag{}.Region()
	log.Println(reg.Canonicalize()) // US
}

我期望的是 "ZZ",为什么是 "US"?我在文档中找不到相关信息。

英文:

I am using the official/unofficial language package and have this:

package main

import (
	"log"

	"golang.org/x/text/language"
)

func main() {
	reg, _ := language.Tag{}.Region()
	log.Println(reg.Canonicalize()) // US
}

I was expecting "ZZ". Why is it "US"? I can't find anything in the documentation.

答案1

得分: 0

如果你查看方法tag{}.Region()的源代码,你会发现如果没有指定区域,它会尝试添加标签并将可能性设置为低。如果你查看addTags()的实现,你会看到以下代码:

if t.lang == 0 {
  t.lang = _en // 默认语言为英语
}

其中默认语言被设置为英语。谢谢。

英文:

If you look at the source code for the method tag{}.Region() you'll see that if region is not specified it tries to add tags and sets the likelihood to low. addTags() If you look at the implementation for addTags() you'll see

if t.lang == 0 {
  t.lang = _en // default language
}

where the default language is set to English. Cheers.

答案2

得分: 0

如果标签的语言未定义(默认为零),则它只会从likelyLang数组中获取第一个值。

    x := likelyLang[t.lang]
	...
	if x.region != 0 {
		t.setUndefinedScript(scriptID(x.script))
		t.setUndefinedRegion(regionID(x.region))
	}

并且它具有_US(0x134)的地区代码(令人惊讶)。然后将该地区代码复制到输入标签中。

P.S. @reticentroot的答案与此相似。在调查期间没有看到它。

英文:

If tag's language is not defined (zero by default), then it just get the first value from likelyLang array.

    x := likelyLang[t.lang]
	...
	if x.region != 0 {
		t.setUndefinedScript(scriptID(x.script))
		t.setUndefinedRegion(regionID(x.region))
	}

And it has _US (0x134) region code (surprise). After that it copies region to the input tag.

P.S. @reticentroot's answer is about the same. Didn't see it during investigation.

huangapple
  • 本文由 发表于 2017年8月7日 01:51:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/45534941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定