Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add language getter for Locale #77

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ func (l *Locale) SetDomain(dom string) {
l.Unlock()
}

// GetLanguage is the lang getter for Locale configuration
func (l *Locale) GetLanguage() string {
l.RLock()
lang := l.lang
l.RUnlock()
return lang
}

// Get uses a domain "default" to return the corresponding Translation of a given string.
// Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
func (l *Locale) Get(str string, vars ...interface{}) string {
Expand Down
30 changes: 30 additions & 0 deletions locale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ msgstr "More Translation"
// Create Locale with full language code
l := NewLocale("/tmp", "en_US")

// Test language
language := l.GetLanguage()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me know if you think I should have a dedicate test block for this instead

if language != "en_US" {
t.Errorf("Expected 'en_US' but got '%s'", language)
}

// Force nil domain storage
l.Domains = nil

Expand Down Expand Up @@ -293,6 +299,12 @@ msgstr "More Translation"
// Create Locale with full language code
l = NewLocale("/tmp", "golem")

// Test language
language = l.GetLanguage()
if language != "golem" {
t.Errorf("Expected 'golem' but got '%s'", language)
}

// Force nil domain storage
l.Domains = nil

Expand Down Expand Up @@ -323,6 +335,12 @@ msgstr "More Translation"
// Create Locale with full language code
l = NewLocale("fixtures/", "fr_FR")

// Test language
language = l.GetLanguage()
if language != "fr_FR" {
t.Errorf("Expected 'fr_FR' but got '%s'", language)
}

// Force nil domain storage
l.Domains = nil

Expand Down Expand Up @@ -353,6 +371,12 @@ msgstr "More Translation"
// Create Locale with full language code
l = NewLocale("fixtures/", "de_DE")

// Test language
language = l.GetLanguage()
if language != "de_DE" {
t.Errorf("Expected 'de_DE' but got '%s'", language)
}

// Force nil domain storage
l.Domains = nil

Expand Down Expand Up @@ -383,6 +407,12 @@ msgstr "More Translation"
// Create Locale with full language code
l = NewLocale("fixtures/", "de_AT")

// Test language
language = l.GetLanguage()
if language != "de_AT" {
t.Errorf("Expected 'de_AT' but got '%s'", language)
}

// Force nil domain storage
l.Domains = nil

Expand Down