Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jobs:
name: Lint code
runs-on: ubuntu-latest
env:
GOLANGCI_LINT_VERSION: v2.1.6 # github-releases:golangci/golangci-lint
GOLANGCI_LINT_VERSION: v2.4.0 # github-releases:golangci/golangci-lint
steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Set up Go
uses: actions/setup-go@v5
with:
Expand Down
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ linters:
- varnamelen
- wrapcheck
- wsl
- noinlineerr
settings:
cyclop:
max-complexity: 12
Expand Down
15 changes: 11 additions & 4 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func GetLocaleWithFallback(registry l10n.LocaleRegistry, locale string) (l10n.Lo
}
}
}

return loc, Response{}
}

Expand All @@ -51,9 +52,12 @@ type ResponseError interface {

// HandleError handles default and ResponseErrors. Returns true if the error was handled.
func HandleError(b *ResponseBuilder, loc l10n.LocaleInstance, err error) bool {
var resp Response
var respErr ResponseError
var l10nErr l10n.LocaleError
var (
resp Response
respErr ResponseError
l10nErr l10n.LocaleError
)

switch {
case loc == nil:
resp = Response{
Expand Down Expand Up @@ -89,6 +93,7 @@ func HandleError(b *ResponseBuilder, loc l10n.LocaleInstance, err error) bool {
// }

b.With(resp)

return true
}

Expand All @@ -99,11 +104,13 @@ func CheckForLocaleError(loc l10n.LocaleInstance) error {
return nil
}

lastErr := errs[len(errs)-1] //nolint:ifshort,nolintlint
lastErr := errs[len(errs)-1] //nolint:nolintlint

var l10nErr l10n.LocaleError
if errors.As(lastErr, &l10nErr) {
return TranslationError{l10nErr.GetLocale(), l10nErr.GetKey()}
}

return TextError{loc.GetName(), lastErr.Error()}
}

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/puzpuzpuz/xsync v1.5.2 // indirect
github.com/valyala/fastrand v1.1.0 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
Expand Down
78 changes: 16 additions & 62 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,102 +1,56 @@
github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI=
github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
github.com/aws/aws-lambda-go v1.48.0 h1:1aZUYsrJu0yo5fC4z+Rba1KhNImXcJcvHu763BxoyIo=
github.com/aws/aws-lambda-go v1.48.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
github.com/aws/aws-lambda-go v1.49.0 h1:z4VhTqkFZPM3xpEtTqWqRqsRH4TZBMJqTkRiBPYLqIQ=
github.com/aws/aws-lambda-go v1.49.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/hamba/logger/v2 v2.5.1 h1:EM+S6CPYIs66XmW9kK/RBghgtkLcO4kQWWPAlsIwNR4=
github.com/hamba/logger/v2 v2.5.1/go.mod h1:b+y7XddZMTTSIjKdOOIHWlhg1hMoE9eKhanB9wPNLj0=
github.com/hamba/logger/v2 v2.6.0 h1:73CPbhY5luRzITKeX8hhdL4K3jezsH8cyTYgcOl7JeA=
github.com/hamba/logger/v2 v2.6.0/go.mod h1:JEikfAmI6C/vaSE8MSb4SXTgWclNSN6tmdyyLv3/ATA=
github.com/hamba/logger/v2 v2.7.0 h1:CnDL1tpSlQ7OjdPlRBf2ZXSmouqsY2hWOK0Dy2oRabM=
github.com/hamba/logger/v2 v2.7.0/go.mod h1:0WAKwMox/SJ7b0dP7jZMx8S9sVXAaOU1deWQzB2lFeg=
github.com/hamba/logger/v2 v2.8.0 h1:0JJnEhVW4sHGn4/9fPP0LsZXD2ytG+NrnrXCdM8/vmg=
github.com/hamba/logger/v2 v2.8.0/go.mod h1:V58KZPAmDEWi14dOZjbKDPFkdyvpGwxXtLzLkVTNBic=
github.com/hamba/statter/v2 v2.3.4 h1:g8rJIlDhN9s1Td5x+g+uvgbI0HmTzLiZoRmCfLK8CAI=
github.com/hamba/statter/v2 v2.3.4/go.mod h1:bBparxhvlgp5rWvmaGIc6+Tc4kOYF6cUU0bDUjjaioA=
github.com/hamba/statter/v2 v2.3.5 h1:qAFu+n4Q08LtrBdcBYexItfOoJJ16D/m/boq8fm5hn4=
github.com/hamba/statter/v2 v2.3.5/go.mod h1:LS7DqETxoVpRraL3tn9O70zYCaVRL2A98SvZw/LZkAI=
github.com/hamba/statter/v2 v2.4.0 h1:0axag+o3oFGH7UC+tYXN3oY/Qq5pspQUaMpHjtxjw40=
github.com/hamba/statter/v2 v2.4.0/go.mod h1:U8ojLXmTKsBQ8HX+JVW3iqOw2Wepm4YnTYoxyim6spA=
github.com/hamba/statter/v2 v2.6.0 h1:d64DL4p48xAW4M876jrtguuEFXFf6cDFJR0WxNCDKVI=
github.com/hamba/statter/v2 v2.6.0/go.mod h1:5QpB06ilNfJNfXW5YYe4cHUlh1NGTjIlpKDv/nl7U8w=
github.com/hamba/statter/v2 v2.6.1 h1:AVNKPEyRz3y4tpzkW4a7eHcdpjUOvyEtH0eqSRQl/uY=
github.com/hamba/statter/v2 v2.6.1/go.mod h1:r3rbO7/TeKsPpL5iFMOKqSD6PzLptg+w6TPHccBNeaQ=
github.com/hamba/statter/v2 v2.7.0 h1:9CnjJ5PcxOzIVJSAFSJm0lnUUBjTo3psV9nn+yZ1cMM=
github.com/hamba/statter/v2 v2.7.0/go.mod h1:SJPj0HCM+z7GxnoG+YBgN87SP0GVJ5YPjqHINrgqFYE=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8=
github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc=
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM=
github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc=
github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg=
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
github.com/puzpuzpuz/xsync v1.5.2 h1:yRAP4wqSOZG+/4pxJ08fPTwrfL0IzE/LKQ/cw509qGY=
github.com/puzpuzpuz/xsync v1.5.2/go.mod h1:K98BYhX3k1dQ2M63t1YNVDanbwUPmBCAhNmVrrxfiGg=
github.com/prometheus/client_golang v1.22.0 h1:rb93p9lokFEsctTys46VnV1kLCDpVZ0a/Y92Vm0Zc6Q=
github.com/prometheus/client_golang v1.22.0/go.mod h1:R7ljNsLXhuQXYZYtw6GAE9AZg8Y7vEW5scdCXrWRXC0=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.0 h1:ib4sjIrwZKxE5u/Japgo/7SJV3PvgjGiRNAvTVGqQl8=
github.com/stretchr/testify v1.11.0/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
go.opentelemetry.io/otel v1.22.0 h1:xS7Ku+7yTFvDfDraDIJVpw7XPyuHlB9MCiqqX5mcJ6Y=
go.opentelemetry.io/otel v1.22.0/go.mod h1:eoV4iAi3Ea8LkAEI9+GFT44O6T/D0GWAVFyZVCC6pMI=
go.opentelemetry.io/otel v1.23.1 h1:Za4UzOqJYS+MUczKI320AtqZHZb7EqxO00jAHE0jmQY=
go.opentelemetry.io/otel v1.23.1/go.mod h1:Td0134eafDLcTS4y+zQ26GE8u3dEuRBiBCTUIRHaikA=
go.opentelemetry.io/otel v1.26.0 h1:LQwgL5s/1W7YiiRwxf03QGnWLb2HW4pLiAhaA5cZXBs=
go.opentelemetry.io/otel v1.26.0/go.mod h1:UmLkJHUAidDval2EICqBMbnAd0/m2vmpf/dAM+fvFs4=
go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts=
go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc=
go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY=
go.opentelemetry.io/otel v1.34.0/go.mod h1:OWFPOQ+h4G8xpyjgqo4SxJYdDQ/qmRH+wivy7zzx9oI=
go.opentelemetry.io/otel/trace v1.22.0 h1:Hg6pPujv0XG9QaVbGOBVHunyuLcCC3jN7WEhPx83XD0=
go.opentelemetry.io/otel/trace v1.22.0/go.mod h1:RbbHXVqKES9QhzZq/fE5UnOSILqRt40a21sPw2He1xo=
go.opentelemetry.io/otel/trace v1.23.1 h1:4LrmmEd8AU2rFvU1zegmvqW7+kWarxtNOPyeL6HmYY8=
go.opentelemetry.io/otel/trace v1.23.1/go.mod h1:4IpnpJFwr1mo/6HL8XIPJaE9y0+u1KcVmuW7dwFSVrI=
go.opentelemetry.io/otel/trace v1.26.0 h1:1ieeAUb4y0TE26jUFrCIXKpTuVK7uJGN9/Z/2LP5sQA=
go.opentelemetry.io/otel/trace v1.26.0/go.mod h1:4iDxvGDQuUkHve82hJJ8UqrwswHYsZuWCBllGV2U2y0=
go.opentelemetry.io/otel/trace v1.30.0 h1:7UBkkYzeg3C7kQX8VAidWh2biiQbtAKjyIML8dQ9wmc=
go.opentelemetry.io/otel/trace v1.30.0/go.mod h1:5EyKqTzzmyqB9bwtCCq6pDLktPK6fmGf/Dph+8VI02o=
go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k=
go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
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
17 changes: 17 additions & 0 deletions l10n/l10n.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (e MissingPlaceholderError) Error() string {
if e.Placeholder == "" {
return fmt.Sprintf("locale %s: key '%s' is missing a placeholder in translation", e.Locale, e.Key)
}

return fmt.Sprintf("locale %s: key '%s' is missing placeholder '%s' in translation", e.Locale, e.Key, e.Placeholder)
}

Expand Down Expand Up @@ -217,6 +218,7 @@ func (r *Registry) Register(l LocaleInstance, opts ...RegisterFunc) error {
if l.GetName() == "" {
return errors.New("cannot register locale with no name")
}

if _, ok := r.locales[l.GetName()]; ok {
return fmt.Errorf("locale %s already registered", l.GetName())
}
Expand Down Expand Up @@ -247,7 +249,9 @@ func (r *Registry) SetDefault(locale string) error {
if _, err := r.Resolve(locale); err != nil {
return err
}

r.defaultLocale = locale

return nil
}

Expand All @@ -262,6 +266,7 @@ func (r *Registry) Resolve(locale string) (LocaleInstance, error) {
if !ok {
return nil, fmt.Errorf("locale '%s' not found", locale)
}

return l, nil
}

Expand Down Expand Up @@ -303,7 +308,9 @@ func (l *Locale) Get(key string, args ...interface{}) string {
l.errors = append(l.errors, err)
}
}

l.appendErrorMissingParam(key, []string{t})

return t
}

Expand All @@ -320,7 +327,9 @@ func (l *Locale) GetAny(key string, args ...interface{}) string {
l.errors = append(l.errors, err)
}
}

l.appendErrorMissingParam(key, []string{t})

return t
}

Expand All @@ -337,7 +346,9 @@ func (l *Locale) GetAll(key string, args ...interface{}) []string {
l.errors = append(l.errors, err)
}
}

l.appendErrorMissingParam(key, t)

return t
}

Expand Down Expand Up @@ -369,6 +380,7 @@ func (s Snippets) GetFirst(key string, args ...interface{}) (string, error) {
if !ok || len(s[key]) == 0 {
return "", NoTranslationError{"", key, ""}
}

return fmt.Sprintf(s[key][0], args...), nil
}

Expand All @@ -378,11 +390,14 @@ func (s Snippets) GetAny(key string, args ...interface{}) (string, error) {
if !ok || len(s[key]) == 0 {
return "", NoTranslationError{"", key, ""}
}

if len(s[key]) == 1 {
return fmt.Sprintf(s[key][0], args...), nil
}

l := len(s[key])
r := rand.Intn(l) //nolint:gosec

return fmt.Sprintf(s[key][r], args...), nil
}

Expand All @@ -392,9 +407,11 @@ func (s Snippets) GetAll(key string, args ...interface{}) ([]string, error) {
if !ok || len(s[key]) == 0 {
return []string{}, NoTranslationError{"", key, ""}
}

r := make([]string, len(s[key]))
for i, v := range s[key] {
r[i] = fmt.Sprintf(v, args...)
}

return r, nil
}
9 changes: 9 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func (e NotFoundError) Error() string {
if e.name == "" {
return fmt.Sprintf("element '%s' was not found in the request", e.element)
}

return fmt.Sprintf("element '%s' with name '%s' was not found in the request", e.element, e.name)
}

Expand Down Expand Up @@ -116,6 +117,7 @@ func (r *RequestEnvelope) IntentName() string {
if err != nil {
return ""
}

return i.Name
}

Expand Down Expand Up @@ -273,6 +275,7 @@ func (r *RequestEnvelope) RequestType() RequestType {
if r.Request == nil {
return ""
}

return r.Request.Type
}

Expand All @@ -281,6 +284,7 @@ func (r *RequestEnvelope) IsIntentRequest() bool {
if r.Request == nil || r.Request.Type == "" {
return false
}

return r.Request.Type == TypeIntentRequest
}

Expand All @@ -289,6 +293,7 @@ func (r *RequestEnvelope) RequestLocale() string {
if r.Request == nil {
return ""
}

return string(r.Request.Locale)
}

Expand Down Expand Up @@ -377,6 +382,7 @@ func (r *RequestEnvelope) SessionID() string {
if r.Session == nil {
return ""
}

return r.Session.SessionID
}

Expand All @@ -385,6 +391,7 @@ func (r *RequestEnvelope) SessionUser() (*ContextUser, error) {
if r.Session == nil || r.Session.User == nil {
return nil, &NotFoundError{"Session.User", ""}
}

return r.Session.User, nil
}

Expand Down Expand Up @@ -433,6 +440,7 @@ func (r *RequestEnvelope) ContextPerson() (*ContextSystemPerson, error) {
if err != nil || s.Person == nil {
return &ContextSystemPerson{}, &NotFoundError{"System.Person", ""}
}

return r.Context.System.Person, nil
}

Expand All @@ -442,6 +450,7 @@ func (r *RequestEnvelope) ContextUser() (*ContextUser, error) {
if err != nil || s.User == nil {
return &ContextUser{}, &NotFoundError{"System.User", ""}
}

return r.Context.System.User, nil
}

Expand Down
Loading
Loading