| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/xml" |
| 5 |
"fmt" |
| 6 |
"regexp" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"golang.org/x/text/cases" |
| 11 |
"golang.org/x/text/language" |
| 12 |
) |
| 13 |
|
| 14 |
// SupplementalData is the top level struct of plural.xml |
| 15 |
type SupplementalData struct { |
| 16 |
XMLName xml.Name `xml:"supplementalData"` |
| 17 |
PluralGroups []PluralGroup `xml:"plurals>pluralRules"` |
| 18 |
} |
| 19 |
|
| 20 |
// PluralGroup is a group of locales with the same plural rules. |
| 21 |
type PluralGroup struct { |
| 22 |
Locales string `xml:"locales,attr"` |
| 23 |
PluralRules []PluralRule `xml:"pluralRule"` |
| 24 |
} |
| 25 |
|
| 26 |
// Name returns a unique name for this plural group. |
| 27 |
func (pg *PluralGroup) Name() string { |
| 28 |
n := cases.Title(language.AmericanEnglish).String(pg.Locales) |
| 29 |
return strings.ReplaceAll(n, " ", "") |
| 30 |
} |
| 31 |
|
| 32 |
// SplitLocales returns all the locales in the PluralGroup as a slice. |
| 33 |
func (pg *PluralGroup) SplitLocales() []string { |
| 34 |
return strings.Split(pg.Locales, " ") |
| 35 |
} |
| 36 |
|
| 37 |
// PluralRule is the rule for a single plural form. |
| 38 |
type PluralRule struct { |
| 39 |
Count string `xml:"count,attr"` |
| 40 |
Rule string `xml:",innerxml"` |
| 41 |
} |
| 42 |
|
| 43 |
// CountTitle returns the title case of the PluralRule's count. |
| 44 |
func (pr *PluralRule) CountTitle() string { |
| 45 |
return cases.Title(language.AmericanEnglish).String(pr.Count) |
| 46 |
} |
| 47 |
|
| 48 |
// Condition returns the condition where the PluralRule applies. |
| 49 |
func (pr *PluralRule) Condition() string { |
| 50 |
i := strings.Index(pr.Rule, "@") |
| 51 |
return pr.Rule[:i] |
| 52 |
} |
| 53 |
|
| 54 |
// Examples returns the integer and decimal examples for the PluralRule. |
| 55 |
func (pr *PluralRule) Examples() (integers []string, decimals []string) { |
| 56 |
ex := strings.ReplaceAll(pr.Rule, ", …", "") |
| 57 |
ddelim := "@decimal" |
| 58 |
if i := strings.Index(ex, ddelim); i > 0 { |
| 59 |
dex := strings.TrimSpace(ex[i+len(ddelim):]) |
| 60 |
dex = strings.ReplaceAll(dex, "c", "e") |
| 61 |
decimals = strings.Split(dex, ", ") |
| 62 |
ex = ex[:i] |
| 63 |
} |
| 64 |
idelim := "@integer" |
| 65 |
if i := strings.Index(ex, idelim); i > 0 { |
| 66 |
iex := strings.TrimSpace(ex[i+len(idelim):]) |
| 67 |
integers = strings.Split(iex, ", ") |
| 68 |
for j, integer := range integers { |
| 69 |
ii := strings.IndexAny(integer, "eEcC") |
| 70 |
if ii > 0 { |
| 71 |
zeros, err := strconv.ParseInt(integer[ii+1:], 10, 0) |
| 72 |
if err != nil { |
| 73 |
panic(err) |
| 74 |
} |
| 75 |
integers[j] = integer[:ii] + strings.Repeat("0", int(zeros)) |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
return integers, decimals |
| 80 |
} |
| 81 |
|
| 82 |
// IntegerExamples returns the integer examples for the PluralRule. |
| 83 |
func (pr *PluralRule) IntegerExamples() []string { |
| 84 |
integer, _ := pr.Examples() |
| 85 |
return integer |
| 86 |
} |
| 87 |
|
| 88 |
// DecimalExamples returns the decimal examples for the PluralRule. |
| 89 |
func (pr *PluralRule) DecimalExamples() []string { |
| 90 |
_, decimal := pr.Examples() |
| 91 |
return decimal |
| 92 |
} |
| 93 |
|
| 94 |
var relationRegexp = regexp.MustCompile(`([niftvwce])(?:\s*%\s*([0-9]+))?\s*(!=|=)(.*)`) |
| 95 |
|
| 96 |
// GoCondition converts the XML condition to valid Go code. |
| 97 |
func (pr *PluralRule) GoCondition() string { |
| 98 |
var ors []string |
| 99 |
for _, and := range strings.Split(pr.Condition(), "or") { |
| 100 |
var ands []string |
| 101 |
for _, relation := range strings.Split(and, "and") { |
| 102 |
parts := relationRegexp.FindStringSubmatch(relation) |
| 103 |
if parts == nil { |
| 104 |
continue |
| 105 |
} |
| 106 |
lvar := cases.Title(language.AmericanEnglish).String(parts[1]) |
| 107 |
lmod, op, rhs := parts[2], parts[3], strings.TrimSpace(parts[4]) |
| 108 |
if op == "=" { |
| 109 |
op = "==" |
| 110 |
} |
| 111 |
if lvar == "E" { |
| 112 |
// E is a deprecated symbol for C |
| 113 |
// https://unicode.org/reports/tr35/tr35-numbers.html#Plural_Operand_Meanings |
| 114 |
lvar = "C" |
| 115 |
} |
| 116 |
lvar = "ops." + lvar |
| 117 |
var rhor []string |
| 118 |
var rany []string |
| 119 |
for _, rh := range strings.Split(rhs, ",") { |
| 120 |
if parts := strings.Split(rh, ".."); len(parts) == 2 { |
| 121 |
from, to := parts[0], parts[1] |
| 122 |
if lvar == "ops.N" { |
| 123 |
if lmod != "" { |
| 124 |
rhor = append(rhor, fmt.Sprintf("ops.NModInRange(%s, %s, %s)", lmod, from, to)) |
| 125 |
} else { |
| 126 |
rhor = append(rhor, fmt.Sprintf("ops.NInRange(%s, %s)", from, to)) |
| 127 |
} |
| 128 |
} else if lmod != "" { |
| 129 |
rhor = append(rhor, fmt.Sprintf("intInRange(%s %% %s, %s, %s)", lvar, lmod, from, to)) |
| 130 |
} else { |
| 131 |
rhor = append(rhor, fmt.Sprintf("intInRange(%s, %s, %s)", lvar, from, to)) |
| 132 |
} |
| 133 |
} else { |
| 134 |
rany = append(rany, rh) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
if len(rany) > 0 { |
| 139 |
rh := strings.Join(rany, ",") |
| 140 |
if lvar == "ops.N" { |
| 141 |
if lmod != "" { |
| 142 |
rhor = append(rhor, fmt.Sprintf("ops.NModEqualsAny(%s, %s)", lmod, rh)) |
| 143 |
} else { |
| 144 |
rhor = append(rhor, fmt.Sprintf("ops.NEqualsAny(%s)", rh)) |
| 145 |
} |
| 146 |
} else if lmod != "" { |
| 147 |
rhor = append(rhor, fmt.Sprintf("intEqualsAny(%s %% %s, %s)", lvar, lmod, rh)) |
| 148 |
} else { |
| 149 |
rhor = append(rhor, fmt.Sprintf("intEqualsAny(%s, %s)", lvar, rh)) |
| 150 |
} |
| 151 |
} |
| 152 |
r := strings.Join(rhor, " || ") |
| 153 |
if len(rhor) > 1 { |
| 154 |
r = "(" + r + ")" |
| 155 |
} |
| 156 |
if op == "!=" { |
| 157 |
r = "!" + r |
| 158 |
} |
| 159 |
ands = append(ands, r) |
| 160 |
} |
| 161 |
ors = append(ors, strings.Join(ands, " && ")) |
| 162 |
} |
| 163 |
return strings.Join(ors, " ||\n") |
| 164 |
} |