Skip to content

Commit 6777757

Browse files
committed
temp
1 parent 376396a commit 6777757

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

modules/translation/plural_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package translation
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"golang.org/x/text/feature/plural"
13+
"golang.org/x/text/language"
14+
"golang.org/x/text/message/catalog"
15+
)
16+
17+
func matchInt(l language.Tag, v any) plural.Form {
18+
digits := []byte(fmt.Sprint(v))
19+
for i := range digits {
20+
digits[i] -= '0'
21+
}
22+
return plural.Cardinal.MatchDigits(l, digits, len(digits), 0)
23+
}
24+
25+
var formsAll = []plural.Form{
26+
plural.Zero,
27+
plural.One,
28+
plural.Two,
29+
plural.Few,
30+
plural.Many,
31+
plural.Other,
32+
}
33+
34+
func supportedForms(l language.Tag) (forms []plural.Form) {
35+
cat := catalog.NewBuilder()
36+
for _, form := range formsAll {
37+
err := cat.Set(l, "%d test", plural.Selectf(1, "%d", form, "%d test"))
38+
if err == nil {
39+
forms = append(forms, form)
40+
}
41+
}
42+
return forms
43+
}
44+
45+
func TrPlural(langTag language.Tag, format string, args ...any) string {
46+
// HINT: this is for demo purpose only, not optimized
47+
form := plural.Other
48+
forms := supportedForms(langTag)
49+
for _, arg := range args {
50+
switch arg.(type) {
51+
case int, int64:
52+
form = matchInt(langTag, arg)
53+
}
54+
}
55+
p1 := strings.Index(format, "$[")
56+
p2 := strings.Index(format, "]")
57+
if p1 != -1 && p2 != -1 && p1 < p2 {
58+
words := strings.Split(format[p1+2:p2], ",")
59+
word := words[len(words)-1]
60+
for i := range words {
61+
if forms[i] == form {
62+
word = words[i]
63+
}
64+
}
65+
format = format[:p1] + strings.TrimSpace(word) + format[p2+1:]
66+
}
67+
return fmt.Sprintf(format, args...)
68+
}
69+
70+
func TestPlural(t *testing.T) {
71+
msg := TrPlural(language.English, "%d $[one, other]", 0)
72+
assert.Equal(t, "0 other", msg)
73+
msg = TrPlural(language.English, "%d $[one, other]", 1)
74+
assert.Equal(t, "1 one", msg)
75+
msg = TrPlural(language.English, "%d $[one, other]", 2)
76+
assert.Equal(t, "2 other", msg)
77+
78+
msg = TrPlural(language.Latvian, "%d $[zero, one, other]", 0)
79+
assert.Equal(t, "0 zero", msg)
80+
msg = TrPlural(language.Latvian, "%d $[zero, one, other]", 1)
81+
assert.Equal(t, "1 one", msg)
82+
msg = TrPlural(language.Latvian, "%d $[zero, one, other]", 2)
83+
assert.Equal(t, "2 other", msg)
84+
85+
msg = TrPlural(language.Arabic, "%d $[zero, one, two, few, many, other]", 0)
86+
assert.Equal(t, "0 zero", msg)
87+
msg = TrPlural(language.Arabic, "%d $[zero, one, two, few, many, other]", 1)
88+
assert.Equal(t, "1 one", msg)
89+
msg = TrPlural(language.Arabic, "%d $[zero, one, two, few, many, other]", 2)
90+
assert.Equal(t, "2 two", msg)
91+
msg = TrPlural(language.Arabic, "%d $[zero, one, two, few, many, other]", 3)
92+
assert.Equal(t, "3 few", msg)
93+
msg = TrPlural(language.Arabic, "%d $[zero, one, two, few, many, other]", 11)
94+
assert.Equal(t, "11 many", msg)
95+
msg = TrPlural(language.Arabic, "%d $[zero, one, two, few, many, other]", 100)
96+
assert.Equal(t, "100 other", msg)
97+
}

0 commit comments

Comments
 (0)