Skip to content

Commit

Permalink
Merge pull request #1181 from nyaruka/i18n
Browse files Browse the repository at this point in the history
Switch to i18n package from gocommon
  • Loading branch information
rowanseymour authored Sep 4, 2023
2 parents d26cae2 + 3d44ef6 commit 67f81b1
Show file tree
Hide file tree
Showing 88 changed files with 519 additions and 747 deletions.
4 changes: 2 additions & 2 deletions assets/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package assets
import (
"fmt"

"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/envs"
)

// ChannelUUID is the UUID of a channel
Expand Down Expand Up @@ -41,7 +41,7 @@ type Channel interface {
Schemes() []string
Roles() []ChannelRole
Parent() *ChannelReference
Country() envs.Country
Country() i18n.Country
MatchPrefixes() []string
AllowInternational() bool
}
Expand Down
8 changes: 4 additions & 4 deletions assets/static/channel.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package static

import (
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
)

// Channel is a JSON serializable implementation of a channel asset
Expand All @@ -14,7 +14,7 @@ type Channel struct {
Schemes_ []string `json:"schemes" validate:"min=1"`
Roles_ []assets.ChannelRole `json:"roles" validate:"min=1,dive,eq=send|eq=receive|eq=call|eq=answer|eq=ussd"`
Parent_ *assets.ChannelReference `json:"parent" validate:"omitempty,dive"`
Country_ envs.Country `json:"country,omitempty"`
Country_ i18n.Country `json:"country,omitempty"`
MatchPrefixes_ []string `json:"match_prefixes,omitempty"`
AllowInternational_ bool `json:"allow_international,omitempty"`
}
Expand All @@ -33,7 +33,7 @@ func NewChannel(uuid assets.ChannelUUID, name string, address string, schemes []
}

// NewTelChannel creates a new tel channel
func NewTelChannel(uuid assets.ChannelUUID, name string, address string, roles []assets.ChannelRole, parent *assets.ChannelReference, country envs.Country, matchPrefixes []string, allowInternational bool) assets.Channel {
func NewTelChannel(uuid assets.ChannelUUID, name string, address string, roles []assets.ChannelRole, parent *assets.ChannelReference, country i18n.Country, matchPrefixes []string, allowInternational bool) assets.Channel {
return &Channel{
UUID_: uuid,
Name_: name,
Expand Down Expand Up @@ -66,7 +66,7 @@ func (c *Channel) Roles() []assets.ChannelRole { return c.Roles_ }
func (c *Channel) Parent() *assets.ChannelReference { return c.Parent_ }

// Country returns this channel's associated country code (if any)
func (c *Channel) Country() envs.Country { return c.Country_ }
func (c *Channel) Country() i18n.Country { return c.Country_ }

// MatchPrefixes returns this channel's match prefixes values used for selecting a channel for a URN (if any)
func (c *Channel) MatchPrefixes() []string { return c.MatchPrefixes_ }
Expand Down
6 changes: 3 additions & 3 deletions assets/static/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package static_test
import (
"testing"

"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/assets/static"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/utils"

"github.com/stretchr/testify/assert"
Expand All @@ -26,7 +26,7 @@ func TestChannel(t *testing.T) {
assert.Equal(t, []string{"tel"}, channel.Schemes())
assert.Equal(t, []assets.ChannelRole{assets.ChannelRoleSend}, channel.Roles())
assert.Nil(t, channel.Parent())
assert.Equal(t, envs.NilCountry, channel.Country())
assert.Equal(t, i18n.NilCountry, channel.Country())
assert.Nil(t, channel.MatchPrefixes())
assert.True(t, channel.AllowInternational())

Expand All @@ -44,7 +44,7 @@ func TestChannel(t *testing.T) {
false,
)

assert.Equal(t, envs.Country("RW"), channel.Country())
assert.Equal(t, i18n.Country("RW"), channel.Country())
assert.Equal(t, []string{"+25079"}, channel.MatchPrefixes())
assert.False(t, channel.AllowInternational())
}
8 changes: 4 additions & 4 deletions assets/static/template.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package static

import (
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
)

// Template is a JSON serializable implementation of a template asset
Expand Down Expand Up @@ -40,13 +40,13 @@ func (t *Template) Translations() []assets.TemplateTranslation {
type TemplateTranslation struct {
Channel_ assets.ChannelReference `json:"channel" validate:"required"`
Content_ string `json:"content" validate:"required"`
Locale_ envs.Locale `json:"locale" validate:"required"`
Locale_ i18n.Locale `json:"locale" validate:"required"`
Namespace_ string `json:"namespace"`
VariableCount_ int `json:"variable_count"`
}

// NewTemplateTranslation creates a new template translation
func NewTemplateTranslation(channel assets.ChannelReference, locale envs.Locale, content string, variableCount int, namespace string) *TemplateTranslation {
func NewTemplateTranslation(channel assets.ChannelReference, locale i18n.Locale, content string, variableCount int, namespace string) *TemplateTranslation {
return &TemplateTranslation{
Channel_: channel,
Content_: content,
Expand All @@ -63,7 +63,7 @@ func (t *TemplateTranslation) Content() string { return t.Content_ }
func (t *TemplateTranslation) Namespace() string { return t.Namespace_ }

// Language returns the locale this translation is in
func (t *TemplateTranslation) Locale() envs.Locale { return t.Locale_ }
func (t *TemplateTranslation) Locale() i18n.Locale { return t.Locale_ }

// VariableCount returns the number of variables in this template
func (t *TemplateTranslation) VariableCount() int { return t.VariableCount_ }
Expand Down
7 changes: 3 additions & 4 deletions assets/static/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package static
import (
"testing"

"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"

"github.com/stretchr/testify/assert"
)

Expand All @@ -16,9 +15,9 @@ func TestTemplate(t *testing.T) {
UUID: assets.ChannelUUID("ffffffff-9b24-92e1-ffff-ffffb207cdb4"),
}

translation := NewTemplateTranslation(channel, envs.Locale("eng-US"), "Hello {{1}}", 1, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b")
translation := NewTemplateTranslation(channel, i18n.Locale("eng-US"), "Hello {{1}}", 1, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b")
assert.Equal(t, channel, translation.Channel())
assert.Equal(t, envs.Locale("eng-US"), translation.Locale())
assert.Equal(t, i18n.Locale("eng-US"), translation.Locale())
assert.Equal(t, "Hello {{1}}", translation.Content())
assert.Equal(t, 1, translation.VariableCount())
assert.Equal(t, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", translation.Namespace())
Expand Down
4 changes: 2 additions & 2 deletions assets/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package assets
import (
"fmt"

"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/envs"
)

// TemplateUUID is the UUID of a template
Expand Down Expand Up @@ -45,7 +45,7 @@ type Template interface {
// TemplateTranslation represents a single translation for a specific template and channel
type TemplateTranslation interface {
Content() string
Locale() envs.Locale
Locale() i18n.Locale
Namespace() string
VariableCount() int
Channel() ChannelReference
Expand Down
10 changes: 5 additions & 5 deletions cmd/docgen/docs/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/goflow/utils/i18n"
"github.com/nyaruka/goflow/utils/po"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -47,7 +47,7 @@ func Generate(baseDir, outputDir, localeDir string) error {
fmt.Printf(" > Found %d tagged items with tag %s\n", len(v), k)
}

locales := i18n.NewLibrary(localeDir, srcLocale)
locales := po.NewLibrary(localeDir, srcLocale)

// keep track of all unique strings we look up via gettext
msgIDs := make(map[string]bool)
Expand All @@ -64,9 +64,9 @@ func Generate(baseDir, outputDir, localeDir string) error {
}

// use the unique messages set to create a new POT file
pot := i18n.NewPO(i18n.NewPOHeader("Generated by goflow docgen", dates.Now(), srcLocale))
pot := po.NewPO(po.NewPOHeader("Generated by goflow docgen", dates.Now(), srcLocale))
for msgID := range msgIDs {
pot.AddEntry(&i18n.POEntry{MsgID: msgID})
pot.AddEntry(&po.POEntry{MsgID: msgID})
}
pot.Sort()

Expand All @@ -79,7 +79,7 @@ func Generate(baseDir, outputDir, localeDir string) error {
}

// generates all documentation a given language by invoking all generators
func generateForLang(baseDir, outputDir string, items map[string][]*TaggedItem, locales *i18n.Library, locale string, msgUsed func(string)) error {
func generateForLang(baseDir, outputDir string, items map[string][]*TaggedItem, locales *po.Library, locale string, msgUsed func(string)) error {
fmt.Printf("Generating docs in '%s'\n", locale)

// en_US -> en-us
Expand Down
7 changes: 4 additions & 3 deletions cmd/flowrunner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/buger/jsonparser"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/stringsx"
"github.com/nyaruka/gocommon/urns"
Expand Down Expand Up @@ -72,7 +73,7 @@ func main() {

engine := createEngine(witToken)

repro, err := RunFlow(engine, assetsPath, flowUUID, initialMsg, envs.Language(contactLang), os.Stdin, os.Stdout)
repro, err := RunFlow(engine, assetsPath, flowUUID, initialMsg, i18n.Language(contactLang), os.Stdin, os.Stdout)

if err != nil {
fmt.Println(err.Error())
Expand Down Expand Up @@ -103,7 +104,7 @@ func createEngine(witToken string) flows.Engine {
}

// RunFlow steps through a flow
func RunFlow(eng flows.Engine, assetsPath string, flowUUID assets.FlowUUID, initialMsg string, contactLang envs.Language, in io.Reader, out io.Writer) (*Repro, error) {
func RunFlow(eng flows.Engine, assetsPath string, flowUUID assets.FlowUUID, initialMsg string, contactLang i18n.Language, in io.Reader, out io.Writer) (*Repro, error) {
assetsJSON, err := os.ReadFile(assetsPath)
if err != nil {
return nil, errors.Wrapf(err, "error reading assets file '%s'", assetsPath)
Expand Down Expand Up @@ -141,7 +142,7 @@ func RunFlow(eng flows.Engine, assetsPath string, flowUUID assets.FlowUUID, init

// create our environment
la, _ := time.LoadLocation("America/Los_Angeles")
languages := []envs.Language{flow.Language(), contact.Language()}
languages := []i18n.Language{flow.Language(), contact.Language()}
env := envs.NewBuilder().WithTimezone(la).WithAllowedLanguages(languages).Build()

repro := &Repro{}
Expand Down
6 changes: 3 additions & 3 deletions cmd/flowxgettext/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"

"github.com/buger/jsonparser"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/definition"
"github.com/nyaruka/goflow/flows/definition/migrations"
Expand All @@ -32,13 +32,13 @@ func main() {
flags.PrintDefaults()
os.Exit(1)
}
if err := FlowXGetText(envs.Language(lang), excludeArgs, args, os.Stdout); err != nil {
if err := FlowXGetText(i18n.Language(lang), excludeArgs, args, os.Stdout); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}

func FlowXGetText(lang envs.Language, excludeArgs bool, paths []string, writer io.Writer) error {
func FlowXGetText(lang i18n.Language, excludeArgs bool, paths []string, writer io.Writer) error {
sources, err := loadFlows(paths)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/flowxgettext/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"time"

"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/i18n"
main "github.com/nyaruka/goflow/cmd/flowxgettext"
"github.com/nyaruka/goflow/envs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -18,7 +18,7 @@ func TestFlowXGetText(t *testing.T) {

out := &strings.Builder{}

err := main.FlowXGetText(envs.Language("fra"), false, []string{"../../test/testdata/runner/two_questions.json"}, out)
err := main.FlowXGetText(i18n.Language("fra"), false, []string{"../../test/testdata/runner/two_questions.json"}, out)
require.NoError(t, err)

assert.Contains(t, out.String(), `
Expand Down
3 changes: 2 additions & 1 deletion contactql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/antlr/antlr4/runtime/Go/antlr/v4"
"github.com/nyaruka/gocommon/i18n"
gen "github.com/nyaruka/goflow/antlr/gen/contactql"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
Expand Down Expand Up @@ -202,7 +203,7 @@ func (c *Condition) validate(env envs.Environment, resolver Resolver) error {
}
} else if c.propKey == AttributeLanguage {
if c.value != "" {
_, err := envs.ParseLanguage(c.value)
_, err := i18n.ParseLanguage(c.value)
if err != nil {
return NewQueryError(ErrInvalidLanguage, "'%s' is not a valid language code", c.value).withExtra("value", c.value)
}
Expand Down
37 changes: 0 additions & 37 deletions envs/country.go

This file was deleted.

30 changes: 0 additions & 30 deletions envs/country_test.go

This file was deleted.

Loading

0 comments on commit 67f81b1

Please sign in to comment.