From 4e2b67f4e694d0248f8ff2db088856b05dea5fd7 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Sat, 29 Jan 2022 21:41:28 -0800 Subject: Allow hyphens in locale name Fixes gohugoio/hugo#9446 --- locales.go | 2 +- locales_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/locales.go b/locales.go index c43f1be..b14a70f 100644 --- a/locales.go +++ b/locales.go @@ -20,7 +20,7 @@ var ( // GetTranslator gets the Translator for the given locale, nil if not found. func GetTranslator(locale string) locales.Translator { - locale = strings.ToLower(locale) + locale = strings.ToLower(strings.ReplaceAll(locale, "-", "_")) mu.RLock() t, found := translators[locale] diff --git a/locales_test.go b/locales_test.go index 44a029f..5f20082 100644 --- a/locales_test.go +++ b/locales_test.go @@ -52,6 +52,12 @@ func TestGetTranslator(t *testing.T) { c.Assert(tnn.MonthWide(d.Month()), qt.Equals, "januar") }) + c.Run("BasicWithHyphenatedKey", func(c *qt.C) { + tnn := GetTranslator("nn-NO") + c.Assert(tnn, qt.Not(qt.IsNil)) + c.Assert(tnn.MonthWide(d.Month()), qt.Equals, "januar") + }) + // Sample tests; verify that the compression script works correctly. c.Run("Sample", func(c *qt.C) { assertSame(c, GetTranslator("en"), en.New()) -- cgit v1.2.3