Skip to content

Commit e89b669

Browse files
committed
Merge pull request #39 from dennisfaust/master
Add LanguageTags and LanguageTranslationIDs functions
2 parents a97d9e6 + 7ba4181 commit e89b669

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

i18n/bundle/bundle.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,24 @@ func (b *Bundle) Translations() map[string]map[string]translation.Translation {
134134
return b.translations
135135
}
136136

137+
// LanguageTags returns the tags of all languages that that have been added.
138+
func (b *Bundle) LanguageTags() []string {
139+
var tags []string
140+
for k := range b.translations {
141+
tags = append(tags, k)
142+
}
143+
return tags
144+
}
145+
146+
// LanguageTranslationIDs returns the ids of all translations that have been added for a given language.
147+
func (b *Bundle) LanguageTranslationIDs(languageTag string) []string {
148+
var ids []string
149+
for id := range b.translations[languageTag] {
150+
ids = append(ids, id)
151+
}
152+
return ids
153+
}
154+
137155
// MustTfunc is similar to Tfunc except it panics if an error happens.
138156
func (b *Bundle) MustTfunc(pref string, prefs ...string) TranslateFunc {
139157
tfunc, err := b.Tfunc(pref, prefs...)

i18n/bundle/bundle_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"fmt"
55
"testing"
66

7+
"reflect"
8+
"sort"
9+
710
"github.com/nicksnyder/go-i18n/i18n/language"
811
"github.com/nicksnyder/go-i18n/i18n/translation"
912
)
@@ -33,6 +36,31 @@ func TestMustTfunc(t *testing.T) {
3336
New().MustTfunc("invalid")
3437
}
3538

39+
func TestLanguageTagsAndTranslationIDs(t *testing.T) {
40+
b := New()
41+
translationID := "translation_id"
42+
englishLanguage := languageWithTag("en-US")
43+
frenchLanguage := languageWithTag("fr-FR")
44+
spanishLanguage := languageWithTag("es")
45+
addFakeTranslation(t, b, englishLanguage, "English"+translationID)
46+
addFakeTranslation(t, b, frenchLanguage, translationID)
47+
addFakeTranslation(t, b, spanishLanguage, translationID)
48+
49+
tags := b.LanguageTags()
50+
sort.Strings(tags)
51+
compareTo := []string{englishLanguage.Tag, spanishLanguage.Tag, frenchLanguage.Tag}
52+
if !reflect.DeepEqual(tags, compareTo) {
53+
t.Errorf("LanguageTags() = %#v; expected: %#v", tags, compareTo)
54+
}
55+
56+
ids := b.LanguageTranslationIDs(englishLanguage.Tag)
57+
sort.Strings(ids)
58+
compareTo = []string{"English" + translationID}
59+
if !reflect.DeepEqual(ids, compareTo) {
60+
t.Errorf("LanguageTranslationIDs() = %#v; expected: %#v", ids, compareTo)
61+
}
62+
}
63+
3664
func TestTfuncAndLanguage(t *testing.T) {
3765
b := New()
3866
translationID := "translation_id"

i18n/i18n.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ func AddTranslation(lang *language.Language, translations ...translation.Transla
115115
defaultBundle.AddTranslation(lang, translations...)
116116
}
117117

118+
// LanguageTags returns the tags of all languages that have been added.
119+
func LanguageTags() []string {
120+
return defaultBundle.LanguageTags()
121+
}
122+
123+
// LanguageTranslationIDs returns the ids of all translations that have been added for a given language.
124+
func LanguageTranslationIDs(languageTag string) []string {
125+
return defaultBundle.LanguageTranslationIDs(languageTag)
126+
}
127+
118128
// MustTfunc is similar to Tfunc except it panics if an error happens.
119129
func MustTfunc(languageSource string, languageSources ...string) TranslateFunc {
120130
return TranslateFunc(defaultBundle.MustTfunc(languageSource, languageSources...))

0 commit comments

Comments
 (0)