Skip to content

Commit

Permalink
Merge pull request #1 from cyx/optimize-no-substitution-case
Browse files Browse the repository at this point in the history
Optimize no substitution case
  • Loading branch information
m1 authored Apr 11, 2021
2 parents f004b52 + 40c39e5 commit 07d5b91
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
14 changes: 11 additions & 3 deletions examples/localizations/localizations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
12 changes: 10 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"html/template"
"text/template"
)

var packageTemplate = template.Must(template.New("").Parse(`// Code generated by go-localize; DO NOT EDIT.
Expand All @@ -13,6 +13,7 @@ package {{ .Package }}
import (
"bytes"
"fmt"
"strings"
"text/template"
)
Expand All @@ -25,7 +26,7 @@ var localizations = map[string]string{
type Replacements map[string]interface{}
type Localizer struct {
Locale string
Locale string
FallbackLocale string
Localizations map[string]string
}
Expand Down Expand Up @@ -60,6 +61,13 @@ func (t Localizer) GetWithLocale(locale, key string, replacements ...*Replacemen
return key
}
}
// If the str doesn't have any substitutions, no need to
// template.Execute.
if strings.Index(str, "}}") == -1 {
return str
}
return t.replace(str, replacements...)
}
Expand Down

0 comments on commit 07d5b91

Please sign in to comment.