nicksnyder / go-i18n

Translate your Go program into multiple languages.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot use "golang.org/x/text/language" when downloaded dependencies via "go mod vendor"

ijajmulani opened this issue · comments

I downloaded dependencies via "go mod vendor".

# ls go-i18n/v2/vendor/
github.com/  golang.org/  gopkg.in/    modules.txt

# ls go-i18n/v2/vendor/golang.org/x/text/
AUTHORS       CONTRIBUTORS  internal/     language/     LICENSE       PATENTS`
package main

import (
        "golang.org/x/text/language"
        i18n "github.com/nicksnyder/go-i18n/v2/i18n"
)
func main() {
        //Read all translation files in the given path
        bundle := i18n.NewBundle(language.English)
}

Getting below error
src/umi/umi.go:133:26: cannot use "golang.org/x/text/language".English (type "golang.org/x/text/language".Tag) as type "github.com/nicksnyder/go-i18n/v2/vendor/golang.org/x/text/language".Tag in argument to i18n.NewBundle

I don't have experience using go mod vendor but it appears like the issue you are experiencing is not specific to this library.

All I can say is that it seems to work for me:

$ mkdir foo
$ cd foo
$ vim main.go
$ go mod init example.com/foo
go: creating new go.mod: module example.com/foo
go: to add module requirements and sums:
	go mod tidy
$ go mod tidy
go: finding module for package github.com/nicksnyder/go-i18n/v2/i18n
go: finding module for package golang.org/x/text/language
go: downloading github.com/nicksnyder/go-i18n v1.10.1
go: downloading golang.org/x/text v0.3.6
go: downloading github.com/nicksnyder/go-i18n/v2 v2.1.2
go: found github.com/nicksnyder/go-i18n/v2/i18n in github.com/nicksnyder/go-i18n/v2 v2.1.2
go: found golang.org/x/text/language in golang.org/x/text v0.3.6
$ go mod vendor
$ ls
go.mod	go.sum	main.go	vendor
$ go build
$ go build -mod=vendor

My project have GO111MODULE=off and I have direct dependencies of github.com/nicksnyder/go-i18n/v2/i18n and golang.org/x/text/language. I wanted to download all the dependency of github.com/nicksnyder/go-i18n/v2/i18n in its vendor folder.

golang.org/x/text/language is use for other purpose also.

$ cd ~/go/src
$ ls
main.go    golang.org

$ git clone github.com/nicksnyder/go-i18n
$ git checkout tags/v2.1.2
$ cd ~/go/src/github.com/nicksnyder/go-i18n/v2
$ go mod vendor
go: downloading github.com/BurntSushi/toml v0.3.0
$ ls vendor/
github.com/  golang.org/  gopkg.in/    modules.txt

$ cd ~/go/src
$ ls
main.go                 github.com                  golang.org
$ ls github.com/nicksnyder/go-i18n/v2/vendor/
github.com  golang.org  gopkg.in  modules.txt
$ cat main.go
package main

import (
        "fmt"

        i18n "github.com/nicksnyder/go-i18n/v2/i18n"
        "golang.org/x/text/language"
)

func main() {
        //Read all translation files in the given path
        bundle := i18n.NewBundle(language.English)
        fmt.Println(bundle)
}

$ go build main.go
# command-line-arguments
./main.go:12:26: cannot use "golang.org/x/text/language".English (type "golang.org/x/text/language".Tag) as type "github.com/nicksnyder/go-i18n/v2/vendor/golang.org/x/text/language".Tag in argument to i18n.NewBundle

So question is, Can we handle dependency of golang.org/x/text/language internally, So user need not have to direct import golang.org/x/text/language.

User should only use import "github.com/nicksnyder/go-i18n/v2/i18n" and let this library handle sub dependency internally.

go mod vendor is a feature of go modules, so it isn’t going to do what you want if you don’t have go modules enabled.

If you want to vendor but aren’t using go modules, then you will need to use some other dependency management tool like dep (https://github.com/golang/dep) or manually rewrite import paths inside of vendor to point to the vendored version of the language package.

This is a good read: https://research.swtch.com/vgo-intro#vendoring_and_reproducible_builds

The whole reason go modules exist is exactly to help you solve the problem you are having here, so if you can, then I would recommend using go modules.