Skip to content

Commit

Permalink
update to version 0.0.2: add fallback defaultLang:key on validLang:ke…
Browse files Browse the repository at this point in the history
…y not found
  • Loading branch information
kataras committed Dec 5, 2019
1 parent 0816511 commit 11c5ed3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
6 changes: 6 additions & 0 deletions _examples/custom-loader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func GOI18N(globPattern string) i18n.Loader {
// i18n package matches the language by itself so we don't really need to do it at serve-time,
// this is why we preload them here.
locales[langIndex] = &goi18nLocale{
index: langIndex,
id: tag.String(),
tag: &tag,
localizer: goI18n.NewLocalizer(b, tag.String()),
Expand All @@ -122,11 +123,16 @@ func GOI18N(globPattern string) i18n.Loader {
}

type goi18nLocale struct {
index int
id string
tag *language.Tag
localizer *goI18n.Localizer
}

func (l *goi18nLocale) Index() int {
return l.index
}

func (l *goi18nLocale) Tag() *language.Tag {
return l.tag
}
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Source code and other details for the project are available at GitHub:
Current Version
0.0.1
0.0.2
Installation
Expand Down
19 changes: 17 additions & 2 deletions i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type (
// Locale is the interface which returns from a `Localizer.GetLocale` method.
// It serves the translations based on "key" or format. See `GetMessage`.
Locale interface {
// Index returns the current locale index from the languages list.
Index() int
// Tag returns the full language Tag attached to this Locale,
// it should be unique across different Locales.
Tag() *language.Tag
Expand Down Expand Up @@ -92,6 +94,9 @@ type I18n struct {
Cookie string
// If true then a subdomain can be a language identifier too.
Subdomain bool
// If true then it will return empty string when translation for a a specific language's key was not found.
// Defaults to false, fallback defaultLang:key will be used.
Strict bool
}

// makeTags converts language codes to language Tags.
Expand Down Expand Up @@ -291,7 +296,12 @@ func (i *I18n) Tr(lang, format string, args ...interface{}) string {

loc := i.localizer.GetLocale(index)
if loc != nil {
return loc.GetMessage(format, args...)
msg := loc.GetMessage(format, args...)
if msg == "" && !i.Strict && index > 0 {
// it's not the default/fallback language and not message found for that lang:key.
return i.localizer.GetLocale(0).GetMessage(format, args...)
}
return msg
}

return fmt.Sprintf(format, args...)
Expand Down Expand Up @@ -374,7 +384,12 @@ func GetMessage(r *http.Request, format string, args ...interface{}) string {
func (i *I18n) GetMessage(r *http.Request, format string, args ...interface{}) string {
loc := i.GetLocale(r)
if loc != nil {
return loc.GetMessage(format, args...)
// it's not the default/fallback language and not message found for that lang:key.
msg := loc.GetMessage(format, args...)
if msg == "" && !i.Strict && loc.Index() > 0 {
return i.localizer.GetLocale(0).GetMessage(format, args...)
}
return msg
}

return fmt.Sprintf(format, args...)
Expand Down
10 changes: 8 additions & 2 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func load(assetNames []string, asset func(string) ([]byte, error), options ...Lo

t := m.Languages[langIndex]
locales[langIndex] = &defaultLocale{
index: langIndex,
id: t.String(),
tag: &t,
templateKeys: templateKeys,
Expand Down Expand Up @@ -189,14 +190,19 @@ func (l MemoryLocalizer) SetDefault(index int) bool {
}

type defaultLocale struct {
id string
tag *language.Tag
index int
id string
tag *language.Tag
// templates *template.Template // we could use the ExecuteTemplate too.
templateKeys map[string]*template.Template
lineKeys map[string]string
other map[string]interface{}
}

func (l *defaultLocale) Index() int {
return l.index
}

func (l *defaultLocale) Tag() *language.Tag {
return l.tag
}
Expand Down
8 changes: 8 additions & 0 deletions loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func testLoadAndTrHelper(t *testing.T, i18N *I18n) {
if expected := "ολοκλήρωση παραγγελίας - όλα"; got != expected {
t.Fatalf("expected %s but got %s", expected, got)
}

// test fallback default language's key.
got = i18N.Tr("el-GR", "KeyOnlyOnDefaultLang")
if expected := "value"; got != expected {
t.Fatalf("expected %s but got %s", expected, got)
}

}

func TestLoadEmptyTags(t *testing.T) {
Expand All @@ -74,5 +81,6 @@ func TestLoadEmptyTags(t *testing.T) {
t.Fatal(err)
}

i18N.SetDefault("en-US")
testLoadAndTrHelper(t, i18N)
}
3 changes: 2 additions & 1 deletion testfiles/en-US/other.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"JSONTemplateExample": "value of {{.Value}}",
"TypeOf": "type of %T"
"TypeOf": "type of %T",
"KeyOnlyOnDefaultLang": "value"
}

0 comments on commit 11c5ed3

Please sign in to comment.