diff --git a/pkg/locales/locale.go b/pkg/locales/locale.go index 22a8905ab57c36e13268f67ca90e047ebb486673..f45f1d85ca9688e4b89e47d58f92412245d02f65 100644 --- a/pkg/locales/locale.go +++ b/pkg/locales/locale.go @@ -27,6 +27,10 @@ func (locale *Locale) IsDefault() bool { return locale.ID == DefaultID } +func (locale *Locale) Equal(other *Locale) bool { + return locale == other || locale != nil && other != nil && *locale == *other +} + // Возвращает язык локали, например "en", "ru" func (locale *Locale) GetLanguage() string { lang, err := language.Parse(locale.Code) diff --git a/pkg/locales/locale_test.go b/pkg/locales/locale_test.go index 18a030603a01aa5879cc20c5d5161a08bbb6888d..5875107a120b71ff4c89eb0f28cc79c6cd225586 100644 --- a/pkg/locales/locale_test.go +++ b/pkg/locales/locale_test.go @@ -36,3 +36,46 @@ func TestLocale_GetLanguage(t *testing.T) { }) } } + +func TestLocale_Equal(t *testing.T) { + tests := []struct { + locale *Locale + other *Locale + want bool + }{ + { + locale: &Locale{Code: "ru-RU"}, + other: &Locale{Code: "ru-RU"}, + want: true, + }, + { + locale: &Locale{Code: "ru-RU"}, + other: &Locale{Code: "ru-RU", Fallback: "en-US"}, + want: false, + }, + { + locale: &Locale{Code: "ru-RU", Fallback: "en-US"}, + other: &Locale{Code: "ru-RU"}, + want: false, + }, + { + locale: &Locale{Code: "ru-RU"}, + other: nil, + want: false, + }, + { + locale: nil, + other: &Locale{Code: "ru-RU"}, + want: false, + }, + { + locale: nil, + other: nil, + want: true, + }, + } + for _, tt := range tests { + got := tt.locale.Equal(tt.other) + require.Equal(t, tt.want, got) + } +}