Skip to content

Commit

Permalink
refactor: use text/language to match ISO-639 & BCP-47 standards (#10)
Browse files Browse the repository at this point in the history
* test: added more tests

Signed-off-by: Carlos A Becker <[email protected]>

* refactor: use text/language to match ISO-639 & BCP-47 standards

Co-authored-by: Carlos A Becker <[email protected]>
Co-authored-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
3 people authored Mar 18, 2022
1 parent 852ad96 commit 6d4858d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 51 deletions.
60 changes: 40 additions & 20 deletions cmd/melt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/tyler-smith/go-bip39/wordlists"
"golang.org/x/crypto/ssh"
"golang.org/x/term"
lang "golang.org/x/text/language"
"golang.org/x/text/language/display"
)

const (
Expand Down Expand Up @@ -266,27 +268,45 @@ func setLanguage(language string) error {
return nil
}

func sanitizeLang(s string) string {
return strings.ReplaceAll(strings.ToLower(s), " ", "-")
}

var wordLists = map[lang.Tag][]string{
lang.Chinese: wordlists.ChineseSimplified,
lang.SimplifiedChinese: wordlists.ChineseSimplified,
lang.TraditionalChinese: wordlists.ChineseTraditional,
lang.Czech: wordlists.Czech,
lang.AmericanEnglish: wordlists.English,
lang.BritishEnglish: wordlists.English,
lang.English: wordlists.English,
lang.French: wordlists.French,
lang.Italian: wordlists.Italian,
lang.Japanese: wordlists.Japanese,
lang.Korean: wordlists.Korean,
lang.Spanish: wordlists.Spanish,
lang.EuropeanSpanish: wordlists.Spanish,
lang.LatinAmericanSpanish: wordlists.Spanish,
}

func getWordlist(language string) []string {
switch strings.ToLower(language) {
case "chinese-simplified", "zh", "zh_hans":
return wordlists.ChineseSimplified
case "chinese-traditional", "zh_hant":
return wordlists.ChineseTraditional
case "czech", "cs":
return wordlists.Czech
case "english", "en":
return wordlists.English
case "french", "fr":
return wordlists.French
case "italian", "it":
return wordlists.Italian
case "japanese", "ja":
return wordlists.Japanese
case "korean", "ko":
return wordlists.Korean
case "spanish", "es":
return wordlists.Spanish
default:
language = sanitizeLang(language)
tag := lang.Make(language)
en := display.English.Languages() // default language name matcher
for t := range wordLists {
if sanitizeLang(en.Name(t)) == language {
tag = t
break
}
}
if tag == lang.Und { // Unknown language
return nil
}
base, _ := tag.Base()
btag := lang.MustParse(base.String())
wl := wordLists[tag]
if wl == nil {
return wordLists[btag]
}
return wl
}
69 changes: 38 additions & 31 deletions cmd/melt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,42 @@ func TestBackupRestoreKnownKey(t *testing.T) {
}

func TestGetWordlist(t *testing.T) {
for lang, wordlist := range map[string][]string{
"cHinese-simplified": wordlists.ChineseSimplified,
"zH": wordlists.ChineseSimplified,
"zH_haNs": wordlists.ChineseSimplified,
"cHinese-tradITIONAL": wordlists.ChineseTraditional,
"zH_hanT": wordlists.ChineseTraditional,
"cZech": wordlists.Czech,
"cS": wordlists.Czech,
"eN": wordlists.English,
"eNglish": wordlists.English,
"fRench": wordlists.French,
"fR": wordlists.French,
"iTaliaN": wordlists.Italian,
"iT": wordlists.Italian,
"jApanesE": wordlists.Japanese,
"jA": wordlists.Japanese,
"kORean": wordlists.Korean,
"kO": wordlists.Korean,
"sPanish": wordlists.Spanish,
"eS": wordlists.Spanish,
"ES": wordlists.Spanish,
"sOmething": nil,
} {
t.Run(lang, func(t *testing.T) {
is := is.New(t)
is.Equal(wordlist, getWordlist(lang))
})
}
}
for lang, wordlist := range map[string][]string{
"cHinese": wordlists.ChineseSimplified,
"simplified-cHinese": wordlists.ChineseSimplified,
"zH": wordlists.ChineseSimplified,
"zH_haNs": wordlists.ChineseSimplified,
"tradITIONAL-cHinese": wordlists.ChineseTraditional,
"zH_hanT": wordlists.ChineseTraditional,
"cZech": wordlists.Czech,
"cS": wordlists.Czech,
"eN": wordlists.English,
"eN-gb": wordlists.English,
"eNglish": wordlists.English,
"american-eNglish": wordlists.English,
"british-eNglish": wordlists.English,
"fRench": wordlists.French,
"fR": wordlists.French,
"iTaliaN": wordlists.Italian,
"iT": wordlists.Italian,
"jApanesE": wordlists.Japanese,
"jA": wordlists.Japanese,
"kORean": wordlists.Korean,
"kO": wordlists.Korean,
"sPanish": wordlists.Spanish,
"eS": wordlists.Spanish,
"eS-ER": wordlists.Spanish,
"european-spanish": wordlists.Spanish,
"ES": wordlists.Spanish,
"zz": nil,
"sOmething": nil,
} {
t.Run(lang, func(t *testing.T) {
is := is.New(t)
is.Equal(wordlist, getWordlist(lang))
})
}
}

func TestBackupRestoreKnownKeyInJapanse(t *testing.T) {
const expectedMnemonic = `
Expand All @@ -119,11 +126,11 @@ func TestBackupRestoreKnownKeyInJapanse(t *testing.T) {
const expectedFingerprint = "SHA256:tX0ZrsNLIB/ZlRK3vy/HsWIIkyBNhYhCSGmtqtxJcWo"

// set language to Japanse
setLanguage("japanese")
setLanguage("ja")

// set language back to English
t.Cleanup(func() {
setLanguage("english")
setLanguage("en")
})

t.Run("backup", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/tyler-smith/go-bip39 v1.1.0
golang.org/x/crypto v0.0.0-20220307211146-efcb8507fb70
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
golang.org/x/text v0.3.6
)

require (
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit 6d4858d

Please sign in to comment.