Skip to content

Commit

Permalink
Merge pull request #4 from imbue11235/feature/pluralize-bounds
Browse files Browse the repository at this point in the history
[Feature] Translations with bounds
  • Loading branch information
imbue11235 authored Dec 1, 2021
2 parents 1eb1a12 + 4673e74 commit 358d69f
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 97 deletions.
46 changes: 23 additions & 23 deletions locale/da/locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@ var Code = "da"
var Locale = locale.Map{
"slice": locale.Map{
"connector": "og",
"rest": "èn anden|%d andre",
"rest": "[1] èn anden|[2-*] %d andre",
},
"time": locale.Map{
"now": "lige nu",
"future": "om %s",
"past": "%s siden",
"estimation": locale.Map{
"s": "et sekund|%d sekunder",
"m": "et minut|%d minutter",
"h": "en time|%d timer",
"d": "en dag|%d dage",
"w": "en uge|%d uger",
"M": "en måned|%d måneder",
"y": "et år|%d år",
"D": "et årti|%d årtier",
"s": "[0-5] et par sekunder|[6-*] %d sekunder",
"m": "[1] et minut|[2-*] %d minutter",
"h": "[1] en time|[2-*] %d timer",
"d": "[1] en dag|[2-*] %d dage",
"w": "[1] en uge|[2-*] %d uger",
"M": "[1] en måned|[2-*] %d måneder",
"y": "[1] et år|[2-*] %d år",
"D": "[1] et årti|[2-*] %d årtier",
"l": "lang tid",
},
"precision": locale.Map{
"s": "1 sekund|%d sekunder",
"m": "1 minut|%d minutter",
"h": "1 time|%d timer",
"d": "1 dag|%d dage",
"M": "1 måned|%d måneder",
"y": "1 år|%d år",
"s": "[1] 1 sekund|[2-*] %d sekunder",
"m": "[1] 1 minut|[2-*] %d minutter",
"h": "[1] 1 time|[2-*] %d timer",
"d": "[1] 1 dag|[2-*] %d dage",
"M": "[1] 1 måned|[2-*] %d måneder",
"y": "[1] 1 år|[2-*] %d år",
},
},
"int": locale.Map{
"K": "tusind|tusinde",
"M": "million|millioner",
"B": "milliard|milliarder",
"T": "billion|billioner",
"Q": "billiard|billiarder",
"Qi": "trillion|trillioner",
"Sx": "trilliard|trilliarder",
"Sp": "kvadrillion|kvadrillioner",
"K": "[1] tusind|[2-*] tusinde",
"M": "[1] million|[2-*] millioner",
"B": "[1] milliard|[2-*] milliarder",
"T": "[1] billion|[2-*] billioner",
"Q": "[1] billiard|[2-*] billiarder",
"Qi": "[1] trillion|[2-*] trillioner",
"Sx": "[1] trilliard|[2-*] trilliarder",
"Sp": "[1] kvadrillion|[2-*] kvadrillioner",
},
}
44 changes: 22 additions & 22 deletions locale/de/locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@ var Code = "de"
var Locale = locale.Map{
"slice": locale.Map{
"connector": "und",
"rest": "ein anderer|%d andere",
"rest": "[1] ein anderer|[2-*] %d andere",
},
"time": locale.Map{
"now": "gerade jetzt",
"future": "in %s",
"past": "vor %s",
"estimation": locale.Map{
"s": "eine Sekunde|%d Sekunden",
"m": "eine Minute|%d Minuten",
"h": "eine Stunde|%d Stunden",
"d": "ein Tag|%d Tage",
"w": "eine Woche|%d Wochen",
"M": "ein Monat|%d Monate",
"y": "ein Jahr|%d Jahre",
"D": "ein Jahrzehnt|%d Jahrzehnte",
"s": "[1] eine Sekunde|[2-*] %d Sekunden",
"m": "[1] eine Minute|[2-*] %d Minuten",
"h": "[1] eine Stunde|[2-*] %d Stunden",
"d": "[1] ein Tag|[2-*] %d Tage",
"w": "[1] eine Woche|[2-*] %d Wochen",
"M": "[1] ein Monat|[2-*] %d Monate",
"y": "[1] ein Jahr|[2-*] %d Jahre",
"D": "[1] ein Jahrzehnt|[2-*] %d Jahrzehnte",
"l": "lange Zeit",
},
"precision": locale.Map{
"s": "1 Sekunde|%d Sekunden",
"m": "1 Minute|%d Minuten",
"h": "1 Stunde|%d Stunden",
"d": "1 Tag|%d Tage",
"M": "1 Monat|%d Monate",
"y": "1 Jahr|%d Jahre",
"s": "[1] 1 Sekunde|[2-*] %d Sekunden",
"m": "[1] 1 Minute|[2-*] %d Minuten",
"h": "[1] 1 Stunde|[2-*] %d Stunden",
"d": "[1] 1 Tag|[2-*] %d Tage",
"M": "[1] 1 Monat|[2-*] %d Monate",
"y": "[1] 1 Jahr|[2-*] %d Jahre",
},
},
"int": locale.Map{
"K": "tausend",
"M": "Million|Millionen",
"B": "Milliarde|Milliarden",
"T": "Billion|Billionen",
"Q": "Billiarde|Billiarden",
"Qi": "Trillion|Trillionen",
"Sx": "Sextillion|Sextillionen",
"Sp": "Septillion|Septillionen",
"M": "[1] Million|[2-*] Millionen",
"B": "[1] Milliarde|[2-*] Milliarden",
"T": "[1] Billion|[2-*] Billionen",
"Q": "[1] Billiarde|[2-*] Billiarden",
"Qi": "[1] Trillion|[2-*] Trillionen",
"Sx": "[1] Sextillion|[2-*] Sextillionen",
"Sp": "[1] Septillion|[2-*] Septillionen",
},
}
30 changes: 15 additions & 15 deletions locale/en/locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ var Code = "en"
var Locale = locale.Map{
"slice": locale.Map{
"connector": "and",
"rest": "one other|%d others",
"rest": "[1] one other|[2-*] %d others",
},
"time": locale.Map{
"now": "just now",
"future": "in %s",
"past": "%s ago",
"estimation": locale.Map{
"s": "a second|%d seconds",
"m": "a minute|%d minutes",
"h": "an hour|%d hours",
"d": "a day|%d days",
"w": "a week|%d weeks",
"M": "a month|%d months",
"y": "a year|%d years",
"D": "a decade|%d decades",
"s": "[0-5] a few seconds|[6-*] %d seconds",
"m": "[1] a minute|[2-*] %d minutes",
"h": "[1] an hour|[2-*] %d hours",
"d": "[1] a day|[2-*] %d days",
"w": "[1] a week|[2-*] %d weeks",
"M": "[1] a month|[2-*] %d months",
"y": "[1] a year|[2-*] %d years",
"D": "[1] a decade|[2-*] %d decades",
"l": "a long time",
},
"precision": locale.Map{
"s": "1 second|%d seconds",
"m": "1 minute|%d minutes",
"h": "1 hour|%d hours",
"d": "1 day|%d days",
"M": "1 month|%d months",
"y": "1 year|%d years",
"s": "[1] 1 second|[2-*] %d seconds",
"m": "[1] 1 minute|[2-*] %d minutes",
"h": "[1] 1 hour|[2-*] %d hours",
"d": "[1] 1 day|[2-*] %d days",
"M": "[1] 1 month|[2-*] %d months",
"y": "[1] 1 year|[2-*] %d years",
},
},
"int": locale.Map{
Expand Down
35 changes: 18 additions & 17 deletions locale/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package locale

import (
"fmt"
"strings"
)

// Manager ...
Expand Down Expand Up @@ -41,6 +40,23 @@ func (m *Manager) getTranslationOrFallback(path string) string {
return translation
}

func (m *Manager) getPluralizationOrFallback(path string, count int) string {
pluralizer, err := m.currentTranslator.getPluralizer(path)

if err != nil {
// at this point, we will try to use the fallback translator
// and as a last option, falling back to the default fallback string
// if everything else fails
pluralizer, err = m.fallbackTranslator.getPluralizer(path)

if err != nil {
return m.getTranslationOrFallback(path)
}
}

return pluralizer.apply(count)
}

// Translate ...
func (m *Manager) Translate(path string, args ...interface{}) string {
translation := m.getTranslationOrFallback(path)
Expand All @@ -54,22 +70,7 @@ func (m *Manager) Translate(path string, args ...interface{}) string {

// Pluralize ...
func (m *Manager) Pluralize(path string, count int) string {
translation := m.getTranslationOrFallback(path)
parts := strings.Split(translation, pluralSeparator)

if count != 1 && len(parts) > 1 {
return m.applyCountToTranslation(parts[1], count)
}

return parts[0]
}

func (m *Manager) applyCountToTranslation(translation string, count int) string {
if strings.Contains(translation, "%d") {
return fmt.Sprintf(translation, count)
}

return translation
return m.getPluralizationOrFallback(path, count)
}

func (m *Manager) addTranslator(code string, translations Map) *translator {
Expand Down
30 changes: 16 additions & 14 deletions locale/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func TestManagerTranslate(t *testing.T) {

func TestManagerPluralize(t *testing.T) {
manager := NewManager(WithLocale("default", Map{
"dollar": "I only have 1 dollar|I have %d dollars!!!!",
"dollar": "[1] I only have 1 dollar|[2-*] I have %d dollars!!!!",
"time": Map{
"minutes": "one minute|%d minutes",
"minutes": "[1] one minute|[2-*] %d minutes",
},
"normal": "Everything is normal",
}))
Expand All @@ -58,6 +58,7 @@ func TestManagerPluralize(t *testing.T) {
{"time.minutes", "2 minutes", 2},
{"normal", "Everything is normal", 1},
{"normal", "Everything is normal", 100},
{"none.existing.path", "", 1},
}

for _, test := range tests {
Expand Down Expand Up @@ -90,21 +91,22 @@ func TestManagerSetLocale(t *testing.T) {
}
}

func TestManagerApplyCountToTranslation(t *testing.T) {
func TestManagerRegisterLocale(t *testing.T) {
manager := NewManager()

tests := []struct {
text string
count int
expected string
}{
{"%d bananas", 55, "55 bananas"},
{"1 banana", 55, "1 banana"},
if err := manager.RegisterLocale("1", Map{}); err != nil {
t.Errorf("expected no error, but got: %s", err)
}

for _, test := range tests {
if actual := manager.applyCountToTranslation(test.text, test.count); actual != test.expected {
t.Errorf("expected `%s`, but got `%s`", test.expected, actual)
}
if err := manager.RegisterLocale("1", Map{}); err == nil {
t.Error("expected error, but got none")
}
}

func TestManagerSetFallbackLocale(t *testing.T) {
manager := NewManager()

if err := manager.SetFallbackLocale("1"); err == nil {
t.Error("expected error, but got none")
}
}
Loading

0 comments on commit 358d69f

Please sign in to comment.