dongshen6060 2018-01-30 09:56
浏览 128
已采纳

如何从字符串中获取有效的IETF语言标签

I would like to convert locale tag to IETF standard. IETF language tag asserts en-US as a correct tag for American English. In may case, it's possible that I would get en_US as an input.

Currently I'm using this method:

func toLanguageTag(l string) string {
    return strings.Replace(l, "_", "-", -1)
}

For example, I know that Java has toLanguageTag method. Should I continue use this approach, or there is more preferable way to do what I want?

  • 写回答

1条回答 默认 最新

  • dtml3340 2018-01-30 10:05
    关注

    There's a package in the external packages repository of Go (maintained by the Go project but not part of the standard library), that handles parsing and validation of language tags.

    https://godoc.org/golang.org/x/text/language

    Example:

    package main
    
    import (
       "fmt"
       "golang.org/x/text/language"
    )
    
    func main() {
       tag := language.Make("en_us")
       fmt.Println(tag)
    }
    // Output: en-US
    

    Note that language.Make() omits the error in case of an invalid tag. You should use language.Parse() which returns an error in practice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮