Skip to content

Commit

Permalink
cmd/snap: include locale when linting description being lower case
Browse files Browse the repository at this point in the history
This helps debug which translation is incorrect and needs to be adjusted when
users run into this and paste the output they see directly to i.e. the forum.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
  • Loading branch information
anonymouse64 committed May 18, 2021
1 parent f33c017 commit d6002db
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/snap/main.go
Expand Up @@ -173,7 +173,7 @@ func lintDesc(cmdName, optName, desc, origDesc string) {
r, _ := utf8.DecodeRuneInString(desc)
// note IsLower != !IsUpper for runes with no upper/lower.
if unicode.IsLower(r) && !strings.HasPrefix(desc, "login.ubuntu.com") && !strings.HasPrefix(desc, cmdName) {
noticef("description of %s's %q is lowercase: %q", cmdName, optName, desc)
noticef("description of %s's %q is lowercase in locale %q: %q", cmdName, optName, i18n.CurrentLocale(), desc)
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/snap/main_test.go
Expand Up @@ -342,9 +342,15 @@ func (s *SnapSuite) TestLintDesc(c *C) {
c.Check(log.String(), HasLen, 0)
log.Reset()

// LintDesc complains about lowercase description.
// LintDesc complains about lowercase description and mentions the locale
// that the system is currently in.
prevValue := os.Getenv("LC_MESSAGES")
os.Setenv("LC_MESSAGES", "en_US")
defer func() {
os.Setenv("LC_MESSAGES", prevValue)
}()
snap.LintDesc("command", "<option>", "description", "")
c.Check(log.String(), testutil.Contains, `description of command's "<option>" is lowercase: "description"`)
c.Check(log.String(), testutil.Contains, `description of command's "<option>" is lowercase in locale "en_US": "description"`)
log.Reset()

// LintDesc does not complain about lowercase description starting with login.ubuntu.com
Expand Down
17 changes: 13 additions & 4 deletions i18n/i18n.go
Expand Up @@ -83,10 +83,7 @@ func bindTextDomain(domain, dir string) {

func setLocale(loc string) {
if loc == "" {
loc = os.Getenv("LC_MESSAGES")
if loc == "" {
loc = os.Getenv("LANG")
}
loc = CurrentLocale()
}
// de_DE.UTF-8, de_DE@euro all need to get simplified
loc = strings.Split(loc, "@")[0]
Expand All @@ -95,6 +92,18 @@ func setLocale(loc string) {
locale = translations.Locale(loc)
}

func CurrentLocale() string {
loc := os.Getenv("LC_MESSAGES")
if loc == "" {
loc = os.Getenv("LANG")
}

loc = strings.Split(loc, "@")[0]
loc = strings.Split(loc, ".")[0]

return loc
}

// G is the shorthand for Gettext
func G(msgid string) string {
return locale.Gettext(msgid)
Expand Down

0 comments on commit d6002db

Please sign in to comment.