pax_global_header 0000666 0000000 0000000 00000000064 13505412006 0014506 g ustar 00root root 0000000 0000000 52 comment=c4faa710783ead174fd0dd6d956fd58e9cddc62c golang-github-nicksnyder-go-i18n.v2-2.0.2/ 0000775 0000000 0000000 00000000000 13505412006 0020113 5 ustar 00root root 0000000 0000000 golang-github-nicksnyder-go-i18n.v2-2.0.2/.codecov.yml 0000664 0000000 0000000 00000000212 13505412006 0022331 0 ustar 00root root 0000000 0000000 coverage: range: 50..100 status: patch: default: threshold: 50% project: default: threshold: 0.1% golang-github-nicksnyder-go-i18n.v2-2.0.2/.gitignore 0000664 0000000 0000000 00000000125 13505412006 0022101 0 ustar 00root root 0000000 0000000 *.a _* output/ .DS_Store *.test *.swp v2/example/example v2/goi18n/goi18n v2/dist/ golang-github-nicksnyder-go-i18n.v2-2.0.2/.golangci.yml 0000664 0000000 0000000 00000000212 13505412006 0022472 0 ustar 00root root 0000000 0000000 # https://github.com/golangci/golangci/wiki/Configuration # service: # prepare: # - go get -t ./... linters: enable: - gofmt golang-github-nicksnyder-go-i18n.v2-2.0.2/.travis.yml 0000664 0000000 0000000 00000000676 13505412006 0022235 0 ustar 00root root 0000000 0000000 language: go matrix: include: - go: 1.9.x env: GO111MODULE=on - go: 1.12.x env: GO111MODULE=on COVER='-coverprofile=coverage.txt -covermode=atomic' script: - cd v2; go test -race $COVER ./... after_success: - bash <(curl -s https://codecov.io/bash) deploy: - provider: script skip_cleanup: true script: cd v2; curl -sL https://git.io/goreleaser | bash on: tags: true condition: $TRAVIS_OS_NAME = linux golang-github-nicksnyder-go-i18n.v2-2.0.2/CHANGELOG.md 0000664 0000000 0000000 00000004544 13505412006 0021733 0 ustar 00root root 0000000 0000000 # Changelog Major version changes are documented in the changelog. To see the documentation for minor or patch version, [view the release notes](https://github.com/nicksnyder/go-i18n/releases). ## v2 ### Motivation The first commit to this project was January 2012 (go1 had not yet been released) and v1.0.0 was tagged June 2015 (go1.4). This project has evolved with the Go ecosystem since then in a backwards compatible way, but there is a growing list of issues and warts that cannot be addressed without breaking compatiblity. v2 is rewrite of the API from first principals to make it more idiomatic Go, and to resolve a backlog of issues: https://github.com/nicksnyder/go-i18n/milestone/1 ### Improvements * Use `golang.org/x/text/language` to get standardized behavior for language matching (https://github.com/nicksnyder/go-i18n/issues/30, https://github.com/nicksnyder/go-i18n/issues/44, https://github.com/nicksnyder/go-i18n/issues/76) * Remove global state so that the race detector does not complain when downstream projects run tests that depend on go-i18n in parallel (https://github.com/nicksnyder/go-i18n/issues/82) * Automatically extract messages from Go source code (https://github.com/nicksnyder/go-i18n/issues/64) * Provide clearer documentation and examples (https://github.com/nicksnyder/go-i18n/issues/27) * Reduce complexity of file format for simple translations (https://github.com/nicksnyder/go-i18n/issues/85) * Support descriptions for messages (https://github.com/nicksnyder/go-i18n/issues/8) * Support custom template delimiters (https://github.com/nicksnyder/go-i18n/issues/88) ### Upgrading from v1 The i18n package in v2 is completely different than v1. Refer to the [documentation](https://godoc.org/github.com/nicksnyder/go-i18n/v2/i18n) and [README](https://github.com/nicksnyder/go-i18n/blob/master/README.md) for guidance. The goi18n command has similarities and differences: * `goi18n merge` has a new implementation but accomplishes the same task. * `goi18n extract` extracts messages from Go source files. * `goi18n constants` no longer exists. Prefer to extract messages directly from Go source files. v2 makes changes to the canonical message file format, but you can use v1 message files with v2. Message files will be converted to the new format the first time they are processed by the new `goi18n merge` command. v2 requires Go 1.9 or newer. golang-github-nicksnyder-go-i18n.v2-2.0.2/LICENSE 0000664 0000000 0000000 00000002075 13505412006 0021124 0 ustar 00root root 0000000 0000000 Copyright (c) 2014 Nick Snyder https://github.com/nicksnyder Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-nicksnyder-go-i18n.v2-2.0.2/README.md 0000664 0000000 0000000 00000011321 13505412006 0021370 0 ustar 00root root 0000000 0000000 # go-i18n [](http://travis-ci.org/nicksnyder/go-i18n) [](https://goreportcard.com/report/github.com/nicksnyder/go-i18n) [](https://codecov.io/gh/nicksnyder/go-i18n) [](https://sourcegraph.com/github.com/nicksnyder/go-i18n?badge) go-i18n is a Go [package](#package-i18n) and a [command](#command-goi18n) that helps you translate Go programs into multiple languages. - Supports [pluralized strings](http://cldr.unicode.org/index/cldr-spec/plural-rules) for all 200+ languages in the [Unicode Common Locale Data Repository (CLDR)](http://www.unicode.org/cldr/charts/28/supplemental/language_plural_rules.html). - Code and tests are [automatically generated](https://github.com/nicksnyder/go-i18n/tree/master/i18n/language/codegen) from [CLDR data](http://cldr.unicode.org/index/downloads). - Supports strings with named variables using [text/template](http://golang.org/pkg/text/template/) syntax. - Supports message files of any format (e.g. JSON, TOML, YAML). ## Package i18n [](http://godoc.org/github.com/nicksnyder/go-i18n/v2/i18n) The i18n package provides support for looking up messages according to a set of locale preferences. ```go import "github.com/nicksnyder/go-i18n/v2/i18n" ``` Create a Bundle to use for the lifetime of your application. ```go bundle := i18n.NewBundle(language.English) ``` Load translations into your bundle during initialization. ```go bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) bundle.LoadMessageFile("es.toml") ``` Create a Localizer to use for a set of language preferences. ```go func(w http.ResponseWriter, r *http.Request) { lang := r.FormValue("lang") accept := r.Header.Get("Accept-Language") localizer := i18n.NewLocalizer(bundle, lang, accept) } ``` Use the Localizer to lookup messages. ```go localizer.Localize(&i18n.LocalizeConfig{ DefaultMessage: &i18n.Message{ ID: "PersonCats", One: "{{.Name}} has {{.Count}} cat.", Other: "{{.Name}} has {{.Count}} cats.", }, TemplateData: map[string]interface{}{ "Name": "Nick", "Count": 2, }, PluralCount: 2, }) // Nick has 2 cats. ``` ## Command goi18n [](http://godoc.org/github.com/nicksnyder/go-i18n/v2/goi18n) The goi18n command manages message files used by the i18n package. ``` go get -u github.com/nicksnyder/go-i18n/v2/goi18n goi18n -help ``` ### Extracting messages Use `goi18n extract` to extract all i18n.Message struct literals in Go source files to a message file for translation. ```toml # active.en.toml [PersonCats] description = "The number of cats a person has" one = "{{.Name}} has {{.Count}} cat." other = "{{.Name}} has {{.Count}} cats." ``` ### Translating a new language 1. Create an empty message file for the language that you want to add (e.g. `translate.es.toml`). 2. Run `goi18n merge active.en.toml translate.es.toml` to populate `translate.es.toml` with the mesages to be translated. ```toml # translate.es.toml [HelloPerson] hash = "sha1-5b49bfdad81fedaeefb224b0ffc2acc58b09cff5" other = "Hello {{.Name}}" ``` 3. After `translate.es.toml` has been translated, rename it to `active.es.toml`. ```toml # active.es.toml [HelloPerson] hash = "sha1-5b49bfdad81fedaeefb224b0ffc2acc58b09cff5" other = "Hola {{.Name}}" ``` 4. Load `active.es.toml` into your bundle. ```go bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) bundle.LoadMessageFile("active.es.toml") ``` ### Translating new messages If you have added new messages to your program: 1. Run `goi18n extract` to update `active.en.toml` with the new messages. 2. Run `goi18n merge active.*.toml` to generate updated `translate.*.toml` files. 3. Translate all the messages in the `translate.*.toml` files. 4. Run `goi18n merge active.*.toml translate.*.toml` to merge the translated messages into the active message files. ## For more information and examples: - Read the [documentation](http://godoc.org/github.com/nicksnyder/go-i18n/v2). - Look at the [code examples](https://github.com/nicksnyder/go-i18n/blob/master/v2/i18n/example_test.go) and [tests](https://github.com/nicksnyder/go-i18n/blob/master/i18n/v2/localizer_test.go). - Look at an example [application](https://github.com/nicksnyder/go-i18n/tree/master/v2/example). ## License go-i18n is available under the MIT license. See the [LICENSE](LICENSE) file for more info. golang-github-nicksnyder-go-i18n.v2-2.0.2/dev.md 0000664 0000000 0000000 00000000622 13505412006 0021213 0 ustar 00root root 0000000 0000000 # Development notes ## How to upgrade CLDR data 1. Go to http://cldr.unicode.org/index/downloads to find the latest version. 1. Download the latest version of cldr-common (e.g. http://unicode.org/Public/cldr/33/cldr-common-33.0.zip) 1. Unzip and copy `v2/common/supplemental/plurals.xml` to `v2/i18n/internal/plural/codegen/plurals.xml` 1. Run `generate.sh` in `v2/i18n/internal/plural/codegen/` golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/ 0000775 0000000 0000000 00000000000 13505412006 0020442 5 ustar 00root root 0000000 0000000 golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/.goreleaser.yml 0000664 0000000 0000000 00000000526 13505412006 0023376 0 ustar 00root root 0000000 0000000 builds: - binary: goi18n main: ./goi18n/ goos: - windows - darwin - linux goarch: - amd64 env: - CGO_ENABLED=0 archives: - format: binary name_template: "{{ .Binary }}-{{ .Version }}-{{ .Os }}-{{ .Arch }}" before: hooks: - go mod download checksum: name_template: "checksums.txt" golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/example/ 0000775 0000000 0000000 00000000000 13505412006 0022075 5 ustar 00root root 0000000 0000000 golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/example/README.md 0000664 0000000 0000000 00000000527 13505412006 0023360 0 ustar 00root root 0000000 0000000 # Example This directory contains an example project that uses go-i18n. ``` go run main.go ``` Then open http://localhost:8080 in your web browser. You can customize the template data and locale via query parameters like this: http://localhost:8080/?name=Nick&unreadEmailCount=2 http://localhost:8080/?name=Nick&unreadEmailCount=2&lang=es golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/example/active.en.toml 0000664 0000000 0000000 00000000612 13505412006 0024645 0 ustar 00root root 0000000 0000000 HelloPerson = "Hello {{.Name}}" [MyUnreadEmails] description = "The number of unread emails I have" one = "I have {{.PluralCount}} unread email." other = "I have {{.PluralCount}} unread emails." [PersonUnreadEmails] description = "The number of unread emails a person has" one = "{{.Name}} has {{.UnreadEmailCount}} unread email." other = "{{.Name}} has {{.UnreadEmailCount}} unread emails." golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/example/active.es.toml 0000664 0000000 0000000 00000001173 13505412006 0024655 0 ustar 00root root 0000000 0000000 [HelloPerson] hash = "sha1-5b49bfdad81fedaeefb224b0ffc2acc58b09cff5" other = "Hola {{.Name}}" [MyUnreadEmails] description = "The number of unread emails I have" hash = "sha1-6a65d17f53981a3657db1897630e9cb069053ea8" one = "Tengo {{.PluralCount}} correo electrónico sin leer" other = "Tengo {{.PluralCount}} correos electrónicos no leídos" [PersonUnreadEmails] description = "The number of unread emails a person has" hash = "sha1-3a672fa89c5c8564bb233c907638004983792464" one = "{{.Name}} tiene {{.UnreadEmailCount}} correo electrónico no leído" other = "{{.Name}} tiene {{.UnreadEmailCount}} correos electrónicos no leídos" golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/example/main.go 0000664 0000000 0000000 00000004606 13505412006 0023356 0 ustar 00root root 0000000 0000000 // Command example runs a sample webserver that uses go-i18n/v2/i18n. package main import ( "fmt" "html/template" "log" "net/http" "strconv" "github.com/BurntSushi/toml" "github.com/nicksnyder/go-i18n/v2/i18n" "golang.org/x/text/language" ) var page = template.Must(template.New("").Parse(`
{{.}}
{{end}} `)) func main() { bundle := i18n.NewBundle(language.English) bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) // No need to load active.en.toml since we are providing default translations. // bundle.MustLoadMessageFile("active.en.toml") bundle.MustLoadMessageFile("active.es.toml") http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { lang := r.FormValue("lang") accept := r.Header.Get("Accept-Language") localizer := i18n.NewLocalizer(bundle, lang, accept) name := r.FormValue("name") if name == "" { name = "Bob" } unreadEmailCount, _ := strconv.ParseInt(r.FormValue("unreadEmailCount"), 10, 64) helloPerson := localizer.MustLocalize(&i18n.LocalizeConfig{ DefaultMessage: &i18n.Message{ ID: "HelloPerson", Other: "Hello {{.Name}}", }, TemplateData: map[string]string{ "Name": name, }, }) myUnreadEmails := localizer.MustLocalize(&i18n.LocalizeConfig{ DefaultMessage: &i18n.Message{ ID: "MyUnreadEmails", Description: "The number of unread emails I have", One: "I have {{.PluralCount}} unread email.", Other: "I have {{.PluralCount}} unread emails.", }, PluralCount: unreadEmailCount, }) personUnreadEmails := localizer.MustLocalize(&i18n.LocalizeConfig{ DefaultMessage: &i18n.Message{ ID: "PersonUnreadEmails", Description: "The number of unread emails a person has", One: "{{.Name}} has {{.UnreadEmailCount}} unread email.", Other: "{{.Name}} has {{.UnreadEmailCount}} unread emails.", }, PluralCount: unreadEmailCount, TemplateData: map[string]interface{}{ "Name": name, "UnreadEmailCount": unreadEmailCount, }, }) err := page.Execute(w, map[string]interface{}{ "Title": helloPerson, "Paragraphs": []string{ myUnreadEmails, personUnreadEmails, }, }) if err != nil { panic(err) } }) fmt.Println("Listening on http://localhost:8080") log.Fatal(http.ListenAndServe(":8080", nil)) } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/go.mod 0000664 0000000 0000000 00000000624 13505412006 0021552 0 ustar 00root root 0000000 0000000 module github.com/nicksnyder/go-i18n/v2 require ( github.com/BurntSushi/toml v0.3.0 golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284 // indirect golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c // indirect golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b // indirect golang.org/x/text v0.3.2 golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c // indirect gopkg.in/yaml.v2 v2.2.1 ) golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/go.sum 0000664 0000000 0000000 00000004014 13505412006 0021574 0 ustar 00root root 0000000 0000000 github.com/BurntSushi/toml v0.3.0 h1:e1/Ivsx3Z0FVTV0NSOv/aVgbUWyQuzj7DDnFblkRvsY= github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.0.0-20171214130843-f21a4dfb5e38 h1:yr7ItWHARpqySNZjEh5mPMHrw3xPR9tMnomFZVcO1mQ= golang.org/x/text v0.0.0-20171214130843-f21a4dfb5e38/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/goi18n/ 0000775 0000000 0000000 00000000000 13505412006 0021547 5 ustar 00root root 0000000 0000000 golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/goi18n/active.en.toml 0000664 0000000 0000000 00000000000 13505412006 0024306 0 ustar 00root root 0000000 0000000 golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/goi18n/common_test.go 0000664 0000000 0000000 00000000250 13505412006 0024422 0 ustar 00root root 0000000 0000000 package main import "io/ioutil" func mustTempDir(prefix string) string { outdir, err := ioutil.TempDir("", prefix) if err != nil { panic(err) } return outdir } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/goi18n/extract_command.go 0000664 0000000 0000000 00000013111 13505412006 0025243 0 ustar 00root root 0000000 0000000 package main import ( "flag" "fmt" "go/ast" "go/parser" "go/token" "io/ioutil" "os" "path/filepath" "strings" "github.com/nicksnyder/go-i18n/v2/i18n" ) func usageExtract() { fmt.Fprintf(os.Stderr, `usage: goi18n extract [options] [paths] Extract walks the files and directories in paths and extracts all messages to a single file. If no files or paths are provided, it walks the current working directory. xx-yy.active.format This file contains messages that should be loaded at runtime. Flags: -sourceLanguage tag The language tag of the extracted messages (e.g. en, en-US, zh-Hant-CN). Default: en -outdir directory Write message files to this directory. Default: . -format format Output message files in this format. Supported formats: json, toml, yaml Default: toml `) } type extractCommand struct { paths []string sourceLanguage languageTag outdir string format string } func (ec *extractCommand) name() string { return "extract" } func (ec *extractCommand) parse(args []string) error { flags := flag.NewFlagSet("extract", flag.ExitOnError) flags.Usage = usageExtract flags.Var(&ec.sourceLanguage, "sourceLanguage", "en") flags.StringVar(&ec.outdir, "outdir", ".", "") flags.StringVar(&ec.format, "format", "toml", "") if err := flags.Parse(args); err != nil { return err } ec.paths = flags.Args() return nil } func (ec *extractCommand) execute() error { if len(ec.paths) == 0 { ec.paths = []string{"."} } messages := []*i18n.Message{} for _, path := range ec.paths { if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if info.IsDir() { return nil } if filepath.Ext(path) != ".go" { return nil } // Don't extract from test files. if strings.HasSuffix(path, "_test.go") { return nil } buf, err := ioutil.ReadFile(path) if err != nil { return err } msgs, err := extractMessages(buf) if err != nil { return err } messages = append(messages, msgs...) return nil }); err != nil { return err } } messageTemplates := map[string]*i18n.MessageTemplate{} for _, m := range messages { if mt := i18n.NewMessageTemplate(m); mt != nil { messageTemplates[m.ID] = mt } } path, content, err := writeFile(ec.outdir, "active", ec.sourceLanguage.Tag(), ec.format, messageTemplates, true) if err != nil { return err } return ioutil.WriteFile(path, content, 0666) } // extractMessages extracts messages from the bytes of a Go source file. func extractMessages(buf []byte) ([]*i18n.Message, error) { fset := token.NewFileSet() file, err := parser.ParseFile(fset, "", buf, parser.AllErrors) if err != nil { return nil, err } extractor := newExtractor(file) ast.Walk(extractor, file) return extractor.messages, nil } func newExtractor(file *ast.File) *extractor { return &extractor{i18nPackageName: i18nPackageName(file)} } type extractor struct { i18nPackageName string messages []*i18n.Message } func (e *extractor) Visit(node ast.Node) ast.Visitor { e.extractMessages(node) return e } func (e *extractor) extractMessages(node ast.Node) { cl, ok := node.(*ast.CompositeLit) if !ok { return } switch t := cl.Type.(type) { case *ast.SelectorExpr: if !e.isMessageType(t) { return } e.extractMessage(cl) case *ast.ArrayType: if !e.isMessageType(t.Elt) { return } for _, el := range cl.Elts { ecl, ok := el.(*ast.CompositeLit) if !ok { continue } e.extractMessage(ecl) } case *ast.MapType: if !e.isMessageType(t.Value) { return } for _, el := range cl.Elts { kve, ok := el.(*ast.KeyValueExpr) if !ok { continue } vcl, ok := kve.Value.(*ast.CompositeLit) if !ok { continue } e.extractMessage(vcl) } } } func (e *extractor) isMessageType(expr ast.Expr) bool { se := unwrapSelectorExpr(expr) if se == nil { return false } if se.Sel.Name != "Message" && se.Sel.Name != "LocalizeConfig" { return false } x, ok := se.X.(*ast.Ident) if !ok { return false } return x.Name == e.i18nPackageName } func unwrapSelectorExpr(e ast.Expr) *ast.SelectorExpr { switch et := e.(type) { case *ast.SelectorExpr: return et case *ast.StarExpr: se, _ := et.X.(*ast.SelectorExpr) return se default: return nil } } func (e *extractor) extractMessage(cl *ast.CompositeLit) { data := make(map[string]string) for _, elt := range cl.Elts { kve, ok := elt.(*ast.KeyValueExpr) if !ok { continue } key, ok := kve.Key.(*ast.Ident) if !ok { continue } v, ok := extractStringLiteral(kve.Value) if !ok { continue } data[key.Name] = v } if len(data) == 0 { return } if messageID := data["MessageID"]; messageID != "" { data["ID"] = messageID } e.messages = append(e.messages, i18n.MustNewMessage(data)) } func extractStringLiteral(expr ast.Expr) (string, bool) { switch v := expr.(type) { case *ast.BasicLit: if v.Kind != token.STRING { return "", false } s := v.Value[1 : len(v.Value)-1] if v.Value[0] == '"' { s = strings.Replace(s, `\"`, `"`, -1) } return s, true case *ast.BinaryExpr: if v.Op != token.ADD { return "", false } x, ok := extractStringLiteral(v.X) if !ok { return "", false } y, ok := extractStringLiteral(v.Y) if !ok { return "", false } return x + y, true default: return "", false } } func i18nPackageName(file *ast.File) string { for _, i := range file.Imports { if i.Path.Kind == token.STRING && i.Path.Value == `"github.com/nicksnyder/go-i18n/v2/i18n"` { if i.Name == nil { return "i18n" } return i.Name.Name } } return "" } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/goi18n/extract_command_test.go 0000664 0000000 0000000 00000012476 13505412006 0026317 0 ustar 00root root 0000000 0000000 package main import ( "bytes" "io/ioutil" "os" "path/filepath" "testing" ) func TestExtract(t *testing.T) { tests := []struct { name string fileName string file string activeFile []byte }{ { name: "no translations", fileName: "file.go", file: `package main`, }, { name: "global declaration", fileName: "file.go", file: `package main import "github.com/nicksnyder/go-i18n/v2/i18n" var m = &i18n.Message{ ID: "Plural ID", } `, }, { name: "escape", fileName: "file.go", file: `package main import "github.com/nicksnyder/go-i18n/v2/i18n" var a = &i18n.Message{ ID: "a", Other: "a \" b", } var b = &i18n.Message{ ID: "b", Other: ` + "`" + `a " b` + "`" + `, } `, activeFile: []byte(`a = "a \" b" b = "a \" b" `), }, { name: "array", fileName: "file.go", file: `package main import "github.com/nicksnyder/go-i18n/v2/i18n" var a = []*i18n.Message{ { ID: "a", Other: "a", }, { ID: "b", Other: "b", }, } `, activeFile: []byte(`a = "a" b = "b" `), }, { name: "map", fileName: "file.go", file: `package main import "github.com/nicksnyder/go-i18n/v2/i18n" var a = map[string]*i18n.Message{ "a": { ID: "a", Other: "a", }, "b": { ID: "b", Other: "b", }, } `, activeFile: []byte(`a = "a" b = "b" `), }, { name: "no extract from test", fileName: "file_test.go", file: `package main import "github.com/nicksnyder/go-i18n/v2/i18n" func main() { bundle := i18n.NewBundle(language.English) l := i18n.NewLocalizer(bundle, "en") l.Localize(&i18n.LocalizeConfig{MessageID: "Plural ID"}) } `, }, { name: "must short form id only", fileName: "file.go", file: `package main import "github.com/nicksnyder/go-i18n/v2/i18n" func main() { bundle := i18n.NewBundle(language.English) l := i18n.NewLocalizer(bundle, "en") l.MustLocalize(&i18n.LocalizeConfig{MessageID: "Plural ID"}) } `, }, { name: "custom package name", fileName: "file.go", file: `package main import bar "github.com/nicksnyder/go-i18n/v2/i18n" func main() { _ := &bar.Message{ ID: "Plural ID", } } `, }, { name: "exhaustive plural translation", fileName: "file.go", file: `package main import "github.com/nicksnyder/go-i18n/v2/i18n" func main() { _ := &i18n.Message{ ID: "Plural ID", Description: "Plural description", Zero: "Zero translation", One: "One translation", Two: "Two translation", Few: "Few translation", Many: "Many translation", Other: "Other translation", } } `, activeFile: []byte(`["Plural ID"] description = "Plural description" few = "Few translation" many = "Many translation" one = "One translation" other = "Other translation" two = "Two translation" zero = "Zero translation" `), }, { name: "concat id", fileName: "file.go", file: `package main import "github.com/nicksnyder/go-i18n/v2/i18n" func main() { _ := &i18n.Message{ ID: "Plural" + " " + "ID", } } `, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { indir := mustTempDir("TestExtractCommandIn") defer os.RemoveAll(indir) outdir := mustTempDir("TestExtractCommandOut") defer os.RemoveAll(outdir) inpath := filepath.Join(indir, test.fileName) if err := ioutil.WriteFile(inpath, []byte(test.file), 0666); err != nil { t.Fatal(err) } if code := testableMain([]string{"extract", "-outdir", outdir, indir}); code != 0 { t.Fatalf("expected exit code 0; got %d\n", code) } files, err := ioutil.ReadDir(outdir) if err != nil { t.Fatal(err) } if len(files) != 1 { t.Fatalf("expected 1 file; got %#v", files) } actualFile := files[0] expectedName := "active.en.toml" if actualFile.Name() != expectedName { t.Fatalf("expected %s; got %s", expectedName, actualFile.Name()) } outpath := filepath.Join(outdir, actualFile.Name()) actual, err := ioutil.ReadFile(outpath) if err != nil { t.Fatal(err) } if !bytes.Equal(actual, test.activeFile) { t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", test.activeFile, actual) } }) } } func TestExtractCommand(t *testing.T) { outdir, err := ioutil.TempDir("", "TestExtractCommand") if err != nil { t.Fatal(err) } defer os.RemoveAll(outdir) if code := testableMain([]string{"extract", "-outdir", outdir, "../example/"}); code != 0 { t.Fatalf("expected exit code 0; got %d", code) } actual, err := ioutil.ReadFile(filepath.Join(outdir, "active.en.toml")) if err != nil { t.Fatal(err) } expected := []byte(`HelloPerson = "Hello {{.Name}}" [MyUnreadEmails] description = "The number of unread emails I have" one = "I have {{.PluralCount}} unread email." other = "I have {{.PluralCount}} unread emails." [PersonUnreadEmails] description = "The number of unread emails a person has" one = "{{.Name}} has {{.UnreadEmailCount}} unread email." other = "{{.Name}} has {{.UnreadEmailCount}} unread emails." `) if !bytes.Equal(actual, expected) { t.Fatalf("files not equal\nactual:\n%s\nexpected:\n%s", actual, expected) } } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/goi18n/main.go 0000664 0000000 0000000 00000007452 13505412006 0023032 0 ustar 00root root 0000000 0000000 // Command goi18n manages message files used by the i18n package. // // go get -u github.com/nicksnyder/go-i18n/v2/goi18n // goi18n -help // // Use `goi18n extract` to create a message file that contains the messages defined in your Go source files. // # en.toml // [PersonCats] // description = "The number of cats a person has" // one = "{{.Name}} has {{.Count}} cat." // other = "{{.Name}} has {{.Count}} cats." // // Use `goi18n merge` to create message files for translation. // # translate.es.toml // [PersonCats] // description = "The number of cats a person has" // hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091" // one = "{{.Name}} has {{.Count}} cat." // other = "{{.Name}} has {{.Count}} cats." // // Use `goi18n merge` to merge translated message files with your existing message files. // # active.es.toml // [PersonCats] // description = "The number of cats a person has" // hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091" // one = "{{.Name}} tiene {{.Count}} gato." // other = "{{.Name}} tiene {{.Count}} gatos." // // Load the active messages into your bundle. // bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) // bundle.MustLoadMessageFile("active.es.toml") package main import ( "flag" "fmt" "os" "golang.org/x/text/language" ) func mainUsage() { fmt.Fprintf(os.Stderr, `goi18n (v2) is a tool for managing message translations. Usage: goi18n command [arguments] The commands are: merge merge message files extract extract messages from Go files Workflow: Use 'goi18n extract' to create a message file that contains the messages defined in your Go source files. # en.toml [PersonCats] description = "The number of cats a person has" one = "{{.Name}} has {{.Count}} cat." other = "{{.Name}} has {{.Count}} cats." Use 'goi18n merge' to create message files for translation. # translate.es.toml [PersonCats] description = "The number of cats a person has" hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091" one = "{{.Name}} has {{.Count}} cat." other = "{{.Name}} has {{.Count}} cats." Use 'goi18n merge' to merge translated message files with your existing message files. # active.es.toml [PersonCats] description = "The number of cats a person has" hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091" one = "{{.Name}} tiene {{.Count}} gato." other = "{{.Name}} tiene {{.Count}} gatos." Load the active messages into your bundle. bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) bundle.MustLoadMessageFile("active.es.toml") `) } type command interface { name() string parse(arguments []string) error execute() error } func main() { os.Exit(testableMain(os.Args[1:])) } func testableMain(args []string) int { flags := flag.NewFlagSet("goi18n", flag.ContinueOnError) flags.Usage = mainUsage if err := flags.Parse(args); err != nil { if err == flag.ErrHelp { return 2 } return 1 } if flags.NArg() == 0 { mainUsage() return 2 } commands := []command{ &mergeCommand{}, &extractCommand{}, } cmdName := flags.Arg(0) for _, cmd := range commands { if cmd.name() == cmdName { if err := cmd.parse(flags.Args()[1:]); err != nil { fmt.Fprintln(os.Stderr, err) return 1 } if err := cmd.execute(); err != nil { fmt.Fprintln(os.Stderr, err) return 1 } return 0 } } fmt.Fprintf(os.Stderr, "goi18n: unknown subcommand %s\n", cmdName) return 1 } type languageTag language.Tag func (lt languageTag) String() string { return lt.Tag().String() } func (lt *languageTag) Set(value string) error { t, err := language.Parse(value) if err != nil { return err } *lt = languageTag(t) return nil } func (lt languageTag) Tag() language.Tag { tag := language.Tag(lt) if tag.IsRoot() { return language.English } return tag } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/goi18n/main_test.go 0000664 0000000 0000000 00000001100 13505412006 0024051 0 ustar 00root root 0000000 0000000 package main import ( "strings" "testing" ) func TestMain(t *testing.T) { testCases := []struct { args []string exitCode int }{ { args: []string{"-help"}, exitCode: 2, }, { args: []string{"extract"}, exitCode: 0, }, { args: []string{"merge"}, exitCode: 1, }, } for _, testCase := range testCases { t.Run(strings.Join(testCase.args, " "), func(t *testing.T) { if code := testableMain(testCase.args); code != testCase.exitCode { t.Fatalf("expected exit code %d; got %d", testCase.exitCode, code) } }) } } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/goi18n/marshal.go 0000664 0000000 0000000 00000003522 13505412006 0023527 0 ustar 00root root 0000000 0000000 package main import ( "bytes" "encoding/json" "fmt" "path/filepath" "github.com/BurntSushi/toml" "github.com/nicksnyder/go-i18n/v2/i18n" "github.com/nicksnyder/go-i18n/v2/internal/plural" "golang.org/x/text/language" yaml "gopkg.in/yaml.v2" ) func writeFile(outdir, label string, langTag language.Tag, format string, messageTemplates map[string]*i18n.MessageTemplate, sourceLanguage bool) (path string, content []byte, err error) { v := marshalValue(messageTemplates, sourceLanguage) content, err = marshal(v, format) if err != nil { return "", nil, fmt.Errorf("failed to marshal %s strings to %s: %s", langTag, format, err) } path = filepath.Join(outdir, fmt.Sprintf("%s.%s.%s", label, langTag, format)) return } func marshalValue(messageTemplates map[string]*i18n.MessageTemplate, sourceLanguage bool) interface{} { v := make(map[string]interface{}, len(messageTemplates)) for id, template := range messageTemplates { if other := template.PluralTemplates[plural.Other]; sourceLanguage && len(template.PluralTemplates) == 1 && other != nil && template.Description == "" && template.LeftDelim == "" && template.RightDelim == "" { v[id] = other.Src } else { m := map[string]string{} if template.Description != "" { m["description"] = template.Description } if !sourceLanguage { m["hash"] = template.Hash } for pluralForm, template := range template.PluralTemplates { m[string(pluralForm)] = template.Src } v[id] = m } } return v } func marshal(v interface{}, format string) ([]byte, error) { switch format { case "json": return json.MarshalIndent(v, "", " ") case "toml": var buf bytes.Buffer enc := toml.NewEncoder(&buf) enc.Indent = "" err := enc.Encode(v) return buf.Bytes(), err case "yaml": return yaml.Marshal(v) } return nil, fmt.Errorf("unsupported format: %s", format) } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/goi18n/merge_command.go 0000664 0000000 0000000 00000021300 13505412006 0024667 0 ustar 00root root 0000000 0000000 package main import ( "crypto/sha1" "encoding/json" "flag" "fmt" "io" "io/ioutil" "os" "github.com/BurntSushi/toml" "github.com/nicksnyder/go-i18n/v2/i18n" "github.com/nicksnyder/go-i18n/v2/internal" "github.com/nicksnyder/go-i18n/v2/internal/plural" "golang.org/x/text/language" yaml "gopkg.in/yaml.v2" ) func usageMerge() { fmt.Fprintf(os.Stderr, `usage: goi18n merge [options] [message files] Merge reads all messages in the message files and produces two files per language. xx-yy.active.format This file contains messages that should be loaded at runtime. xx-yy.translate.format This file contains messages that are empty and should be translated. Message file names must have a suffix of a supported format (e.g. ".json") and contain a valid language tag as defined by RFC 5646 (e.g. "en-us", "fr", "zh-hant", etc.). To add support for a new language, create an empty translation file with the appropriate name and pass it in to goi18n merge. Flags: -sourceLanguage tag Translate messages from this language (e.g. en, en-US, zh-Hant-CN) Default: en -outdir directory Write message files to this directory. Default: . -format format Output message files in this format. Supported formats: json, toml, yaml Default: toml `) } type mergeCommand struct { messageFiles []string sourceLanguage languageTag outdir string format string } func (mc *mergeCommand) name() string { return "merge" } func (mc *mergeCommand) parse(args []string) error { flags := flag.NewFlagSet("merge", flag.ExitOnError) flags.Usage = usageMerge flags.Var(&mc.sourceLanguage, "sourceLanguage", "en") flags.StringVar(&mc.outdir, "outdir", ".", "") flags.StringVar(&mc.format, "format", "toml", "") if err := flags.Parse(args); err != nil { return err } mc.messageFiles = flags.Args() return nil } func (mc *mergeCommand) execute() error { if len(mc.messageFiles) < 1 { return fmt.Errorf("need at least one message file to parse") } inFiles := make(map[string][]byte) for _, path := range mc.messageFiles { content, err := ioutil.ReadFile(path) if err != nil { return err } inFiles[path] = content } ops, err := merge(inFiles, mc.sourceLanguage.Tag(), mc.outdir, mc.format) if err != nil { return err } for path, content := range ops.writeFiles { if err := ioutil.WriteFile(path, content, 0666); err != nil { return err } } for _, path := range ops.deleteFiles { // Ignore error since it isn't guaranteed to exist. os.Remove(path) } return nil } type fileSystemOp struct { writeFiles map[string][]byte deleteFiles []string } func merge(messageFiles map[string][]byte, sourceLanguageTag language.Tag, outdir, outputFormat string) (*fileSystemOp, error) { unmerged := make(map[language.Tag][]map[string]*i18n.MessageTemplate) sourceMessageTemplates := make(map[string]*i18n.MessageTemplate) unmarshalFuncs := map[string]i18n.UnmarshalFunc{ "json": json.Unmarshal, "toml": toml.Unmarshal, "yaml": yaml.Unmarshal, } for path, content := range messageFiles { mf, err := i18n.ParseMessageFileBytes(content, path, unmarshalFuncs) if err != nil { return nil, fmt.Errorf("failed to load message file %s: %s", path, err) } templates := map[string]*i18n.MessageTemplate{} for _, m := range mf.Messages { templates[m.ID] = i18n.NewMessageTemplate(m) } if mf.Tag == sourceLanguageTag { for _, template := range templates { if sourceMessageTemplates[template.ID] != nil { return nil, fmt.Errorf("multiple source translations for id %s", template.ID) } template.Hash = hash(template) sourceMessageTemplates[template.ID] = template } } unmerged[mf.Tag] = append(unmerged[mf.Tag], templates) } if len(sourceMessageTemplates) == 0 { return nil, fmt.Errorf("no messages found for source locale %s", sourceLanguageTag) } pluralRules := plural.DefaultRules() all := make(map[language.Tag]map[string]*i18n.MessageTemplate) all[sourceLanguageTag] = sourceMessageTemplates for _, srcTemplate := range sourceMessageTemplates { for dstLangTag, messageTemplates := range unmerged { if dstLangTag == sourceLanguageTag { continue } pluralRule := pluralRules.Rule(dstLangTag) if pluralRule == nil { // Non-standard languages not supported because // we don't know if translations are complete or not. continue } if all[dstLangTag] == nil { all[dstLangTag] = make(map[string]*i18n.MessageTemplate) } dstMessageTemplate := all[dstLangTag][srcTemplate.ID] if dstMessageTemplate == nil { dstMessageTemplate = &i18n.MessageTemplate{ Message: &i18n.Message{ ID: srcTemplate.ID, Description: srcTemplate.Description, Hash: srcTemplate.Hash, }, PluralTemplates: make(map[plural.Form]*internal.Template), } all[dstLangTag][srcTemplate.ID] = dstMessageTemplate } // Check all unmerged message templates for this message id. for _, messageTemplates := range messageTemplates { unmergedTemplate := messageTemplates[srcTemplate.ID] if unmergedTemplate == nil { continue } // Ignore empty hashes for v1 backward compatibility. if unmergedTemplate.Hash != "" && unmergedTemplate.Hash != srcTemplate.Hash { // This was translated from different content so discard. continue } // Merge in the translated messages. for pluralForm := range pluralRule.PluralForms { dt := unmergedTemplate.PluralTemplates[pluralForm] if dt != nil && dt.Src != "" { dstMessageTemplate.PluralTemplates[pluralForm] = dt } } } } } translate := make(map[language.Tag]map[string]*i18n.MessageTemplate) active := make(map[language.Tag]map[string]*i18n.MessageTemplate) for langTag, messageTemplates := range all { active[langTag] = make(map[string]*i18n.MessageTemplate) if langTag == sourceLanguageTag { active[langTag] = messageTemplates continue } pluralRule := pluralRules.Rule(langTag) if pluralRule == nil { // Non-standard languages not supported because // we don't know if translations are complete or not. continue } for _, messageTemplate := range messageTemplates { srcMessageTemplate := sourceMessageTemplates[messageTemplate.ID] activeMessageTemplate, translateMessageTemplate := activeDst(srcMessageTemplate, messageTemplate, pluralRule) if translateMessageTemplate != nil { if translate[langTag] == nil { translate[langTag] = make(map[string]*i18n.MessageTemplate) } translate[langTag][messageTemplate.ID] = translateMessageTemplate } if activeMessageTemplate != nil { active[langTag][messageTemplate.ID] = activeMessageTemplate } } } writeFiles := make(map[string][]byte, len(translate)+len(active)) for langTag, messageTemplates := range translate { path, content, err := writeFile(outdir, "translate", langTag, outputFormat, messageTemplates, false) if err != nil { return nil, err } writeFiles[path] = content } deleteFiles := []string{} for langTag, messageTemplates := range active { path, content, err := writeFile(outdir, "active", langTag, outputFormat, messageTemplates, langTag == sourceLanguageTag) if err != nil { return nil, err } if len(content) > 0 { writeFiles[path] = content } else { deleteFiles = append(deleteFiles, path) } } return &fileSystemOp{writeFiles: writeFiles, deleteFiles: deleteFiles}, nil } // activeDst returns the active part of the dst and whether dst is a complete translation of src. func activeDst(src, dst *i18n.MessageTemplate, pluralRule *plural.Rule) (active *i18n.MessageTemplate, translateMessageTemplate *i18n.MessageTemplate) { pluralForms := pluralRule.PluralForms if len(src.PluralTemplates) == 1 { pluralForms = map[plural.Form]struct{}{ plural.Other: {}, } } for pluralForm := range pluralForms { dt := dst.PluralTemplates[pluralForm] if dt == nil || dt.Src == "" { if translateMessageTemplate == nil { translateMessageTemplate = &i18n.MessageTemplate{ Message: &i18n.Message{ ID: src.ID, Description: src.Description, Hash: src.Hash, }, PluralTemplates: make(map[plural.Form]*internal.Template), } } translateMessageTemplate.PluralTemplates[pluralForm] = src.PluralTemplates[plural.Other] continue } if active == nil { active = &i18n.MessageTemplate{ Message: &i18n.Message{ ID: src.ID, Description: src.Description, Hash: src.Hash, }, PluralTemplates: make(map[plural.Form]*internal.Template), } } active.PluralTemplates[pluralForm] = dt } return } func hash(t *i18n.MessageTemplate) string { h := sha1.New() _, _ = io.WriteString(h, t.Description) _, _ = io.WriteString(h, t.PluralTemplates[plural.Other].Src) return fmt.Sprintf("sha1-%x", h.Sum(nil)) } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/goi18n/merge_command_test.go 0000664 0000000 0000000 00000036275 13505412006 0025747 0 ustar 00root root 0000000 0000000 package main import ( "bytes" "io/ioutil" "os" "path/filepath" "testing" "golang.org/x/text/language" ) type testCase struct { name string inFiles map[string][]byte sourceLanguage language.Tag outFiles map[string][]byte deleteFiles []string } func expectFile(s string) []byte { // Trimming leading newlines gives nicer formatting for file literals in test cases. return bytes.TrimLeft([]byte(s), "\n") } func TestMerge(t *testing.T) { testCases := []*testCase{ { name: "single identity", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "one.en-US.toml": []byte("1HelloMessage = \"Hello\"\n"), }, outFiles: map[string][]byte{ "active.en-US.toml": []byte("1HelloMessage = \"Hello\"\n"), }, }, { name: "plural identity", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "active.en-US.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" one = "{{.Count}} unread email" other = "{{.Count}} unread emails" `), }, outFiles: map[string][]byte{ "active.en-US.toml": expectFile(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" one = "{{.Count}} unread email" other = "{{.Count}} unread emails" `), }, }, { name: "migrate source lang from v1 format", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "one.en-US.json": []byte(`[ { "id": "simple", "translation": "simple translation" }, { "id": "everything", "translation": { "zero": "zero translation", "one": "one translation", "two": "two translation", "few": "few translation", "many": "many translation", "other": "other translation" } } ]`), }, outFiles: map[string][]byte{ "active.en-US.toml": expectFile(` simple = "simple translation" [everything] few = "few translation" many = "many translation" one = "one translation" other = "other translation" two = "two translation" zero = "zero translation" `), }, }, { name: "migrate source lang from v1 flat format", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "one.en-US.json": []byte(`{ "simple": { "other": "simple translation" }, "everything": { "zero": "zero translation", "one": "one translation", "two": "two translation", "few": "few translation", "many": "many translation", "other": "other translation" } }`), }, outFiles: map[string][]byte{ "active.en-US.toml": expectFile(` simple = "simple translation" [everything] few = "few translation" many = "many translation" one = "one translation" other = "other translation" two = "two translation" zero = "zero translation" `), }, }, { name: "merge source files", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "one.en-US.toml": []byte("1HelloMessage = \"Hello\"\n"), "two.en-US.toml": []byte("2GoodbyeMessage = \"Goodbye\"\n"), }, outFiles: map[string][]byte{ "active.en-US.toml": []byte("1HelloMessage = \"Hello\"\n2GoodbyeMessage = \"Goodbye\"\n"), }, }, { name: "missing hash", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "en-US.toml": []byte(` 1HelloMessage = "Hello" `), "es-ES.toml": []byte(` [1HelloMessage] other = "Hola" `), }, outFiles: map[string][]byte{ "active.en-US.toml": expectFile(` 1HelloMessage = "Hello" `), "active.es-ES.toml": expectFile(` [1HelloMessage] hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" other = "Hola" `), }, }, { name: "add single translation", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "en-US.toml": []byte(` 1HelloMessage = "Hello" 2GoodbyeMessage = "Goodbye" `), "es-ES.toml": []byte(` [1HelloMessage] hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" other = "Hola" `), }, outFiles: map[string][]byte{ "active.en-US.toml": expectFile(` 1HelloMessage = "Hello" 2GoodbyeMessage = "Goodbye" `), "active.es-ES.toml": expectFile(` [1HelloMessage] hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" other = "Hola" `), "translate.es-ES.toml": expectFile(` [2GoodbyeMessage] hash = "sha1-b5b29c53e3c71cb9c6581ab053d7758fab8ca24d" other = "Goodbye" `), }, }, { name: "remove single translation", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "en-US.toml": []byte(` 1HelloMessage = "Hello" `), "es-ES.toml": []byte(` [1HelloMessage] hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" other = "Hola" [2GoodbyeMessage] hash = "sha1-b5b29c53e3c71cb9c6581ab053d7758fab8ca24d" other = "Goodbye" `), }, outFiles: map[string][]byte{ "active.en-US.toml": expectFile(` 1HelloMessage = "Hello" `), "active.es-ES.toml": expectFile(` [1HelloMessage] hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" other = "Hola" `), }, }, { name: "edit single translation", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "en-US.toml": []byte(` 1HelloMessage = "Hi" `), "es-ES.toml": []byte(` [1HelloMessage] hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" other = "Hola" `), }, outFiles: map[string][]byte{ "active.en-US.toml": expectFile(` 1HelloMessage = "Hi" `), "translate.es-ES.toml": expectFile(` [1HelloMessage] hash = "sha1-94dd9e08c129c785f7f256e82fbe0a30e6d1ae40" other = "Hi" `), }, }, { name: "add plural translation", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "en-US.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" one = "{{.Count}} unread email" other = "{{.Count}} unread emails" `), "es-ES.toml": nil, "ar-AR.toml": nil, "zh-CN.toml": nil, }, outFiles: map[string][]byte{ "active.en-US.toml": expectFile(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" one = "{{.Count}} unread email" other = "{{.Count}} unread emails" `), "translate.es-ES.toml": expectFile(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" one = "{{.Count}} unread emails" other = "{{.Count}} unread emails" `), "translate.ar-AR.toml": expectFile(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" few = "{{.Count}} unread emails" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" many = "{{.Count}} unread emails" one = "{{.Count}} unread emails" other = "{{.Count}} unread emails" two = "{{.Count}} unread emails" zero = "{{.Count}} unread emails" `), "translate.zh-CN.toml": expectFile(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" other = "{{.Count}} unread emails" `), }, }, { name: "remove plural translation", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "en-US.toml": []byte(` 1HelloMessage = "Hello" `), "es-ES.toml": []byte(` [1HelloMessage] hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" other = "Hola" [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" one = "{{.Count}} unread emails" other = "{{.Count}} unread emails" `), "ar-AR.toml": []byte(` [1HelloMessage] hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" other = "Hello" [UnreadEmails] description = "Message that tells the user how many unread emails they have" few = "{{.Count}} unread emails" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" many = "{{.Count}} unread emails" one = "{{.Count}} unread emails" other = "{{.Count}} unread emails" two = "{{.Count}} unread emails" zero = "{{.Count}} unread emails" `), "zh-CN.toml": []byte(` [1HelloMessage] hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" other = "Hello" [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" other = "{{.Count}} unread emails" `), }, outFiles: map[string][]byte{ "active.en-US.toml": expectFile(` 1HelloMessage = "Hello" `), "active.es-ES.toml": expectFile(` [1HelloMessage] hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" other = "Hola" `), "active.ar-AR.toml": expectFile(` [1HelloMessage] hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" other = "Hello" `), "active.zh-CN.toml": expectFile(` [1HelloMessage] hash = "sha1-f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" other = "Hello" `), }, }, { name: "edit plural translation", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "en-US.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" one = "{{.Count}} unread emails!" other = "{{.Count}} unread emails!" `), "es-ES.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" one = "{{.Count}} unread emails" other = "{{.Count}} unread emails" `), "ar-AR.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" few = "{{.Count}} unread emails" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" many = "{{.Count}} unread emails" one = "{{.Count}} unread emails" other = "{{.Count}} unread emails" two = "{{.Count}} unread emails" zero = "{{.Count}} unread emails" `), "zh-CN.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" other = "{{.Count}} unread emails" `), }, deleteFiles: []string{ "active.es-ES.toml", "active.ar-AR.toml", "active.zh-CN.toml", }, outFiles: map[string][]byte{ "active.en-US.toml": expectFile(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" one = "{{.Count}} unread emails!" other = "{{.Count}} unread emails!" `), "translate.es-ES.toml": expectFile(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-92a24983c5bbc0c42462cdc252dca68ebdb46501" one = "{{.Count}} unread emails!" other = "{{.Count}} unread emails!" `), "translate.ar-AR.toml": expectFile(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" few = "{{.Count}} unread emails!" hash = "sha1-92a24983c5bbc0c42462cdc252dca68ebdb46501" many = "{{.Count}} unread emails!" one = "{{.Count}} unread emails!" other = "{{.Count}} unread emails!" two = "{{.Count}} unread emails!" zero = "{{.Count}} unread emails!" `), "translate.zh-CN.toml": expectFile(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-92a24983c5bbc0c42462cdc252dca68ebdb46501" other = "{{.Count}} unread emails!" `), }, }, { name: "merge plural translation", sourceLanguage: language.AmericanEnglish, inFiles: map[string][]byte{ "en-US.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" one = "{{.Count}} unread emails" other = "{{.Count}} unread emails" `), "zero.ar-AR.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" zero = "{{.Count}} unread emails" `), "one.ar-AR.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" one = "{{.Count}} unread emails" `), "two.ar-AR.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" two = "{{.Count}} unread emails" `), "few.ar-AR.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" few = "{{.Count}} unread emails" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" `), "many.ar-AR.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" many = "{{.Count}} unread emails" `), "other.ar-AR.toml": []byte(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" other = "{{.Count}} unread emails" `), }, outFiles: map[string][]byte{ "active.en-US.toml": expectFile(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" one = "{{.Count}} unread emails" other = "{{.Count}} unread emails" `), "active.ar-AR.toml": expectFile(` [UnreadEmails] description = "Message that tells the user how many unread emails they have" few = "{{.Count}} unread emails" hash = "sha1-5afbc91dfedb9755627655c365eb47a89e541099" many = "{{.Count}} unread emails" one = "{{.Count}} unread emails" other = "{{.Count}} unread emails" two = "{{.Count}} unread emails" zero = "{{.Count}} unread emails" `), }, }, } for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { indir := mustTempDir("TestMergeCommandIn") defer os.RemoveAll(indir) outdir := mustTempDir("TestMergeCommandOut") defer os.RemoveAll(outdir) infiles := make([]string, 0, len(testCase.inFiles)) for name, content := range testCase.inFiles { path := filepath.Join(indir, name) infiles = append(infiles, path) if err := ioutil.WriteFile(path, content, 0666); err != nil { t.Fatal(err) } } for _, name := range testCase.deleteFiles { path := filepath.Join(outdir, name) if err := ioutil.WriteFile(path, []byte(`this file should get deleted`), 0666); err != nil { t.Fatal(err) } } args := append([]string{"merge", "-sourceLanguage", testCase.sourceLanguage.String(), "-outdir", outdir}, infiles...) if code := testableMain(args); code != 0 { t.Fatalf("expected exit code 0; got %d\n", code) } files, err := ioutil.ReadDir(outdir) if err != nil { t.Fatal(err) } // Verify that all actual files have expected contents. actualFiles := make(map[string]struct{}, len(files)) for _, f := range files { actualFiles[f.Name()] = struct{}{} if f.IsDir() { t.Errorf("found unexpected dir %s", f.Name()) continue } path := filepath.Join(outdir, f.Name()) actual, err := ioutil.ReadFile(path) if err != nil { t.Error(err) continue } expected, ok := testCase.outFiles[f.Name()] if !ok { t.Errorf("found unexpected file %s with contents:\n%s\n", f.Name(), actual) continue } if !bytes.Equal(actual, expected) { t.Errorf("unexpected contents %s\ngot\n%s\nexpected\n%s", f.Name(), actual, expected) continue } } // Verify that all expected files are accounted for. for name := range testCase.outFiles { if _, ok := actualFiles[name]; !ok { t.Errorf("did not find expected file %s", name) } } }) } } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/ 0000775 0000000 0000000 00000000000 13505412006 0021221 5 ustar 00root root 0000000 0000000 golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/bundle.go 0000664 0000000 0000000 00000010102 13505412006 0023013 0 ustar 00root root 0000000 0000000 package i18n import ( "fmt" "io/ioutil" "github.com/nicksnyder/go-i18n/v2/internal/plural" "golang.org/x/text/language" ) // UnmarshalFunc unmarshals data into v. type UnmarshalFunc func(data []byte, v interface{}) error // Bundle stores a set of messages and pluralization rules. // Most applications only need a single bundle // that is initialized early in the application's lifecycle. // It is not goroutine safe to modify the bundle while Localizers // are reading from it. type Bundle struct { defaultLanguage language.Tag unmarshalFuncs map[string]UnmarshalFunc messageTemplates map[language.Tag]map[string]*MessageTemplate pluralRules plural.Rules tags []language.Tag matcher language.Matcher } // artTag is the language tag used for artifical languages // https://en.wikipedia.org/wiki/Codes_for_constructed_languages var artTag = language.MustParse("art") // NewBundle returns a bundle with a default language and a default set of plural rules. func NewBundle(defaultLanguage language.Tag) *Bundle { b := &Bundle{ defaultLanguage: defaultLanguage, pluralRules: plural.DefaultRules(), } b.pluralRules[artTag] = b.pluralRules.Rule(language.English) b.addTag(defaultLanguage) return b } // RegisterUnmarshalFunc registers an UnmarshalFunc for format. func (b *Bundle) RegisterUnmarshalFunc(format string, unmarshalFunc UnmarshalFunc) { if b.unmarshalFuncs == nil { b.unmarshalFuncs = make(map[string]UnmarshalFunc) } b.unmarshalFuncs[format] = unmarshalFunc } // LoadMessageFile loads the bytes from path // and then calls ParseMessageFileBytes. func (b *Bundle) LoadMessageFile(path string) (*MessageFile, error) { buf, err := ioutil.ReadFile(path) if err != nil { return nil, err } return b.ParseMessageFileBytes(buf, path) } // MustLoadMessageFile is similar to LoadTranslationFile // except it panics if an error happens. func (b *Bundle) MustLoadMessageFile(path string) { if _, err := b.LoadMessageFile(path); err != nil { panic(err) } } // ParseMessageFileBytes parses the bytes in buf to add translations to the bundle. // // The format of the file is everything after the last ".". // // The language tag of the file is everything after the second to last "." or after the last path separator, but before the format. func (b *Bundle) ParseMessageFileBytes(buf []byte, path string) (*MessageFile, error) { messageFile, err := ParseMessageFileBytes(buf, path, b.unmarshalFuncs) if err != nil { return nil, err } if err := b.AddMessages(messageFile.Tag, messageFile.Messages...); err != nil { return nil, err } return messageFile, nil } // MustParseMessageFileBytes is similar to ParseMessageFileBytes // except it panics if an error happens. func (b *Bundle) MustParseMessageFileBytes(buf []byte, path string) { if _, err := b.ParseMessageFileBytes(buf, path); err != nil { panic(err) } } // AddMessages adds messages for a language. // It is useful if your messages are in a format not supported by ParseMessageFileBytes. func (b *Bundle) AddMessages(tag language.Tag, messages ...*Message) error { pluralRule := b.pluralRules.Rule(tag) if pluralRule == nil { return fmt.Errorf("no plural rule registered for %s", tag) } if b.messageTemplates == nil { b.messageTemplates = map[language.Tag]map[string]*MessageTemplate{} } if b.messageTemplates[tag] == nil { b.messageTemplates[tag] = map[string]*MessageTemplate{} b.addTag(tag) } for _, m := range messages { b.messageTemplates[tag][m.ID] = NewMessageTemplate(m) } return nil } // MustAddMessages is similar to AddMessages except it panics if an error happens. func (b *Bundle) MustAddMessages(tag language.Tag, messages ...*Message) { if err := b.AddMessages(tag, messages...); err != nil { panic(err) } } func (b *Bundle) addTag(tag language.Tag) { for _, t := range b.tags { if t == tag { // Tag already exists return } } b.tags = append(b.tags, tag) b.matcher = language.NewMatcher(b.tags) } // LanguageTags returns the list of language tags // of all the translations loaded into the bundle func (b *Bundle) LanguageTags() []language.Tag { return b.tags } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/bundle_test.go 0000664 0000000 0000000 00000014017 13505412006 0024063 0 ustar 00root root 0000000 0000000 package i18n import ( "fmt" "reflect" "testing" "github.com/BurntSushi/toml" "golang.org/x/text/language" yaml "gopkg.in/yaml.v2" ) var simpleMessage = MustNewMessage(map[string]string{ "id": "simple", "other": "simple translation", }) var detailMessage = MustNewMessage(map[string]string{ "id": "detail", "description": "detail description", "other": "detail translation", }) var everythingMessage = MustNewMessage(map[string]string{ "id": "everything", "description": "everything description", "zero": "zero translation", "one": "one translation", "two": "two translation", "few": "few translation", "many": "many translation", "other": "other translation", }) func TestConcurrentAccess(t *testing.T) { bundle := NewBundle(language.English) bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) bundle.MustParseMessageFileBytes([]byte(` # Comment hello = "world" `), "en.toml") count := 10 errch := make(chan error, count) for i := 0; i < count; i++ { go func() { localized := NewLocalizer(bundle, "en").MustLocalize(&LocalizeConfig{MessageID: "hello"}) if localized != "world" { errch <- fmt.Errorf(`expected "world"; got %q`, localized) } else { errch <- nil } }() } for i := 0; i < count; i++ { if err := <-errch; err != nil { t.Fatal(err) } } } func TestPseudoLanguage(t *testing.T) { bundle := NewBundle(language.English) bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) expected := "nuqneH" bundle.MustParseMessageFileBytes([]byte(` # Comment hello = "`+expected+`" `), "art-x-klingon.toml") { localized, err := NewLocalizer(bundle, "art-x-klingon").Localize(&LocalizeConfig{MessageID: "hello"}) if err != nil { t.Fatal(err) } if localized != expected { t.Fatalf("expected %q\ngot %q", expected, localized) } } { localized, err := NewLocalizer(bundle, "art").Localize(&LocalizeConfig{MessageID: "hello"}) if err != nil { t.Fatal(err) } if localized != expected { t.Fatalf("expected %q\ngot %q", expected, localized) } } { expected := "" localized, err := NewLocalizer(bundle, "en").Localize(&LocalizeConfig{MessageID: "hello"}) if err == nil { t.Fatal(err) } if localized != expected { t.Fatalf("expected %q\ngot %q", expected, localized) } } } func TestJSON(t *testing.T) { bundle := NewBundle(language.English) bundle.MustParseMessageFileBytes([]byte(`{ "simple": "simple translation", "detail": { "description": "detail description", "other": "detail translation" }, "everything": { "description": "everything description", "zero": "zero translation", "one": "one translation", "two": "two translation", "few": "few translation", "many": "many translation", "other": "other translation" } }`), "en-US.json") expectMessage(t, bundle, language.AmericanEnglish, "simple", simpleMessage) expectMessage(t, bundle, language.AmericanEnglish, "detail", detailMessage) expectMessage(t, bundle, language.AmericanEnglish, "everything", everythingMessage) } func TestYAML(t *testing.T) { bundle := NewBundle(language.English) bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal) bundle.MustParseMessageFileBytes([]byte(` # Comment simple: simple translation # Comment detail: description: detail description other: detail translation # Comment everything: description: everything description zero: zero translation one: one translation two: two translation few: few translation many: many translation other: other translation `), "en-US.yaml") expectMessage(t, bundle, language.AmericanEnglish, "simple", simpleMessage) expectMessage(t, bundle, language.AmericanEnglish, "detail", detailMessage) expectMessage(t, bundle, language.AmericanEnglish, "everything", everythingMessage) } func TestTOML(t *testing.T) { bundle := NewBundle(language.English) bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) bundle.MustParseMessageFileBytes([]byte(` # Comment simple = "simple translation" # Comment [detail] description = "detail description" other = "detail translation" # Comment [everything] description = "everything description" zero = "zero translation" one = "one translation" two = "two translation" few = "few translation" many = "many translation" other = "other translation" `), "en-US.toml") expectMessage(t, bundle, language.AmericanEnglish, "simple", simpleMessage) expectMessage(t, bundle, language.AmericanEnglish, "detail", detailMessage) expectMessage(t, bundle, language.AmericanEnglish, "everything", everythingMessage) } func TestV1Format(t *testing.T) { bundle := NewBundle(language.English) bundle.MustParseMessageFileBytes([]byte(`[ { "id": "simple", "translation": "simple translation" }, { "id": "everything", "translation": { "zero": "zero translation", "one": "one translation", "two": "two translation", "few": "few translation", "many": "many translation", "other": "other translation" } } ] `), "en-US.json") expectMessage(t, bundle, language.AmericanEnglish, "simple", simpleMessage) e := *everythingMessage e.Description = "" expectMessage(t, bundle, language.AmericanEnglish, "everything", &e) } func TestV1FlatFormat(t *testing.T) { bundle := NewBundle(language.English) bundle.MustParseMessageFileBytes([]byte(`{ "simple": { "other": "simple translation" }, "everything": { "zero": "zero translation", "one": "one translation", "two": "two translation", "few": "few translation", "many": "many translation", "other": "other translation" } } `), "en-US.json") expectMessage(t, bundle, language.AmericanEnglish, "simple", simpleMessage) e := *everythingMessage e.Description = "" expectMessage(t, bundle, language.AmericanEnglish, "everything", &e) } func expectMessage(t *testing.T, bundle *Bundle, tag language.Tag, messageID string, message *Message) { expected := NewMessageTemplate(message) actual := bundle.messageTemplates[tag][messageID] if !reflect.DeepEqual(actual, expected) { t.Errorf("bundle.MessageTemplates[%q][%q] = %#v; want %#v", tag, messageID, actual, expected) } } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/doc.go 0000664 0000000 0000000 00000001537 13505412006 0022323 0 ustar 00root root 0000000 0000000 // Package i18n provides support for looking up messages // according to a set of locale preferences. // // Create a Bundle to use for the lifetime of your application. // bundle := i18n.NewBundle(language.English) // // Load translations into your bundle during initialization. // bundle.LoadMessageFile("en-US.yaml") // // Create a Localizer to use for a set of language preferences. // func(w http.ResponseWriter, r *http.Request) { // lang := r.FormValue("lang") // accept := r.Header.Get("Accept-Language") // localizer := i18n.NewLocalizer(bundle, lang, accept) // } // // Use the Localizer to lookup messages. // localizer.MustLocalize(&i18n.LocalizeConfig{ // DefaultMessage: &i18n.Message{ // ID: "HelloWorld", // Other: "Hello World!", // }, // }) package i18n golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/example_test.go 0000664 0000000 0000000 00000007136 13505412006 0024251 0 ustar 00root root 0000000 0000000 package i18n_test import ( "fmt" "github.com/BurntSushi/toml" "github.com/nicksnyder/go-i18n/v2/i18n" "golang.org/x/text/language" ) func ExampleLocalizer_MustLocalize() { bundle := i18n.NewBundle(language.English) localizer := i18n.NewLocalizer(bundle, "en") fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ DefaultMessage: &i18n.Message{ ID: "HelloWorld", Other: "Hello World!", }, })) // Output: // Hello World! } func ExampleLocalizer_MustLocalize_noDefaultMessage() { bundle := i18n.NewBundle(language.English) bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) bundle.MustParseMessageFileBytes([]byte(` HelloWorld = "Hello World!" `), "en.toml") bundle.MustParseMessageFileBytes([]byte(` HelloWorld = "Hola Mundo!" `), "es.toml") { localizer := i18n.NewLocalizer(bundle, "en-US") fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "HelloWorld"})) } { localizer := i18n.NewLocalizer(bundle, "es-ES") fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "HelloWorld"})) } // Output: // Hello World! // Hola Mundo! } func ExampleLocalizer_MustLocalize_plural() { bundle := i18n.NewBundle(language.English) localizer := i18n.NewLocalizer(bundle, "en") catsMessage := &i18n.Message{ ID: "Cats", One: "I have {{.PluralCount}} cat.", Other: "I have {{.PluralCount}} cats.", } fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ DefaultMessage: catsMessage, PluralCount: 1, })) fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ DefaultMessage: catsMessage, PluralCount: 2, })) fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ DefaultMessage: catsMessage, PluralCount: "2.5", })) // Output: // I have 1 cat. // I have 2 cats. // I have 2.5 cats. } func ExampleLocalizer_MustLocalize_template() { bundle := i18n.NewBundle(language.English) localizer := i18n.NewLocalizer(bundle, "en") helloPersonMessage := &i18n.Message{ ID: "HelloPerson", Other: "Hello {{.Name}}!", } fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ DefaultMessage: helloPersonMessage, TemplateData: map[string]string{"Name": "Nick"}, })) // Output: // Hello Nick! } func ExampleLocalizer_MustLocalize_plural_template() { bundle := i18n.NewBundle(language.English) localizer := i18n.NewLocalizer(bundle, "en") personCatsMessage := &i18n.Message{ ID: "PersonCats", One: "{{.Name}} has {{.Count}} cat.", Other: "{{.Name}} has {{.Count}} cats.", } fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ DefaultMessage: personCatsMessage, PluralCount: 1, TemplateData: map[string]interface{}{ "Name": "Nick", "Count": 1, }, })) fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ DefaultMessage: personCatsMessage, PluralCount: 2, TemplateData: map[string]interface{}{ "Name": "Nick", "Count": 2, }, })) fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ DefaultMessage: personCatsMessage, PluralCount: "2.5", TemplateData: map[string]interface{}{ "Name": "Nick", "Count": "2.5", }, })) // Output: // Nick has 1 cat. // Nick has 2 cats. // Nick has 2.5 cats. } func ExampleLocalizer_MustLocalize_customTemplateDelims() { bundle := i18n.NewBundle(language.English) localizer := i18n.NewLocalizer(bundle, "en") helloPersonMessage := &i18n.Message{ ID: "HelloPerson", Other: "Hello <<.Name>>!", LeftDelim: "<<", RightDelim: ">>", } fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{ DefaultMessage: helloPersonMessage, TemplateData: map[string]string{"Name": "Nick"}, })) // Output: // Hello Nick! } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/language_test.go 0000664 0000000 0000000 00000001443 13505412006 0024374 0 ustar 00root root 0000000 0000000 package i18n_test import ( "testing" "golang.org/x/text/language" ) var matcher language.Matcher func BenchmarkNewMatcher(b *testing.B) { langs := []language.Tag{ language.English, language.AmericanEnglish, language.BritishEnglish, language.Spanish, language.EuropeanSpanish, language.Portuguese, language.French, } b.ResetTimer() for i := 0; i < b.N; i++ { matcher = language.NewMatcher(langs) } } func BenchmarkMatchStrings(b *testing.B) { langs := []language.Tag{ language.English, language.AmericanEnglish, language.BritishEnglish, language.Spanish, language.EuropeanSpanish, language.Portuguese, language.French, } matcher := language.NewMatcher(langs) b.ResetTimer() for i := 0; i < b.N; i++ { language.MatchStrings(matcher, "en-US,en;q=0.9") } } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/localizer.go 0000664 0000000 0000000 00000015564 13505412006 0023547 0 ustar 00root root 0000000 0000000 package i18n import ( "fmt" "text/template" "github.com/nicksnyder/go-i18n/v2/internal/plural" "golang.org/x/text/language" ) // Localizer provides Localize and MustLocalize methods that return localized messages. type Localizer struct { // bundle contains the messages that can be returned by the Localizer. bundle *Bundle // tags is the list of language tags that the Localizer checks // in order when localizing a message. tags []language.Tag } // NewLocalizer returns a new Localizer that looks up messages // in the bundle according to the language preferences in langs. // It can parse Accept-Language headers as defined in http://www.ietf.org/rfc/rfc2616.txt. func NewLocalizer(bundle *Bundle, langs ...string) *Localizer { return &Localizer{ bundle: bundle, tags: parseTags(langs), } } func parseTags(langs []string) []language.Tag { tags := []language.Tag{} for _, lang := range langs { t, _, err := language.ParseAcceptLanguage(lang) if err != nil { continue } tags = append(tags, t...) } return tags } // LocalizeConfig configures a call to the Localize method on Localizer. type LocalizeConfig struct { // MessageID is the id of the message to lookup. // This field is ignored if DefaultMessage is set. MessageID string // TemplateData is the data passed when executing the message's template. // If TemplateData is nil and PluralCount is not nil, then the message template // will be executed with data that contains the plural count. TemplateData interface{} // PluralCount determines which plural form of the message is used. PluralCount interface{} // DefaultMessage is used if the message is not found in any message files. DefaultMessage *Message // Funcs is used to extend the Go template engine's built in functions Funcs template.FuncMap } type invalidPluralCountErr struct { messageID string pluralCount interface{} err error } func (e *invalidPluralCountErr) Error() string { return fmt.Sprintf("invalid plural count %#v for message id %q: %s", e.pluralCount, e.messageID, e.err) } // MessageNotFoundErr is returned from Localize when a message could not be found. type MessageNotFoundErr struct { messageID string } func (e *MessageNotFoundErr) Error() string { return fmt.Sprintf("message %q not found", e.messageID) } type pluralizeErr struct { messageID string tag language.Tag } func (e *pluralizeErr) Error() string { return fmt.Sprintf("unable to pluralize %q because there no plural rule for %q", e.messageID, e.tag) } type messageIDMismatchErr struct { messageID string defaultMessageID string } func (e *messageIDMismatchErr) Error() string { return fmt.Sprintf("message id %q does not match default message id %q", e.messageID, e.defaultMessageID) } // Localize returns a localized message. func (l *Localizer) Localize(lc *LocalizeConfig) (string, error) { msg, _, err := l.LocalizeWithTag(lc) return msg, err } // Localize returns a localized message. func (l *Localizer) LocalizeMessage(msg *Message) (string, error) { return l.Localize(&LocalizeConfig{ DefaultMessage: msg, }) } // TODO: uncomment this (and the test) when extract has been updated to extract these call sites too. // Localize returns a localized message. // func (l *Localizer) LocalizeMessageID(messageID string) (string, error) { // return l.Localize(&LocalizeConfig{ // MessageID: messageID, // }) // } // LocalizeWithTag returns a localized message and the language tag. // It may return a best effort localized message even if an error happens. func (l *Localizer) LocalizeWithTag(lc *LocalizeConfig) (string, language.Tag, error) { messageID := lc.MessageID if lc.DefaultMessage != nil { if messageID != "" && messageID != lc.DefaultMessage.ID { return "", language.Und, &messageIDMismatchErr{messageID: messageID, defaultMessageID: lc.DefaultMessage.ID} } messageID = lc.DefaultMessage.ID } var operands *plural.Operands templateData := lc.TemplateData if lc.PluralCount != nil { var err error operands, err = plural.NewOperands(lc.PluralCount) if err != nil { return "", language.Und, &invalidPluralCountErr{messageID: messageID, pluralCount: lc.PluralCount, err: err} } if templateData == nil { templateData = map[string]interface{}{ "PluralCount": lc.PluralCount, } } } tag, template := l.getTemplate(messageID, lc.DefaultMessage) if template == nil { return "", language.Und, &MessageNotFoundErr{messageID: messageID} } pluralForm := l.pluralForm(tag, operands) if pluralForm == plural.Invalid { return "", language.Und, &pluralizeErr{messageID: messageID, tag: tag} } msg, err := template.Execute(pluralForm, templateData, lc.Funcs) if err != nil { // Attempt to fallback to "Other" pluralization in case translations are incomplete. if pluralForm != plural.Other { msg2, err2 := template.Execute(plural.Other, templateData, lc.Funcs) if err2 == nil { return msg2, tag, err } } return "", language.Und, err } return msg, tag, nil } func (l *Localizer) getTemplate(id string, defaultMessage *Message) (language.Tag, *MessageTemplate) { // Fast path. // Optimistically assume this message id is defined in each language. fastTag, template := l.matchTemplate(id, defaultMessage, l.bundle.matcher, l.bundle.tags) if template != nil { return fastTag, template } if len(l.bundle.tags) <= 1 { return l.bundle.defaultLanguage, nil } // Slow path. // We didn't find a translation for the tag suggested by the default matcher // so we need to create a new matcher that contains only the tags in the bundle // that have this message. foundTags := make([]language.Tag, 0, len(l.bundle.messageTemplates)+1) foundTags = append(foundTags, l.bundle.defaultLanguage) for t, templates := range l.bundle.messageTemplates { template := templates[id] if template == nil || template.Other == "" { continue } foundTags = append(foundTags, t) } return l.matchTemplate(id, defaultMessage, language.NewMatcher(foundTags), foundTags) } func (l *Localizer) matchTemplate(id string, defaultMessage *Message, matcher language.Matcher, tags []language.Tag) (language.Tag, *MessageTemplate) { _, i, _ := matcher.Match(l.tags...) tag := tags[i] templates := l.bundle.messageTemplates[tag] if templates != nil && templates[id] != nil { return tag, templates[id] } if tag == l.bundle.defaultLanguage && defaultMessage != nil { return tag, NewMessageTemplate(defaultMessage) } return tag, nil } func (l *Localizer) pluralForm(tag language.Tag, operands *plural.Operands) plural.Form { if operands == nil { return plural.Other } pluralRule := l.bundle.pluralRules.Rule(tag) if pluralRule == nil { return plural.Invalid } return pluralRule.PluralFormFunc(operands) } // MustLocalize is similar to Localize, except it panics if an error happens. func (l *Localizer) MustLocalize(lc *LocalizeConfig) string { localized, err := l.Localize(lc) if err != nil { panic(err) } return localized } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/localizer_test.go 0000664 0000000 0000000 00000042201 13505412006 0024572 0 ustar 00root root 0000000 0000000 package i18n import ( "reflect" "testing" "github.com/nicksnyder/go-i18n/v2/internal/plural" "golang.org/x/text/language" ) func TestLocalizer_Localize(t *testing.T) { tests := []struct { name string defaultLanguage language.Tag messages map[language.Tag][]*Message acceptLangs []string conf *LocalizeConfig expectedErr error expectedLocalized string }{ { name: "message id mismatch", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ MessageID: "HelloWorld", DefaultMessage: &Message{ ID: "DefaultHelloWorld", }, }, expectedErr: &messageIDMismatchErr{messageID: "HelloWorld", defaultMessageID: "DefaultHelloWorld"}, }, { name: "message id not mismatched", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ID: "HelloWorld", Other: "Hello!"}}, }, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ MessageID: "HelloWorld", DefaultMessage: &Message{ ID: "HelloWorld", }, }, expectedLocalized: "Hello!", }, { name: "missing translation from default language", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{MessageID: "HelloWorld"}, expectedErr: &MessageNotFoundErr{messageID: "HelloWorld"}, expectedLocalized: "", }, { name: "empty translation without fallback", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.Spanish: {{ID: "HelloWorld"}}, }, acceptLangs: []string{"es"}, conf: &LocalizeConfig{MessageID: "HelloWorld"}, expectedErr: &MessageNotFoundErr{messageID: "HelloWorld"}, }, { name: "empty translation with fallback", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ID: "HelloWorld", Other: "Hello World!"}}, language.Spanish: {{ID: "HelloWorld"}}, }, acceptLangs: []string{"es"}, conf: &LocalizeConfig{MessageID: "HelloWorld"}, expectedLocalized: "Hello World!", }, { name: "missing translation from default language with other translation", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.Spanish: {{ID: "HelloWorld", Other: "other"}}, }, acceptLangs: []string{"en"}, conf: &LocalizeConfig{MessageID: "HelloWorld"}, expectedErr: &MessageNotFoundErr{messageID: "HelloWorld"}, expectedLocalized: "", }, { name: "missing translation from not default language", defaultLanguage: language.English, acceptLangs: []string{"es"}, conf: &LocalizeConfig{MessageID: "HelloWorld"}, expectedErr: &MessageNotFoundErr{messageID: "HelloWorld"}, expectedLocalized: "", }, { name: "missing translation not default language with other translation", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.French: {{ID: "HelloWorld", Other: "other"}}, }, acceptLangs: []string{"es"}, conf: &LocalizeConfig{MessageID: "HelloWorld"}, expectedErr: &MessageNotFoundErr{messageID: "HelloWorld"}, expectedLocalized: "", }, { name: "accept default language, message in bundle", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ID: "HelloWorld", Other: "other"}}, }, acceptLangs: []string{"en"}, conf: &LocalizeConfig{MessageID: "HelloWorld"}, expectedLocalized: "other", }, { name: "accept default language, message in bundle, default message", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ID: "HelloWorld", Other: "bundle other"}}, }, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ID: "HelloWorld", Other: "default other"}, }, expectedLocalized: "bundle other", }, { name: "accept not default language, message in bundle", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.Spanish: {{ID: "HelloWorld", Other: "other"}}, }, acceptLangs: []string{"es"}, conf: &LocalizeConfig{MessageID: "HelloWorld"}, expectedLocalized: "other", }, { name: "accept not default language, other message in bundle, default message", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ID: "HelloWorld", Other: "bundle other"}}, }, acceptLangs: []string{"es"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ID: "HelloWorld", Other: "default other"}, }, expectedLocalized: "bundle other", }, { name: "accept not default language, message in bundle, default message", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.Spanish: {{ID: "HelloWorld", Other: "bundle other"}}, }, acceptLangs: []string{"es"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ID: "HelloWorld", Other: "default other"}, }, expectedLocalized: "bundle other", }, { name: "accept default language, default message", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ID: "HelloWorld", Other: "default other"}, }, expectedLocalized: "default other", }, { name: "accept not default language, default message", defaultLanguage: language.English, acceptLangs: []string{"es"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ID: "HelloWorld", Other: "default other"}, }, expectedLocalized: "default other", }, { name: "fallback to non-default less specific language", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.Spanish: {{ID: "HelloWorld", Other: "bundle other"}}, }, acceptLangs: []string{"es-ES"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ID: "HelloWorld", Other: "default other"}, }, expectedLocalized: "bundle other", }, { name: "fallback to non-default more specific language", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.EuropeanSpanish: {{ID: "HelloWorld", Other: "bundle other"}}, }, acceptLangs: []string{"es"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ID: "HelloWorld", Other: "default other"}, }, expectedLocalized: "bundle other", }, { name: "plural count one, bundle message", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ ID: "Cats", One: "I have {{.PluralCount}} cat", Other: "I have {{.PluralCount}} cats", }}, }, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ MessageID: "Cats", PluralCount: 1, }, expectedLocalized: "I have 1 cat", }, { name: "plural count other, bundle message", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ ID: "Cats", One: "I have {{.PluralCount}} cat", Other: "I have {{.PluralCount}} cats", }}, }, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ MessageID: "Cats", PluralCount: 2, }, expectedLocalized: "I have 2 cats", }, { name: "plural count float, bundle message", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ ID: "Cats", One: "I have {{.PluralCount}} cat", Other: "I have {{.PluralCount}} cats", }}, }, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ MessageID: "Cats", PluralCount: "2.5", }, expectedLocalized: "I have 2.5 cats", }, { name: "plural count one, default message", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ PluralCount: 1, DefaultMessage: &Message{ ID: "Cats", One: "I have {{.PluralCount}} cat", Other: "I have {{.PluralCount}} cats", }, }, expectedLocalized: "I have 1 cat", }, { name: "plural count missing one, default message", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ PluralCount: 1, DefaultMessage: &Message{ ID: "Cats", Other: "I have {{.PluralCount}} cats", }, }, expectedLocalized: "I have 1 cats", expectedErr: pluralFormNotFoundError{messageID: "Cats", pluralForm: plural.One}, }, { name: "plural count missing other, default message", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ PluralCount: 2, DefaultMessage: &Message{ ID: "Cats", One: "I have {{.PluralCount}} cat", }, }, expectedLocalized: "", expectedErr: pluralFormNotFoundError{messageID: "Cats", pluralForm: plural.Other}, }, { name: "plural count other, default message", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ PluralCount: 2, DefaultMessage: &Message{ ID: "Cats", One: "I have {{.PluralCount}} cat", Other: "I have {{.PluralCount}} cats", }, }, expectedLocalized: "I have 2 cats", }, { name: "plural count float, default message", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ PluralCount: "2.5", DefaultMessage: &Message{ ID: "Cats", One: "I have {{.PluralCount}} cat", Other: "I have {{.PluralCount}} cats", }, }, expectedLocalized: "I have 2.5 cats", }, { name: "template data, bundle message", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ ID: "HelloPerson", Other: "Hello {{.Person}}", }}, }, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ MessageID: "HelloPerson", TemplateData: map[string]string{ "Person": "Nick", }, }, expectedLocalized: "Hello Nick", }, { name: "template data, default message", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ ID: "HelloPerson", Other: "Hello {{.Person}}", }, TemplateData: map[string]string{ "Person": "Nick", }, }, expectedLocalized: "Hello Nick", }, { name: "template data, custom delims, bundle message", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ ID: "HelloPerson", Other: "Hello <<.Person>>", LeftDelim: "<<", RightDelim: ">>", }}, }, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ MessageID: "HelloPerson", TemplateData: map[string]string{ "Person": "Nick", }, }, expectedLocalized: "Hello Nick", }, { name: "template data, custom delims, default message", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ ID: "HelloPerson", Other: "Hello <<.Person>>", LeftDelim: "<<", RightDelim: ">>", }, TemplateData: map[string]string{ "Person": "Nick", }, }, expectedLocalized: "Hello Nick", }, { name: "template data, plural count one, bundle message", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ ID: "PersonCats", One: "{{.Person}} has {{.Count}} cat", Other: "{{.Person}} has {{.Count}} cats", }}, }, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ MessageID: "PersonCats", TemplateData: map[string]interface{}{ "Person": "Nick", "Count": 1, }, PluralCount: 1, }, expectedLocalized: "Nick has 1 cat", }, { name: "template data, plural count other, bundle message", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ ID: "PersonCats", One: "{{.Person}} has {{.Count}} cat", Other: "{{.Person}} has {{.Count}} cats", }}, }, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ MessageID: "PersonCats", TemplateData: map[string]interface{}{ "Person": "Nick", "Count": 2, }, PluralCount: 2, }, expectedLocalized: "Nick has 2 cats", }, { name: "template data, plural count float, bundle message", defaultLanguage: language.English, messages: map[language.Tag][]*Message{ language.English: {{ ID: "PersonCats", One: "{{.Person}} has {{.Count}} cat", Other: "{{.Person}} has {{.Count}} cats", }}, }, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ MessageID: "PersonCats", TemplateData: map[string]interface{}{ "Person": "Nick", "Count": "2.5", }, PluralCount: "2.5", }, expectedLocalized: "Nick has 2.5 cats", }, { name: "template data, plural count one, default message", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ ID: "PersonCats", One: "{{.Person}} has {{.Count}} cat", Other: "{{.Person}} has {{.Count}} cats", }, TemplateData: map[string]interface{}{ "Person": "Nick", "Count": 1, }, PluralCount: 1, }, expectedLocalized: "Nick has 1 cat", }, { name: "template data, plural count other, default message", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ ID: "PersonCats", One: "{{.Person}} has {{.Count}} cat", Other: "{{.Person}} has {{.Count}} cats", }, TemplateData: map[string]interface{}{ "Person": "Nick", "Count": 2, }, PluralCount: 2, }, expectedLocalized: "Nick has 2 cats", }, { name: "template data, plural count float, default message", defaultLanguage: language.English, acceptLangs: []string{"en"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ ID: "PersonCats", One: "{{.Person}} has {{.Count}} cat", Other: "{{.Person}} has {{.Count}} cats", }, TemplateData: map[string]interface{}{ "Person": "Nick", "Count": "2.5", }, PluralCount: "2.5", }, expectedLocalized: "Nick has 2.5 cats", }, { name: "test slow path", defaultLanguage: language.Spanish, messages: map[language.Tag][]*Message{ language.English: {{ ID: "Hello", Other: "Hello!", }}, language.AmericanEnglish: {{ ID: "Goodbye", Other: "Goodbye!", }}, }, acceptLangs: []string{"en-US"}, conf: &LocalizeConfig{ MessageID: "Hello", }, expectedLocalized: "Hello!", }, { name: "test slow path default message", defaultLanguage: language.Spanish, messages: map[language.Tag][]*Message{ language.English: {{ ID: "Goodbye", Other: "Goodbye!", }}, language.AmericanEnglish: {{ ID: "Goodbye", Other: "Goodbye!", }}, }, acceptLangs: []string{"en-US"}, conf: &LocalizeConfig{ DefaultMessage: &Message{ ID: "Hello", Other: "Hola!", }, }, expectedLocalized: "Hola!", }, { name: "test slow path no message", defaultLanguage: language.Spanish, messages: map[language.Tag][]*Message{ language.English: {{ ID: "Goodbye", Other: "Goodbye!", }}, language.AmericanEnglish: {{ ID: "Goodbye", Other: "Goodbye!", }}, }, acceptLangs: []string{"en-US"}, conf: &LocalizeConfig{ MessageID: "Hello", }, expectedErr: &MessageNotFoundErr{messageID: "Hello"}, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { bundle := NewBundle(test.defaultLanguage) for tag, messages := range test.messages { if err := bundle.AddMessages(tag, messages...); err != nil { t.Fatal(err) } } check := func(localized string, err error) { t.Helper() if !reflect.DeepEqual(err, test.expectedErr) { t.Errorf("expected error %#v; got %#v", test.expectedErr, err) } if localized != test.expectedLocalized { t.Errorf("expected localized string %q; got %q", test.expectedLocalized, localized) } } localizer := NewLocalizer(bundle, test.acceptLangs...) check(localizer.Localize(test.conf)) if test.conf.DefaultMessage != nil && reflect.DeepEqual(test.conf, &LocalizeConfig{DefaultMessage: test.conf.DefaultMessage}) { check(localizer.LocalizeMessage(test.conf.DefaultMessage)) } // if test.conf.MessageID != "" && reflect.DeepEqual(test.conf, &LocalizeConfig{MessageID: test.conf.MessageID}) { // check(localizer.LocalizeMessageID(test.conf.MessageID)) // } }) } } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/message.go 0000664 0000000 0000000 00000011703 13505412006 0023176 0 ustar 00root root 0000000 0000000 package i18n import ( "fmt" "strings" ) // Message is a string that can be localized. type Message struct { // ID uniquely identifies the message. ID string // Hash uniquely identifies the content of the message // that this message was translated from. Hash string // Description describes the message to give additional // context to translators that may be relevant for translation. Description string // LeftDelim is the left Go template delimiter. LeftDelim string // RightDelim is the right Go template delimiter.`` RightDelim string // Zero is the content of the message for the CLDR plural form "zero". Zero string // One is the content of the message for the CLDR plural form "one". One string // Two is the content of the message for the CLDR plural form "two". Two string // Few is the content of the message for the CLDR plural form "few". Few string // Many is the content of the message for the CLDR plural form "many". Many string // Other is the content of the message for the CLDR plural form "other". Other string } // NewMessage parses data and returns a new message. func NewMessage(data interface{}) (*Message, error) { m := &Message{} if err := m.unmarshalInterface(data); err != nil { return nil, err } return m, nil } // MustNewMessage is similar to NewMessage except it panics if an error happens. func MustNewMessage(data interface{}) *Message { m, err := NewMessage(data) if err != nil { panic(err) } return m } // unmarshalInterface unmarshals a message from data. func (m *Message) unmarshalInterface(v interface{}) error { strdata, err := stringMap(v) if err != nil { return err } for k, v := range strdata { switch strings.ToLower(k) { case "id": m.ID = v case "description": m.Description = v case "hash": m.Hash = v case "leftdelim": m.LeftDelim = v case "rightdelim": m.RightDelim = v case "zero": m.Zero = v case "one": m.One = v case "two": m.Two = v case "few": m.Few = v case "many": m.Many = v case "other": m.Other = v } } return nil } type keyTypeErr struct { key interface{} } func (err *keyTypeErr) Error() string { return fmt.Sprintf("expected key to be a string but got %#v", err.key) } type valueTypeErr struct { value interface{} } func (err *valueTypeErr) Error() string { return fmt.Sprintf("unsupported type %#v", err.value) } func stringMap(v interface{}) (map[string]string, error) { switch value := v.(type) { case string: return map[string]string{ "other": value, }, nil case map[string]string: return value, nil case map[string]interface{}: strdata := make(map[string]string, len(value)) for k, v := range value { err := stringSubmap(k, v, strdata) if err != nil { return nil, err } } return strdata, nil case map[interface{}]interface{}: strdata := make(map[string]string, len(value)) for k, v := range value { kstr, ok := k.(string) if !ok { return nil, &keyTypeErr{key: k} } err := stringSubmap(kstr, v, strdata) if err != nil { return nil, err } } return strdata, nil default: return nil, &valueTypeErr{value: value} } } func stringSubmap(k string, v interface{}, strdata map[string]string) error { if k == "translation" { switch vt := v.(type) { case string: strdata["other"] = vt default: v1Message, err := stringMap(v) if err != nil { return err } for kk, vv := range v1Message { strdata[kk] = vv } } return nil } switch vt := v.(type) { case string: strdata[k] = vt return nil case nil: return nil default: return fmt.Errorf("expected value for key %q be a string but got %#v", k, v) } } // isMessage tells whether the given data is a message, or a map containing // nested messages. // A map is assumed to be a message if it contains any of the "reserved" keys: // "id", "description", "hash", "leftdelim", "rightdelim", "zero", "one", "two", "few", "many", "other" // with a string value. // e.g., // - {"message": {"description": "world"}} is a message // - {"message": {"description": "world", "foo": "bar"}} is a message ("foo" key is ignored) // - {"notmessage": {"description": {"hello": "world"}}} is not // - {"notmessage": {"foo": "bar"}} is not func isMessage(v interface{}) bool { reservedKeys := []string{"id", "description", "hash", "leftdelim", "rightdelim", "zero", "one", "two", "few", "many", "other"} switch data := v.(type) { case string: return true case map[string]interface{}: for _, key := range reservedKeys { val, ok := data[key] if !ok { continue } _, ok = val.(string) if !ok { continue } // v is a message if it contains a "reserved" key holding a string value return true } case map[interface{}]interface{}: for _, key := range reservedKeys { val, ok := data[key] if !ok { continue } _, ok = val.(string) if !ok { continue } // v is a message if it contains a "reserved" key holding a string value return true } } return false } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/message_template.go 0000664 0000000 0000000 00000003711 13505412006 0025071 0 ustar 00root root 0000000 0000000 package i18n import ( "fmt" "text/template" "github.com/nicksnyder/go-i18n/v2/internal" "github.com/nicksnyder/go-i18n/v2/internal/plural" ) // MessageTemplate is an executable template for a message. type MessageTemplate struct { *Message PluralTemplates map[plural.Form]*internal.Template } // NewMessageTemplate returns a new message template. func NewMessageTemplate(m *Message) *MessageTemplate { pluralTemplates := map[plural.Form]*internal.Template{} setPluralTemplate(pluralTemplates, plural.Zero, m.Zero, m.LeftDelim, m.RightDelim) setPluralTemplate(pluralTemplates, plural.One, m.One, m.LeftDelim, m.RightDelim) setPluralTemplate(pluralTemplates, plural.Two, m.Two, m.LeftDelim, m.RightDelim) setPluralTemplate(pluralTemplates, plural.Few, m.Few, m.LeftDelim, m.RightDelim) setPluralTemplate(pluralTemplates, plural.Many, m.Many, m.LeftDelim, m.RightDelim) setPluralTemplate(pluralTemplates, plural.Other, m.Other, m.LeftDelim, m.RightDelim) if len(pluralTemplates) == 0 { return nil } return &MessageTemplate{ Message: m, PluralTemplates: pluralTemplates, } } func setPluralTemplate(pluralTemplates map[plural.Form]*internal.Template, pluralForm plural.Form, src, leftDelim, rightDelim string) { if src != "" { pluralTemplates[pluralForm] = &internal.Template{ Src: src, LeftDelim: leftDelim, RightDelim: rightDelim, } } } type pluralFormNotFoundError struct { pluralForm plural.Form messageID string } func (e pluralFormNotFoundError) Error() string { return fmt.Sprintf("message %q has no plural form %q", e.messageID, e.pluralForm) } // Execute executes the template for the plural form and template data. func (mt *MessageTemplate) Execute(pluralForm plural.Form, data interface{}, funcs template.FuncMap) (string, error) { t := mt.PluralTemplates[pluralForm] if t == nil { return "", pluralFormNotFoundError{ pluralForm: pluralForm, messageID: mt.Message.ID, } } return t.Execute(funcs, data) } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/message_template_test.go 0000664 0000000 0000000 00000001555 13505412006 0026134 0 ustar 00root root 0000000 0000000 package i18n import ( "reflect" "testing" "github.com/nicksnyder/go-i18n/v2/internal/plural" ) func TestMessageTemplate(t *testing.T) { mt := NewMessageTemplate(&Message{ID: "HelloWorld", Other: "Hello World"}) if mt.PluralTemplates[plural.Other].Src != "Hello World" { panic(mt.PluralTemplates) } } func TestNilMessageTemplate(t *testing.T) { if mt := NewMessageTemplate(&Message{ID: "HelloWorld"}); mt != nil { panic(mt) } } func TestMessageTemplatePluralFormMissing(t *testing.T) { mt := NewMessageTemplate(&Message{ID: "HelloWorld", Other: "Hello World"}) s, err := mt.Execute(plural.Few, nil, nil) if s != "" { t.Errorf("expected %q; got %q", "", s) } expectedErr := pluralFormNotFoundError{pluralForm: plural.Few, messageID: "HelloWorld"} if !reflect.DeepEqual(err, expectedErr) { t.Errorf("expected error %#v; got %#v", expectedErr, err) } } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/message_test.go 0000664 0000000 0000000 00000006743 13505412006 0024245 0 ustar 00root root 0000000 0000000 package i18n import ( "reflect" "testing" ) func TestNewMessage(t *testing.T) { tests := []struct { name string data interface{} message *Message err error }{ { name: "string", data: "other", message: &Message{ Other: "other", }, }, { name: "nil value", data: map[interface{}]interface{}{ "ID": "id", "Zero": nil, "Other": "other", }, message: &Message{ ID: "id", Other: "other", }, }, { name: "map[string]string", data: map[string]string{ "ID": "id", "Hash": "hash", "Description": "description", "LeftDelim": "leftdelim", "RightDelim": "rightdelim", "Zero": "zero", "One": "one", "Two": "two", "Few": "few", "Many": "many", "Other": "other", }, message: &Message{ ID: "id", Hash: "hash", Description: "description", LeftDelim: "leftdelim", RightDelim: "rightdelim", Zero: "zero", One: "one", Two: "two", Few: "few", Many: "many", Other: "other", }, }, { name: "map[string]interface{}", data: map[string]interface{}{ "ID": "id", "Hash": "hash", "Description": "description", "LeftDelim": "leftdelim", "RightDelim": "rightdelim", "Zero": "zero", "One": "one", "Two": "two", "Few": "few", "Many": "many", "Other": "other", }, message: &Message{ ID: "id", Hash: "hash", Description: "description", LeftDelim: "leftdelim", RightDelim: "rightdelim", Zero: "zero", One: "one", Two: "two", Few: "few", Many: "many", Other: "other", }, }, { name: "map[interface{}]interface{}", data: map[interface{}]interface{}{ "ID": "id", "Hash": "hash", "Description": "description", "LeftDelim": "leftdelim", "RightDelim": "rightdelim", "Zero": "zero", "One": "one", "Two": "two", "Few": "few", "Many": "many", "Other": "other", }, message: &Message{ ID: "id", Hash: "hash", Description: "description", LeftDelim: "leftdelim", RightDelim: "rightdelim", Zero: "zero", One: "one", Two: "two", Few: "few", Many: "many", Other: "other", }, }, { name: "map[int]int", data: map[interface{}]interface{}{ 1: 2, }, err: &keyTypeErr{key: 1}, }, { name: "int", data: 1, err: &valueTypeErr{value: 1}, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { actual, err := NewMessage(test.data) if !reflect.DeepEqual(err, test.err) { t.Fatalf("expected %#v; got %#v", test.err, err) } if !reflect.DeepEqual(actual, test.message) { t.Fatalf("\nexpected\n%#v\ngot\n%#v", test.message, actual) } }) } } func TestKeyTypeErr(t *testing.T) { expected := "expected key to be a string but got 1" if actual := (&keyTypeErr{key: 1}).Error(); actual != expected { t.Fatalf("expected %#v; got %#v", expected, actual) } } func TestValueTypeErr(t *testing.T) { expected := "unsupported type 1" if actual := (&valueTypeErr{value: 1}).Error(); actual != expected { t.Fatalf("expected %#v; got %#v", expected, actual) } } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/parse.go 0000664 0000000 0000000 00000007537 13505412006 0022676 0 ustar 00root root 0000000 0000000 package i18n import ( "encoding/json" "errors" "fmt" "os" "golang.org/x/text/language" ) // MessageFile represents a parsed message file. type MessageFile struct { Path string Tag language.Tag Format string Messages []*Message } // ParseMessageFileBytes returns the messages parsed from file. func ParseMessageFileBytes(buf []byte, path string, unmarshalFuncs map[string]UnmarshalFunc) (*MessageFile, error) { lang, format := parsePath(path) tag := language.Make(lang) messageFile := &MessageFile{ Path: path, Tag: tag, Format: format, } if len(buf) == 0 { return messageFile, nil } unmarshalFunc := unmarshalFuncs[messageFile.Format] if unmarshalFunc == nil { if messageFile.Format == "json" { unmarshalFunc = json.Unmarshal } else { return nil, fmt.Errorf("no unmarshaler registered for %s", messageFile.Format) } } var err error var raw interface{} if err = unmarshalFunc(buf, &raw); err != nil { return nil, err } if messageFile.Messages, err = recGetMessages(raw, isMessage(raw), true); err != nil { return nil, err } return messageFile, nil } const nestedSeparator = "." var errInvalidTranslationFile = errors.New("invalid translation file, expected key-values, got a single value") // recGetMessages looks for translation messages inside "raw" parameter, // scanning nested maps using recursion. func recGetMessages(raw interface{}, isMapMessage, isInitialCall bool) ([]*Message, error) { var messages []*Message var err error switch data := raw.(type) { case string: if isInitialCall { return nil, errInvalidTranslationFile } m, err := NewMessage(data) return []*Message{m}, err case map[string]interface{}: if isMapMessage { m, err := NewMessage(data) return []*Message{m}, err } messages = make([]*Message, 0, len(data)) for id, data := range data { // recursively scan map items messages, err = addChildMessages(id, data, messages) if err != nil { return nil, err } } case map[interface{}]interface{}: if isMapMessage { m, err := NewMessage(data) return []*Message{m}, err } messages = make([]*Message, 0, len(data)) for id, data := range data { strid, ok := id.(string) if !ok { return nil, fmt.Errorf("expected key to be string but got %#v", id) } // recursively scan map items messages, err = addChildMessages(strid, data, messages) if err != nil { return nil, err } } case []interface{}: // Backward compatibility for v1 file format. messages = make([]*Message, 0, len(data)) for _, data := range data { // recursively scan slice items childMessages, err := recGetMessages(data, isMessage(data), false) if err != nil { return nil, err } messages = append(messages, childMessages...) } default: return nil, fmt.Errorf("unsupported file format %T", raw) } return messages, nil } func addChildMessages(id string, data interface{}, messages []*Message) ([]*Message, error) { isChildMessage := isMessage(data) childMessages, err := recGetMessages(data, isChildMessage, false) if err != nil { return nil, err } for _, m := range childMessages { if isChildMessage { if m.ID == "" { m.ID = id // start with innermost key } } else { m.ID = id + nestedSeparator + m.ID // update ID with each nested key on the way } messages = append(messages, m) } return messages, nil } func parsePath(path string) (langTag, format string) { formatStartIdx := -1 for i := len(path) - 1; i >= 0; i-- { c := path[i] if os.IsPathSeparator(c) { if formatStartIdx != -1 { langTag = path[i+1 : formatStartIdx] } return } if path[i] == '.' { if formatStartIdx != -1 { langTag = path[i+1 : formatStartIdx] return } if formatStartIdx == -1 { format = path[i+1:] formatStartIdx = i } } } if formatStartIdx != -1 { langTag = path[:formatStartIdx] } return } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/i18n/parse_test.go 0000664 0000000 0000000 00000012007 13505412006 0023721 0 ustar 00root root 0000000 0000000 package i18n import ( "reflect" "sort" "testing" "golang.org/x/text/language" yaml "gopkg.in/yaml.v2" ) func TestParseMessageFileBytes(t *testing.T) { testCases := []struct { name string file string path string unmarshalFuncs map[string]UnmarshalFunc messageFile *MessageFile err error }{ { name: "basic test", file: `{"hello": "world"}`, path: "en.json", messageFile: &MessageFile{ Path: "en.json", Tag: language.English, Format: "json", Messages: []*Message{{ ID: "hello", Other: "world", }}, }, }, { name: "basic test with dot separator in key", file: `{"prepended.hello": "world"}`, path: "en.json", messageFile: &MessageFile{ Path: "en.json", Tag: language.English, Format: "json", Messages: []*Message{{ ID: "prepended.hello", Other: "world", }}, }, }, { name: "invalid test (no key)", file: `"hello"`, path: "en.json", err: errInvalidTranslationFile, }, { name: "nested test", file: `{"nested": {"hello": "world"}}`, path: "en.json", messageFile: &MessageFile{ Path: "en.json", Tag: language.English, Format: "json", Messages: []*Message{{ ID: "nested.hello", Other: "world", }}, }, }, { name: "basic test with description", file: `{"notnested": {"description": "world"}}`, path: "en.json", messageFile: &MessageFile{ Path: "en.json", Tag: language.English, Format: "json", Messages: []*Message{{ ID: "notnested", Description: "world", }}, }, }, { name: "basic test with id", file: `{"key": {"id": "forced.id"}}`, path: "en.json", messageFile: &MessageFile{ Path: "en.json", Tag: language.English, Format: "json", Messages: []*Message{{ ID: "forced.id", }}, }, }, { name: "basic test with description and dummy", file: `{"notnested": {"description": "world", "dummy": "nothing"}}`, path: "en.json", messageFile: &MessageFile{ Path: "en.json", Tag: language.English, Format: "json", Messages: []*Message{{ ID: "notnested", Description: "world", }}, }, }, { name: "deeply nested test", file: `{"outer": {"nested": {"inner": "value"}}}`, path: "en.json", messageFile: &MessageFile{ Path: "en.json", Tag: language.English, Format: "json", Messages: []*Message{{ ID: "outer.nested.inner", Other: "value", }}, }, }, { name: "multiple nested test", file: `{"nested": {"hello": "world", "bye": "all"}}`, path: "en.json", messageFile: &MessageFile{ Path: "en.json", Tag: language.English, Format: "json", Messages: []*Message{{ ID: "nested.hello", Other: "world", }, { ID: "nested.bye", Other: "all", }}, }, }, { name: "YAML nested test", file: ` outer: nested: inner: "value"`, path: "en.yaml", unmarshalFuncs: map[string]UnmarshalFunc{"yaml": yaml.Unmarshal}, messageFile: &MessageFile{ Path: "en.yaml", Tag: language.English, Format: "yaml", Messages: []*Message{{ ID: "outer.nested.inner", Other: "value", }}, }, }, } for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { actual, err := ParseMessageFileBytes([]byte(testCase.file), testCase.path, testCase.unmarshalFuncs) if (err == nil && testCase.err != nil) || (err != nil && testCase.err == nil) || (err != nil && testCase.err != nil && err.Error() != testCase.err.Error()) { t.Fatalf("expected error %#v; got %#v", testCase.err, err) } if testCase.messageFile == nil && actual != nil || testCase.messageFile != nil && actual == nil { t.Fatalf("expected message file %#v; got %#v", testCase.messageFile, actual) } if testCase.messageFile != nil { if actual.Path != testCase.messageFile.Path { t.Errorf("expected path %q; got %q", testCase.messageFile.Path, actual.Path) } if actual.Tag != testCase.messageFile.Tag { t.Errorf("expected tag %q; got %q", testCase.messageFile.Tag, actual.Tag) } if actual.Format != testCase.messageFile.Format { t.Errorf("expected format %q; got %q", testCase.messageFile.Format, actual.Format) } if !equalMessages(actual.Messages, testCase.messageFile.Messages) { t.Errorf("expected %#v; got %#v", testCase.messageFile.Messages, actual.Messages) } } }) } } // equalMessages compares two slices of messages, ignoring private fields and order. // Sorts both input slices, which are therefore modified by this function. func equalMessages(m1, m2 []*Message) bool { if len(m1) != len(m2) { return false } var less = func(m []*Message) func(int, int) bool { return func(i, j int) bool { return m[i].ID < m[j].ID } } sort.Slice(m1, less(m1)) sort.Slice(m2, less(m2)) for i, m := range m1 { if !reflect.DeepEqual(m, m2[i]) { return false } } return true } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/internal/ 0000775 0000000 0000000 00000000000 13505412006 0022256 5 ustar 00root root 0000000 0000000 golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/internal/plural/ 0000775 0000000 0000000 00000000000 13505412006 0023555 5 ustar 00root root 0000000 0000000 golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/internal/plural/codegen/ 0000775 0000000 0000000 00000000000 13505412006 0025161 5 ustar 00root root 0000000 0000000 golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/internal/plural/codegen/generate.sh 0000664 0000000 0000000 00000000303 13505412006 0027303 0 ustar 00root root 0000000 0000000 #!/bin/sh OUT=.. go build && ./codegen -cout $OUT/rule_gen.go -tout $OUT/rule_gen_test.go && \ gofmt -w=true $OUT/rule_gen.go && \ gofmt -w=true $OUT/rule_gen_test.go && \ rm codegen golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/internal/plural/codegen/main.go 0000664 0000000 0000000 00000006505 13505412006 0026442 0 ustar 00root root 0000000 0000000 package main import ( "encoding/xml" "flag" "fmt" "io/ioutil" "os" "text/template" ) var usage = `%[1]s generates Go code to support CLDR plural rules. Usage: %[1]s [options] Options: ` func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, usage, os.Args[0]) flag.PrintDefaults() } var in, cout, tout string flag.StringVar(&in, "i", "plurals.xml", "the input XML file containing CLDR plural rules") flag.StringVar(&cout, "cout", "", "the code output file") flag.StringVar(&tout, "tout", "", "the test output file") flag.BoolVar(&verbose, "v", false, "verbose output") flag.Parse() buf, err := ioutil.ReadFile(in) if err != nil { fatalf("failed to read file: %s", err) } var data SupplementalData if err := xml.Unmarshal(buf, &data); err != nil { fatalf("failed to unmarshal xml: %s", err) } count := 0 for _, pg := range data.PluralGroups { count += len(pg.SplitLocales()) } infof("parsed %d locales", count) if cout != "" { file := openWritableFile(cout) if err := codeTemplate.Execute(file, data); err != nil { fatalf("unable to execute code template because %s", err) } else { infof("generated %s", cout) } } else { infof("not generating code file (use -cout)") } if tout != "" { file := openWritableFile(tout) if err := testTemplate.Execute(file, data); err != nil { fatalf("unable to execute test template because %s", err) } else { infof("generated %s", tout) } } else { infof("not generating test file (use -tout)") } } func openWritableFile(name string) *os.File { file, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { fatalf("failed to write file %s because %s", name, err) } return file } var codeTemplate = template.Must(template.New("rule").Parse(`// This file is generated by i18n/plural/codegen/generate.sh; DO NOT EDIT package plural // DefaultRules returns a map of Rules generated from CLDR language data. func DefaultRules() Rules { rules := Rules{} {{range .PluralGroups}} addPluralRules(rules, {{printf "%#v" .SplitLocales}}, &Rule{ PluralForms: newPluralFormSet({{range $i, $e := .PluralRules}}{{if $i}}, {{end}}{{$e.CountTitle}}{{end}}), PluralFormFunc: func(ops *Operands) Form { {{range .PluralRules}}{{if .GoCondition}} // {{.Condition}} if {{.GoCondition}} { return {{.CountTitle}} }{{end}}{{end}} return Other }, }){{end}} return rules } `)) var testTemplate = template.Must(template.New("rule").Parse(`// This file is generated by i18n/plural/codegen/generate.sh; DO NOT EDIT package plural import "testing" {{range .PluralGroups}} func Test{{.Name}}(t *testing.T) { var tests []pluralFormTest {{range .PluralRules}} {{if .IntegerExamples}}tests = appendIntegerTests(tests, {{.CountTitle}}, {{printf "%#v" .IntegerExamples}}){{end}} {{if .DecimalExamples}}tests = appendDecimalTests(tests, {{.CountTitle}}, {{printf "%#v" .DecimalExamples}}){{end}} {{end}} locales := {{printf "%#v" .SplitLocales}} for _, locale := range locales { runTests(t, locale, tests) } } {{end}} `)) func infof(format string, args ...interface{}) { fmt.Fprintf(os.Stderr, format+"\n", args...) } var verbose bool func verbosef(format string, args ...interface{}) { if verbose { infof(format, args...) } } func fatalf(format string, args ...interface{}) { infof("fatal: "+format+"\n", args...) os.Exit(1) } golang-github-nicksnyder-go-i18n.v2-2.0.2/v2/internal/plural/codegen/plurals.xml 0000664 0000000 0000000 00000054245 13505412006 0027377 0 ustar 00root root 0000000 0000000