| 1 |
// Package strings provides string manipulation utilities. |
| 2 |
package strings |
| 3 |
|
| 4 |
// The so-called spoken language join here works well for some Western |
| 5 |
// languages. PRs for other languages are welcome, but do note that |
| 6 |
// implementation for some languages will be less straightforward than the ones |
| 7 |
// in use here. |
| 8 |
|
| 9 |
import ( |
| 10 |
"strings" |
| 11 |
) |
| 12 |
|
| 13 |
// Language is a spoken Language. |
| 14 |
type Language int |
| 15 |
|
| 16 |
// Available spoken lanaguges. |
| 17 |
const ( |
| 18 |
DE Language = iota |
| 19 |
DK |
| 20 |
EN |
| 21 |
ES |
| 22 |
FR |
| 23 |
IT |
| 24 |
NO |
| 25 |
PT |
| 26 |
SE |
| 27 |
) |
| 28 |
|
| 29 |
// String returns the English name of the [Language] code. |
| 30 |
func (l Language) String() string { |
| 31 |
return map[Language]string{ |
| 32 |
DE: "German", |
| 33 |
DK: "Danish", |
| 34 |
EN: "English", |
| 35 |
ES: "Spanish", |
| 36 |
FR: "French", |
| 37 |
IT: "Italian", |
| 38 |
NO: "Norwegian", |
| 39 |
PT: "Portuguese", |
| 40 |
SE: "Swedish", |
| 41 |
}[l] |
| 42 |
} |
| 43 |
|
| 44 |
func (l Language) conjunction() string { |
| 45 |
switch l { |
| 46 |
case DE: |
| 47 |
return "und" |
| 48 |
case DK: |
| 49 |
return "og" |
| 50 |
case EN: |
| 51 |
return "and" |
| 52 |
case ES: |
| 53 |
return "y" |
| 54 |
case FR: |
| 55 |
return "et" |
| 56 |
case NO: |
| 57 |
return "og" |
| 58 |
case IT: |
| 59 |
return "e" |
| 60 |
case PT: |
| 61 |
return "e" |
| 62 |
case SE: |
| 63 |
return "och" |
| 64 |
default: |
| 65 |
return "" |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func (l Language) separator() string { |
| 70 |
switch l { |
| 71 |
case DE, DK, EN, ES, FR, NO, IT, PT, SE: |
| 72 |
return ", " |
| 73 |
default: |
| 74 |
return " " |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// EnglishJoin joins a slice of strings with commas and the "and" conjugation |
| 79 |
// before the final item. The Oxford comma can optionally be applied. |
| 80 |
// |
| 81 |
// Example: |
| 82 |
// |
| 83 |
// str := EnglishJoin([]string{"meow", "purr", "raow"}, true) |
| 84 |
// fmt.Println(str) // meow, purr, and raow |
| 85 |
func EnglishJoin(words []string, oxfordComma bool) string { |
| 86 |
return spokenLangJoin(words, EN, oxfordComma) |
| 87 |
} |
| 88 |
|
| 89 |
// SpokenLanguageJoin joins a slice of strings with commas and a conjunction |
| 90 |
// before the final item. You may specify the language with [Language]. |
| 91 |
// |
| 92 |
// If you are using English and need the Oxford Comma, use [EnglishJoin]. |
| 93 |
// |
| 94 |
// Example: |
| 95 |
// |
| 96 |
// str := SpokenLanguageJoin([]string{"eins", "zwei", "drei"}, DE) |
| 97 |
// fmt.Println(str) // eins, zwei und drei |
| 98 |
func SpokenLanguageJoin(words []string, language Language) string { |
| 99 |
return spokenLangJoin(words, language, false) |
| 100 |
} |
| 101 |
|
| 102 |
func spokenLangJoin(words []string, language Language, oxfordComma bool) string { |
| 103 |
conjunction := language.conjunction() + " " |
| 104 |
separator := language.separator() |
| 105 |
|
| 106 |
b := strings.Builder{} |
| 107 |
for i, word := range words { |
| 108 |
if word == "" { |
| 109 |
continue |
| 110 |
} |
| 111 |
|
| 112 |
if i == 0 { |
| 113 |
b.WriteString(word) |
| 114 |
continue |
| 115 |
} |
| 116 |
|
| 117 |
// Is this the final word? |
| 118 |
if len(words) > 1 && i == len(words)-1 { |
| 119 |
// Apply the Oxford comma if requested as long as the language is |
| 120 |
// English. |
| 121 |
if language == EN && oxfordComma && i > 1 { |
| 122 |
b.WriteString(separator) |
| 123 |
} else { |
| 124 |
b.WriteRune(' ') |
| 125 |
} |
| 126 |
|
| 127 |
b.WriteString(conjunction + word) |
| 128 |
continue |
| 129 |
} |
| 130 |
|
| 131 |
b.WriteString(separator + word) |
| 132 |
} |
| 133 |
return b.String() |
| 134 |
} |