Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
dejurin authored Oct 13, 2024
2 parents 476616e + af5be8c commit 8f84e3a
Show file tree
Hide file tree
Showing 17 changed files with 318 additions and 91 deletions.
3 changes: 2 additions & 1 deletion .github/README.zh-Hans.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# go-i18n ![Build status](https://github.com/nicksnyder/go-i18n/workflows/Build/badge.svg) [![Report card](https://goreportcard.com/badge/github.com/nicksnyder/go-i18n)](https://goreportcard.com/report/github.com/nicksnyder/go-i18n) [![codecov](https://codecov.io/gh/nicksnyder/go-i18n/branch/master/graph/badge.svg)](https://codecov.io/gh/nicksnyder/go-i18n) [![Sourcegraph](https://sourcegraph.com/github.com/nicksnyder/go-i18n/-/badge.svg)](https://sourcegraph.com/github.com/nicksnyder/go-i18n?badge)
# go-i18n
![Build status](https://github.com/nicksnyder/go-i18n/workflows/Build/badge.svg) [![Report card](https://goreportcard.com/badge/github.com/nicksnyder/go-i18n/v2)](https://goreportcard.com/report/github.com/nicksnyder/go-i18n/v2) [![codecov](https://codecov.io/gh/nicksnyder/go-i18n/graph/badge.svg?token=A9aMfR9vxG)](https://codecov.io/gh/nicksnyder/go-i18n) [![Sourcegraph](https://sourcegraph.com/github.com/nicksnyder/go-i18n/-/badge.svg)](https://sourcegraph.com/github.com/nicksnyder/go-i18n?badge)

go-i18n 是一个帮助您将 Go 程序翻译成多种语言的 Go [](#package-i18n)[命令](#command-goi18n)

Expand Down
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "monthly"
23 changes: 14 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
name: Build
on:
- push
- pull_request
push:
branches:
- main
pull_request:

jobs:
build:
name: Build
runs-on: ubuntu-latest
if: (github.event_name == 'push' && github.ref == 'refs/heads/main') || github.event_name == 'pull_request'
steps:
- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: stable
- name: Git checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Build
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v6
with:
version: latest
args: release --clean --snapshot
- name: Test
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
- name: Upload coverage
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
build_1_18:
name: Build with Go 1.18
runs-on: ubuntu-latest
if: (github.event_name == 'push' && github.ref == 'refs/heads/main') || github.event_name == 'pull_request'
steps:
- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: '1.18'
- name: Git checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Build and test
run: |
go get ./...
Expand Down
11 changes: 5 additions & 6 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
cache: false
go-version: stable
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: v1.55
version: v1.61
8 changes: 4 additions & 4 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: '^1.19.3'
go-version: stable
-
name: Release
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v6
with:
version: latest
args: release --clean
Expand Down
15 changes: 0 additions & 15 deletions .github/workflows/lsif-go.yml

This file was deleted.

23 changes: 23 additions & 0 deletions .github/workflows/scip-go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Sourcegraph code intelligence
on:
- push

jobs:
scip-go:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install scip-go
run: |
curl -L https://github.com/sourcegraph/scip-go/releases/download/v0.1.21/scip-go_0.1.21_linux_amd64.tar.gz -o scip-go.tar.gz --no-progress-meter
tar -xf scip-go.tar.gz
chmod +x ./scip-go
- name: Install src
run: |
curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o src --no-progress-meter
chmod +x ./src
- name: Generate SCIP data
run: ./scip-go
- name: Upload SCIP data to Sourcegraph.com
run: SRC_ACCESS_TOKEN=${{ secrets.SRC_ACCESS_TOKEN }} ./src code-intel upload -github-token=${{ secrets.GITHUB_TOKEN }} -no-progress

1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: 2
builds:
- binary: goi18n
main: ./goi18n/
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# go-i18n ![Build status](https://github.com/nicksnyder/go-i18n/workflows/Build/badge.svg) [![Report card](https://goreportcard.com/badge/github.com/nicksnyder/go-i18n)](https://goreportcard.com/report/github.com/nicksnyder/go-i18n) [![codecov](https://codecov.io/gh/nicksnyder/go-i18n/graph/badge.svg?token=A9aMfR9vxG)](https://codecov.io/gh/nicksnyder/go-i18n) [![Sourcegraph](https://sourcegraph.com/github.com/nicksnyder/go-i18n/-/badge.svg)](https://sourcegraph.com/github.com/nicksnyder/go-i18n?badge)
# go-i18n
![Build status](https://github.com/nicksnyder/go-i18n/workflows/Build/badge.svg) [![Report card](https://goreportcard.com/badge/github.com/nicksnyder/go-i18n/v2)](https://goreportcard.com/report/github.com/nicksnyder/go-i18n/v2) [![codecov](https://codecov.io/gh/nicksnyder/go-i18n/graph/badge.svg?token=A9aMfR9vxG)](https://codecov.io/gh/nicksnyder/go-i18n) [![Sourcegraph](https://sourcegraph.com/github.com/nicksnyder/go-i18n/-/badge.svg)](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.

Expand All @@ -7,14 +8,6 @@ go-i18n is a Go [package](#package-i18n) and a [command](#command-goi18n) that h
- 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).

<strong align="center">
<samp>

[**English**](README.md) · [**简体中文**](.github/README.zh-Hans.md)

</samp>
</strong>

## Package i18n

[![Go Reference](https://pkg.go.dev/badge/github.com/nicksnyder/go-i18n/v2/i18n.svg)](https://pkg.go.dev/github.com/nicksnyder/go-i18n/v2/i18n)
Expand Down Expand Up @@ -140,6 +133,13 @@ If you have added new messages to your program:
- Look at the [code examples](https://github.com/nicksnyder/go-i18n/blob/main/i18n/example_test.go) and [tests](https://github.com/nicksnyder/go-i18n/blob/main/i18n/localizer_test.go).
- Look at an example [application](https://github.com/nicksnyder/go-i18n/tree/main/example).

## Translations of this document

Community translations of this document may be found in the [.github](.github) folder.

These translations are maintained by the community, and are not maintained by the author of this project.
They are not guaranteed to be accurate or up-to-date.

## License

go-i18n is available under the MIT license. See the [LICENSE](LICENSE) file for more info.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.18

require (
github.com/BurntSushi/toml v1.4.0
golang.org/x/text v0.17.0
golang.org/x/text v0.19.0
gopkg.in/yaml.v3 v3.0.1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
43 changes: 42 additions & 1 deletion i18n/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ simple: simple translation
# Comment
detail:
description: detail description
description: detail description
other: detail translation
# Comment
Expand All @@ -150,6 +150,47 @@ everything:
expectMessage(t, bundle, language.AmericanEnglish, "everything", everythingMessage)
}

func TestInvalidYAML(t *testing.T) {
bundle := NewBundle(language.English)
bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
_, err := bundle.ParseMessageFileBytes([]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
garbage: something
description: translation
`), "en-US.yaml")

expectedErr := &mixedKeysError{
reservedKeys: []string{"description"},
unreservedKeys: []string{"detail", "everything", "simple"},
}
if err == nil {
t.Fatalf("expected error %#v; got nil", expectedErr)
}
if err.Error() != expectedErr.Error() {
t.Fatalf("expected error %q; got %q", expectedErr, err)
}
if c := len(bundle.messageTemplates); c > 0 {
t.Fatalf("expected no message templates in bundle; got %d", c)
}
}

func TestTOML(t *testing.T) {
bundle := NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
Expand Down
32 changes: 31 additions & 1 deletion i18n/localizer_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package i18n

import (
"errors"
"fmt"
"reflect"
"testing"
gotmpl "text/template"

"github.com/nicksnyder/go-i18n/v2/i18n/template"
"github.com/nicksnyder/go-i18n/v2/internal/plural"
Expand Down Expand Up @@ -648,6 +650,34 @@ func localizerTests() []localizerTest {
},
expectedErr: &MessageNotFoundErr{Tag: language.English, MessageID: "Hello"},
},
{
name: "use option missingkey=error with missing key",
defaultLanguage: language.English,
messages: map[language.Tag][]*Message{
language.English: {{ID: "Foo", Other: "Foo {{.bar}}"}},
},
acceptLangs: []string{"en"},
conf: &LocalizeConfig{
MessageID: "Foo",
TemplateData: map[string]string{},
TemplateParser: &template.TextParser{Option: "missingkey=error"},
},
expectedErr: gotmpl.ExecError{Name: "", Err: errors.New(`template: :1:6: executing "" at <.bar>: map has no entry for key "bar"`)},
},
{
name: "use option missingkey=default with missing key",
defaultLanguage: language.English,
messages: map[language.Tag][]*Message{
language.English: {{ID: "Foo", Other: "Foo {{.bar}}"}},
},
acceptLangs: []string{"en"},
conf: &LocalizeConfig{
MessageID: "Foo",
TemplateData: map[string]string{},
TemplateParser: &template.TextParser{Option: "missingkey=default"},
},
expectedLocalized: "Foo <no value>",
},
}
}

Expand All @@ -663,7 +693,7 @@ func TestLocalizer_Localize(t *testing.T) {
check := func(localized string, err error) {
t.Helper()
if !reflect.DeepEqual(err, test.expectedErr) {
t.Errorf("expected error %#v; got %#v", test.expectedErr, err)
t.Errorf("\nexpected error: %#v\n got error: %#v", test.expectedErr, err)
}
if localized != test.expectedLocalized {
t.Errorf("expected localized string %q; got %q", test.expectedLocalized, localized)
Expand Down
Loading

0 comments on commit 8f84e3a

Please sign in to comment.