feat(i18n): add current plural category accessor
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
60bb0e91e8
commit
4d8d075189
2 changed files with 30 additions and 0 deletions
|
|
@ -105,6 +105,14 @@ func CurrentDirection() TextDirection {
|
|||
// IsRTL returns true if the current language uses right-to-left text.
|
||||
func IsRTL() bool { return Direction() == DirRTL }
|
||||
|
||||
// CurrentPluralCategory returns the plural category for the current default language.
|
||||
func CurrentPluralCategory(n int) PluralCategory {
|
||||
if svc := Default(); svc != nil {
|
||||
return svc.PluralCategory(n)
|
||||
}
|
||||
return GetPluralCategory("en", n)
|
||||
}
|
||||
|
||||
func detectLanguage(supported []language.Tag) string {
|
||||
langEnv := os.Getenv("LANG")
|
||||
if langEnv == "" {
|
||||
|
|
|
|||
|
|
@ -218,6 +218,28 @@ func TestIsRTL_Good(t *testing.T) {
|
|||
assert.False(t, IsRTL(), "English should not be RTL")
|
||||
}
|
||||
|
||||
// --- Package-level CurrentPluralCategory ---
|
||||
|
||||
func TestCurrentPluralCategory_Good(t *testing.T) {
|
||||
prev := Default()
|
||||
t.Cleanup(func() {
|
||||
SetDefault(prev)
|
||||
})
|
||||
|
||||
svc, err := New()
|
||||
require.NoError(t, err)
|
||||
SetDefault(svc)
|
||||
|
||||
assert.Equal(t, PluralOther, CurrentPluralCategory(0))
|
||||
assert.Equal(t, PluralOne, CurrentPluralCategory(1))
|
||||
assert.Equal(t, PluralOther, CurrentPluralCategory(2))
|
||||
|
||||
require.NoError(t, SetLanguage("fr"))
|
||||
assert.Equal(t, PluralOne, CurrentPluralCategory(0))
|
||||
assert.Equal(t, PluralOne, CurrentPluralCategory(1))
|
||||
assert.Equal(t, PluralOther, CurrentPluralCategory(2))
|
||||
}
|
||||
|
||||
// --- detectLanguage ---
|
||||
|
||||
func TestDetectLanguage_Good(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue