From a57e999da2ea699905f49cfad8bd9b4c174e6050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Lindstr=C3=B6m?= Date: Mon, 7 Oct 2019 09:21:50 +0200 Subject: [PATCH] Add language substitution for swedish (#37) This adds a language substitution for & and @ in swedish. --- languages_substitution.go | 7 ++++++- slug.go | 2 ++ slug_test.go | 11 ++++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/languages_substitution.go b/languages_substitution.go index 2e4d914..3ba3bcd 100644 --- a/languages_substitution.go +++ b/languages_substitution.go @@ -8,7 +8,7 @@ package slug func init() { // Merge language subs with the default one for _, sub := range []*map[rune]string{ - &deSub, &enSub, &esSub, &fiSub, &grSub, &nlSub, &plSub, + &deSub, &enSub, &esSub, &fiSub, &grSub, &nlSub, &plSub, &svSub, } { for key, value := range defaultSub { (*sub)[key] = value @@ -79,6 +79,11 @@ var plSub = map[rune]string{ '@': "na", } +var svSub = map[rune]string{ + '&': "och", + '@': "snabel a", +} + var trSub = map[rune]string{ '&': "ve", '@': "et", diff --git a/slug.go b/slug.go index 22637e7..b14f7d8 100644 --- a/slug.go +++ b/slug.go @@ -71,6 +71,8 @@ func MakeLang(s string, lang string) (slug string) { slug = SubstituteRune(slug, nlSub) case "pl", "pol": slug = SubstituteRune(slug, plSub) + case "sv", "swe": + slug = SubstituteRune(slug, svSub) case "tr", "tur": slug = SubstituteRune(slug, trSub) default: // fallback to "en" if lang not found diff --git a/slug_test.go b/slug_test.go index 45d398c..911ccf4 100644 --- a/slug_test.go +++ b/slug_test.go @@ -63,9 +63,9 @@ func TestSlugMake(t *testing.T) { func TestSlugMakeLang(t *testing.T) { testCases := []struct { - lang string - in string - want string + lang string + in string + want string lowercase bool }{ {"de", "Wir mögen Bücher & Käse", "wir-moegen-buecher-und-kaese", true}, @@ -91,6 +91,10 @@ func TestSlugMakeLang(t *testing.T) { {"nl", "This & that", "this-en-that", true}, {"pl", "This & that", "this-i-that", true}, {"pol", "This & that", "this-i-that", true}, + {"sv", "This & that", "this-och-that", true}, + {"sv", "This @ that", "this-snabel-a-that", true}, + {"swe", "This & that", "this-och-that", true}, + {"swe", "This @ that", "this-snabel-a-that", true}, {"tr", "This & that", "this-ve-that", true}, {"test", "This & that", "this-and-that", true}, // unknown lang, fallback to "en" // Test defaultSub, when adding new lang copy/paste this line, @@ -127,6 +131,7 @@ func TestSlugMakeUserSubstituteLang(t *testing.T) { {map[string]string{"&": "or"}, "de", "This & that", "this-or-that"}, // by default "&" => "und" {map[string]string{"&": "or"}, "DEU", "This & that", "this-or-that"}, // by default "&" => "und" {map[string]string{"&": "or"}, "Fin", "This & that", "this-or-that"}, // by default "&" => "ja" + {map[string]string{"&": "or", "@": "the"}, "sv", "@ This & that", "the-this-or-that"}, // by default "&" => "och", "@" => "snabel a" {map[string]string{"&": "or", "@": "the"}, "de", "@ This & that", "the-this-or-that"}, // by default "&" => "und", "@" => "an" }