Skip to content

Commit

Permalink
ref(locale): define i18n translatable content (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Aug 24, 2024
1 parent 08a3d01 commit 88d6c45
Show file tree
Hide file tree
Showing 30 changed files with 746 additions and 238 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"gomega",
"gomnd",
"goreleaser",
"goroutines",
"gosec",
"gosimple",
"goveralls",
Expand Down Expand Up @@ -62,6 +63,7 @@
"Taskfile",
"testcache",
"thelper",
"toplevel",
"tparallel",
"typecheck",
"unconvert",
Expand Down
37 changes: 31 additions & 6 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ silent: true

dotenv: [".env"]

vars:
FORMAT: json
BINARY_NAME: pants
DEPLOY_DIR: ./locale/deploy
OUT_DIR: ./locale/out
L10N_DIR: ./locale/out/l10n
#
SOURCE_LANG: en-GB
SOURCE_ACTIVE: "active.{{.SOURCE_LANG}}.{{.FORMAT}}"
#
LANGUAGE_US: en-US
US_OUT_DIR: "{{.OUT_DIR}}/{{.LANGUAGE_US}}"
ACTIVE_US: "{{.BINARY_NAME}}.active.en-US.{{.FORMAT}}"
TRANSLATE_US: "{{.BINARY_NAME}}.translate.en-US.{{.FORMAT}}"
TRANSLATE_US_FILEPATH: "{{.US_OUT_DIR}}/{{.TRANSLATE_US}}"

tasks:

# === build ================================================
Expand Down Expand Up @@ -166,30 +182,39 @@ tasks:

clear:
cmds:
- rm ./locale/out/* --recursive
- rm -rf {{.OUT_DIR}}/* --recursive

# extract i18m messages
extract:
cmds:
- mkdir -p ./locale/out/l10n
- goi18n extract
-format json
-sourceLanguage "en-GB"
-outdir ./locale/out/l10n

# new translation
# where is the default? locale/default/li18ngo.active.en-GB.json not populated
# ! creates: locale/out/l10n/active.en-GB.json => extracted output
# ! creates: locale/out/l10n/translate.en-US.json => empty
newt:
deps: [extract]
cmds:
- touch ./locale/out/l10n/translate.en-US.json

# derive a translation from the default
# ! the default active file does not contain hashes, just the extracted content
# ! the foreign translate file contains the hashes
# ! the active foreign file (locale/out/l10n/active.en-US.json) is empty
# ! pass the translate file(locale/out/l10n/translate.en-US.json) to your translator
#
merge:
cmds:
- goi18n merge
-format json
-sourceLanguage "en-GB"
-outdir ./locale/out
./locale/out/active.en-GB.json ./locale/out/l10n/translate.en-US.json
-outdir ./locale/out/l10n
./locale/out/l10n/active.en-GB.json ./locale/out/l10n/translate.en-US.json

# update existing translations
# after running this task, the translation file generated will
Expand All @@ -203,7 +228,7 @@ tasks:
-format json
-sourceLanguage "en-GB"
-outdir ./locale/out
./locale/out/active.en-GB.json ./locale/deploy/active.en-US.json
./locale/out/active.en-GB.json ./i18n/deploy/active.en-US.json

# run this after manual translation has occurred to integrate it
# back into the translation file. Unfortunately, this task doesn't
Expand All @@ -215,5 +240,5 @@ tasks:
- goi18n merge
-format json
-sourceLanguage "en-US"
-outdir ./locale/temp
./locale/out/translate.en-US.json ./locale/deploy/active.en-US.json
-outdir ./i18n/temp
./locale/out/translate.en-US.json ./i18n/deploy/active.en-US.json
8 changes: 5 additions & 3 deletions generic-pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package pants

import (
"context"
"errors"
"time"

"github.com/snivilised/pants/internal/ants"
"github.com/snivilised/pants/internal/lo"
"github.com/snivilised/pants/locale"
)

// functionalPool
Expand Down Expand Up @@ -130,7 +130,9 @@ func fromOutputInfo[O any](o *Options, oi *outputInfo[O]) *outputInfoW[O] {
}
}

func respond[O any](ctx context.Context, wi *outputInfoW[O], output *JobOutput[O]) (err error) {
func respond[O any](ctx context.Context,
wi *outputInfoW[O], output *JobOutput[O],
) (err error) {
select {
case wi.outputCh <- *output:
return nil
Expand All @@ -139,7 +141,7 @@ func respond[O any](ctx context.Context, wi *outputInfoW[O], output *JobOutput[O
case <-ctx.Done():
err = ctx.Err()
case wi.cancelCh <- CancelWorkSignal{}:
err = errors.New("timeout")
err = locale.ErrTimeout
}

case <-ctx.Done():
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ require (
github.com/nicksnyder/go-i18n/v2 v2.4.0
github.com/onsi/ginkgo/v2 v2.20.0
github.com/onsi/gomega v1.34.1
github.com/snivilised/li18ngo v0.1.2
)

require (
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/tools v0.24.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ github.com/onsi/ginkgo/v2 v2.20.0 h1:PE84V2mHqoT1sglvHc8ZdQtPcwmvvt29WLEEO3xmdZw
github.com/onsi/ginkgo/v2 v2.20.0/go.mod h1:lG9ey2Z29hR41WMVthyJBGUBcBhGOtoPF2VFMvBXFCI=
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/snivilised/li18ngo v0.1.2 h1:xiOZnAcIkeHfEVuTy/FBwA1u9IrAfYSjO/DYMC77z/I=
github.com/snivilised/li18ngo v0.1.2/go.mod h1:Or3qUhpR6AM1X51i82RtyCvORWy2/hrxY9lg1i1gFTE=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
Expand Down
42 changes: 39 additions & 3 deletions internal/ants/ants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,52 @@ package ants_test

import (
"context"
"fmt"
"os"
"runtime"
"sync"
"time"

. "github.com/onsi/ginkgo/v2" //nolint:revive // ok
. "github.com/onsi/gomega" //nolint:revive // ok

"github.com/snivilised/li18ngo"
"github.com/snivilised/pants"
"github.com/snivilised/pants/internal/ants"
"github.com/snivilised/pants/internal/helpers"
"github.com/snivilised/pants/locale"
)

var _ = Describe("Ants", func() {
var _ = Describe("Ants", Ordered, func() {
var (
repo string
l10nPath string
testTranslationFile li18ngo.TranslationFiles
)

BeforeAll(func() {
repo = helpers.Repo("")
l10nPath = helpers.Path(repo, "test/data/l10n")

_, err := os.Stat(l10nPath)
Expect(err).To(Succeed(),
fmt.Sprintf("l10n '%v' path does not exist", l10nPath),
)

testTranslationFile = li18ngo.TranslationFiles{
li18ngo.Li18ngoSourceID: li18ngo.TranslationSource{Name: "test"},
}
})

BeforeEach(func() {
if err := li18ngo.Use(func(o *li18ngo.UseOptions) {
o.Tag = li18ngo.DefaultLanguage
o.From.Sources = testTranslationFile
}); err != nil {
Fail(err.Error())
}
})

Context("NewPool", func() {
Context("Submit", func() {
When("non-blocking", func() {
Expand Down Expand Up @@ -49,7 +83,8 @@ var _ = Describe("Ants", func() {
Expect(pool.Submit(ctx, fn)).To(Succeed(),
"nonblocking submit when pool is not full shouldn't return error",
)
Expect(pool.Submit(ctx, demoFunc)).To(MatchError(ants.ErrPoolOverload.Error()),
Expect(pool.Submit(ctx, demoFunc)).To(
MatchError(locale.ErrPoolOverload.Error()),
"nonblocking submit when pool is full should get an ErrPoolOverload",
)

Expand Down Expand Up @@ -104,7 +139,8 @@ var _ = Describe("Ants", func() {
}()
time.Sleep(1 * time.Second)
// already reached max blocking limit
Expect(pool.Submit(ctx, demoFunc)).To(MatchError(ants.ErrPoolOverload.Error()),
Expect(pool.Submit(ctx, demoFunc)).To(
MatchError(locale.ErrPoolOverload.Error()),
"blocking submit when pool reach max blocking submit should return ErrPoolOverload",
)

Expand Down
29 changes: 0 additions & 29 deletions internal/ants/errors.go

This file was deleted.

11 changes: 6 additions & 5 deletions internal/ants/pool-func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"time"

"github.com/snivilised/pants/internal/ants/async"
"github.com/snivilised/pants/locale"
)

// PoolWithFunc accepts the tasks and process them concurrently,
Expand Down Expand Up @@ -135,7 +136,7 @@ func NewPoolWithFunc(ctx context.Context,
options ...Option,
) (*PoolWithFunc, error) {
if pf == nil {
return nil, ErrLackPoolFunc
return nil, locale.ErrLackPoolFunc
}

opts := NewOptions(options...)
Expand All @@ -147,7 +148,7 @@ func NewPoolWithFunc(ctx context.Context,

if !opts.DisablePurge {
if expiry := opts.ExpiryDuration; expiry < 0 {
return nil, ErrInvalidPoolExpiry
return nil, locale.ErrInvalidPoolExpiry
} else if expiry == 0 {
opts.ExpiryDuration = DefaultCleanIntervalTime
}
Expand Down Expand Up @@ -193,7 +194,7 @@ func NewPoolWithFunc(ctx context.Context,
// you should instantiate a PoolWithFunc with ants.WithNonblocking(true).
func (p *PoolWithFunc) Invoke(ctx context.Context, job InputParam) error {
if p.IsClosed() {
return ErrPoolClosed
return locale.ErrPoolClosed
}

w, err := p.retrieveWorker()
Expand Down Expand Up @@ -242,7 +243,7 @@ retry:
if p.o.Nonblocking || exceeded {
p.lock.Unlock()

return nil, ErrPoolOverload
return nil, locale.ErrPoolOverload
}

// Otherwise, we'll have to keep them blocked and wait for at least one worker
Expand All @@ -254,7 +255,7 @@ retry:
if p.IsClosed() {
p.lock.Unlock()

return nil, ErrPoolClosed
return nil, locale.ErrPoolClosed
}

goto retry
Expand Down
9 changes: 5 additions & 4 deletions internal/ants/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"time"

"github.com/snivilised/pants/internal/ants/async"
"github.com/snivilised/pants/locale"
)

// Pool accepts the tasks and process them concurrently,
Expand Down Expand Up @@ -140,7 +141,7 @@ func NewPool(ctx context.Context, options ...Option) (*Pool, error) {

if !opts.DisablePurge {
if expiry := opts.ExpiryDuration; expiry < 0 {
return nil, ErrInvalidPoolExpiry
return nil, locale.ErrInvalidPoolExpiry
} else if expiry == 0 {
opts.ExpiryDuration = DefaultCleanIntervalTime
}
Expand Down Expand Up @@ -188,7 +189,7 @@ func NewPool(ctx context.Context, options ...Option) (*Pool, error) {
// a Pool with ants.WithNonblocking(true).
func (p *Pool) Submit(ctx context.Context, task TaskFunc) error {
if p.IsClosed() {
return ErrPoolClosed
return locale.ErrPoolClosed
}

w, err := p.retrieveWorker()
Expand Down Expand Up @@ -239,7 +240,7 @@ retry:
if p.o.Nonblocking || exceeded {
p.lock.Unlock()

return nil, ErrPoolOverload
return nil, locale.ErrPoolOverload
}

// Otherwise, we'll have to keep them blocked and wait for at least one
Expand All @@ -251,7 +252,7 @@ retry:
if p.IsClosed() {
p.lock.Unlock()

return nil, ErrPoolClosed
return nil, locale.ErrPoolClosed
}

goto retry
Expand Down
6 changes: 4 additions & 2 deletions internal/ants/worker-loop-queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ package ants
import (
"context"
"time"

"github.com/snivilised/pants/locale"
)

type loopQueue struct {
Expand Down Expand Up @@ -65,11 +67,11 @@ func (wq *loopQueue) isEmpty() bool {

func (wq *loopQueue) insert(w worker) error {
if wq.size == 0 {
return errQueueIsReleased
return locale.ErrQueueIsReleased
}

if wq.isFull {
return errQueueIsFull
return locale.ErrQueueIsFull
}
wq.items[wq.tail] = w
wq.tail = (wq.tail + 1) % wq.size
Expand Down
Loading

0 comments on commit 88d6c45

Please sign in to comment.