Skip to content

Commit 9cd7453

Browse files
committed
src: translator: ad merge method for load translations and merge with existing
Signed-off-by: Dmitrii Aleksandrov <goodmobiledevices@gmail.com>
1 parent 702a2c2 commit 9cd7453

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

translator/inmem.go

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func NewInMemStorage(opts ...InMemStorageOption) Storage {
2525

2626
// Add adds new translations for a given language to the storage.
2727
func (t *inMemTranslatorStorage) Add(lang string, data map[string]string) {
28+
if data == nil {
29+
return
30+
}
2831
if _, ok := t.translations[lang]; !ok {
2932
t.translations[lang] = data
3033
}
@@ -33,6 +36,15 @@ func (t *inMemTranslatorStorage) Add(lang string, data map[string]string) {
3336
}
3437
}
3538

39+
func (t *inMemTranslatorStorage) Merge(locales map[string]map[string]string) {
40+
if t.translations == nil {
41+
return
42+
}
43+
for lang, data := range locales {
44+
t.Add(lang, data)
45+
}
46+
}
47+
3648
// Get returns the translated value for a given format and language preferences.
3749
func (t *inMemTranslatorStorage) Get(prefer []string, format string, args ...any) string {
3850
translatedFormat := format

translator/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type Storage interface {
2525

2626
// Add adds translations for a specific language.
2727
Add(lang string, data map[string]string)
28+
// Merge already existing translations with new
29+
Merge(locales map[string]map[string]string)
2830
}
2931

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

0 commit comments

Comments
 (0)