Skip to content

Commit 702a2c2

Browse files
committed
src: translator: renaming
Signed-off-by: Dmitrii Aleksandrov <goodmobiledevices@gmail.com>
1 parent 9365238 commit 702a2c2

File tree

8 files changed

+31
-30
lines changed

8 files changed

+31
-30
lines changed

examples/translation/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"regexp"
8+
79
"github.com/google/uuid"
810
"github.com/insei/valigo"
911
"github.com/insei/valigo/str"
1012
"github.com/insei/valigo/translator"
11-
"regexp"
1213
)
1314

1415
type Sender struct {
@@ -32,7 +33,7 @@ const (
3233
func manualValidatorSettings() *valigo.Validator {
3334
tStorage := translator.NewInMemStorage()
3435
// if you want to add new translation for you custom validators
35-
tStorage.AddTranslations("en", map[string]string{
36+
tStorage.Add("en", map[string]string{
3637
customRegexpLocaleKey: customRegexpLocaleMsg,
3738
})
3839
t := translator.New(translator.WithStorage(tStorage), translator.WithDefaultLang("en"))

options_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const (
1515

1616
func TestOptionApply(t *testing.T) {
1717
tStorage := translator.NewInMemStorage()
18-
tStorage.AddTranslations("en", map[string]string{
18+
tStorage.Add("en", map[string]string{
1919
customRegexpLocaleKey: customRegexpLocaleMsg,
2020
})
2121
tr := translator.New(translator.WithStorage(tStorage), translator.WithDefaultLang("en"))
@@ -60,7 +60,7 @@ func TestOptionApplyWithNilFieldLocationNamingFn(t *testing.T) {
6060

6161
func TestOptionApplyWithMultipleOptions(t *testing.T) {
6262
tStorage := translator.NewInMemStorage()
63-
tStorage.AddTranslations("en", map[string]string{
63+
tStorage.Add("en", map[string]string{
6464
customRegexpLocaleKey: customRegexpLocaleMsg,
6565
})
6666
tr := translator.New(translator.WithStorage(tStorage), translator.WithDefaultLang("en"))

translator/inmem.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type inMemTranslatorStorage struct {
1111
// NewInMemStorage creates a new in-memory translation storage.
1212
// It loads initial data from the embedded locales YAML file.
1313
// If an error occurs during loading, it panics.
14-
func NewInMemStorage(opts ...InMemStorageOption) TranslationStorage {
14+
func NewInMemStorage(opts ...InMemStorageOption) Storage {
1515
data, err := LocalesFromFS(EmbedFSLocalesYAML)
1616
if err != nil {
1717
panic(err)
@@ -23,8 +23,8 @@ func NewInMemStorage(opts ...InMemStorageOption) TranslationStorage {
2323
return storage
2424
}
2525

26-
// AddTranslations adds new translations for a given language to the storage.
27-
func (t *inMemTranslatorStorage) AddTranslations(lang string, data map[string]string) {
26+
// Add adds new translations for a given language to the storage.
27+
func (t *inMemTranslatorStorage) Add(lang string, data map[string]string) {
2828
if _, ok := t.translations[lang]; !ok {
2929
t.translations[lang] = data
3030
}
@@ -33,8 +33,8 @@ func (t *inMemTranslatorStorage) AddTranslations(lang string, data map[string]st
3333
}
3434
}
3535

36-
// GetTranslated returns the translated value for a given format and language preferences.
37-
func (t *inMemTranslatorStorage) GetTranslated(prefer []string, format string, args ...any) string {
36+
// Get returns the translated value for a given format and language preferences.
37+
func (t *inMemTranslatorStorage) Get(prefer []string, format string, args ...any) string {
3838
translatedFormat := format
3939
for _, preferLang := range prefer {
4040
langFormat, ok := t.translations[preferLang][format]

translator/inmem_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func TestInMemTranslatorStorage(t *testing.T) {
1010
data := map[string]string{
1111
"hello": "Hello, %s!",
1212
}
13-
storage.AddTranslations(lang, data)
13+
storage.Add(lang, data)
1414
if _, ok := storage.(*inMemTranslatorStorage).translations[lang]; !ok {
1515
t.Errorf("expected translations to be added for lang %s", lang)
1616
}
@@ -21,15 +21,15 @@ func TestInMemTranslatorStorage(t *testing.T) {
2121
prefer := []string{"en", "fr"}
2222
format := "hello"
2323
args := []any{"John"}
24-
translated := storage.GetTranslated(prefer, format, args...)
24+
translated := storage.Get(prefer, format, args...)
2525
if translated != "Hello, John!" {
2626
t.Errorf("expected translated string to be 'Hello, John!', got '%s'", translated)
2727
}
2828

2929
prefer = []string{"es", "fr"}
3030
format = "hello"
3131
args = []any{"John"}
32-
translated = storage.GetTranslated(prefer, format, args...)
32+
translated = storage.Get(prefer, format, args...)
3333
if translated != "hello%!(EXTRA string=John)" {
3434
t.Errorf("expected translated string to be empty, got '%s'", translated)
3535
}
@@ -42,12 +42,12 @@ func TestInMemTranslatorStorageAddTranslations_ExistingLang(t *testing.T) {
4242
data := map[string]string{
4343
"hello": "Hello, %s!",
4444
}
45-
storage.AddTranslations(lang, data)
45+
storage.Add(lang, data)
4646

4747
newData := map[string]string{
4848
"goodbye": "Goodbye, %s!",
4949
}
50-
storage.AddTranslations(lang, newData)
50+
storage.Add(lang, newData)
5151

5252
if _, ok := storage.(*inMemTranslatorStorage).translations[lang]["goodbye"]; !ok {
5353
t.Errorf("expected new translation to be added for key 'goodbye' in lang %s", lang)
@@ -59,7 +59,7 @@ func TestInMemTranslatorStorage_GetTranslated_NoPrefer(t *testing.T) {
5959

6060
format := "hello"
6161
args := []any{"John"}
62-
translated := storage.GetTranslated(nil, format, args...)
62+
translated := storage.Get(nil, format, args...)
6363
if translated != "hello%!(EXTRA string=John)" {
6464
t.Errorf("expected translated string to be empty, got '%s'", translated)
6565
}

translator/options.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "context"
44

55
// storageOption represents an option to set the storage for a translator.
66
type storageOption struct {
7-
storage TranslationStorageRO
7+
storage StorageRO
88
}
99

1010
// apply sets the storage for the given translator.
@@ -13,7 +13,7 @@ func (o *storageOption) apply(t *translator) {
1313
}
1414

1515
// WithStorage returns an option to set the storage for a translator.
16-
func WithStorage(storage TranslationStorageRO) Option {
16+
func WithStorage(storage StorageRO) Option {
1717
return &storageOption{storage}
1818
}
1919

translator/options_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const mockTranslatedString = "mock translated string"
1212

1313
type mockStorage struct{}
1414

15-
func (m *mockStorage) GetTranslated(prefer []string, format string, args ...any) string {
15+
func (m *mockStorage) Get(prefer []string, format string, args ...any) string {
1616
return mockTranslatedString
1717
}
1818

translator/translator.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// Translator is a struct that handles translations.
1010
type translator struct {
11-
storage TranslationStorageRO
11+
storage StorageRO
1212
getPreferredLanguages func(ctx context.Context) []string
1313
defaultLang string
1414
}
@@ -25,12 +25,12 @@ func (t *translator) getLanguages(ctx context.Context) []string {
2525

2626
// ErrorT returns a translated error message.
2727
func (t *translator) ErrorT(ctx context.Context, format string, args ...any) error {
28-
return fmt.Errorf(t.storage.GetTranslated(t.getLanguages(ctx), format, args...))
28+
return fmt.Errorf(t.storage.Get(t.getLanguages(ctx), format, args...))
2929
}
3030

3131
// T returns a translated string.
3232
func (t *translator) T(ctx context.Context, format string, args ...any) string {
33-
return t.storage.GetTranslated(t.getLanguages(ctx), format, args...)
33+
return t.storage.Get(t.getLanguages(ctx), format, args...)
3434
}
3535

3636
// New returns a new Translator instance with default settings.

translator/types.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ type Translator interface {
1313
T(ctx context.Context, format string, args ...any) string
1414
}
1515

16-
// TranslationStorageRO interface defines a method for retrieving translated text.
17-
type TranslationStorageRO interface {
18-
// GetTranslated returns a translated string based on the provided preferences, format, and arguments.
19-
GetTranslated(prefer []string, format string, args ...any) string
16+
// StorageRO interface defines a method for retrieving translated text.
17+
type StorageRO interface {
18+
// Get returns a translated string based on the provided preferences, format, and arguments.
19+
Get(prefer []string, format string, args ...any) string
2020
}
2121

22-
// TranslationStorage interface extends TranslationStorageRO and adds a method for adding new translations.
23-
type TranslationStorage interface {
24-
TranslationStorageRO
22+
// Storage interface extends StorageRO and adds a method for adding new translations.
23+
type Storage interface {
24+
StorageRO
2525

26-
// AddTranslations adds translations for a specific language.
27-
AddTranslations(lang string, data map[string]string)
26+
// Add adds translations for a specific language.
27+
Add(lang string, data map[string]string)
2828
}
2929

3030
// Option interface defines a method for applying options to a translator.

0 commit comments

Comments
 (0)