doumu9019
2017-08-06 17:51
浏览 23
已采纳

为什么该地区使用空语“美国”?

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.

图片转代码服务由CSDN问答提供 功能建议

我正在使用官方/非官方语言包,并具有以下内容:

   package main 
 
import(
“ log” 
 
“ golang.org/x/text/language"
)

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

我期望“ ZZ”。 为什么是“美国”? 我在文档中找不到任何内容。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dskvfdxgdo2422392 2017-08-06 19:31
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doujiazong0322 2017-08-06 19:48

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题