1
// Copyright 2013 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
11
// Formula provides the language's standard plural formula.
12
func Formula(lang string) func(n int) int {
13
if idx := index(lang); idx != -1 {
14
return formulaTable[fmtForms(FormsTable[idx].Value)]
16
if idx := index("??"); idx != -1 {
17
return formulaTable[fmtForms(FormsTable[idx].Value)]
19
return func(n int) int {
24
func index(lang string) int {
25
for i := 0; i < len(FormsTable); i++ {
26
if strings.HasPrefix(lang, FormsTable[i].Lang) {
33
func fmtForms(forms string) string {
34
forms = strings.TrimSpace(forms)
35
forms = strings.Replace(forms, " ", "", -1)
39
var formulaTable = map[string]func(n int) int{
40
fmtForms("nplurals=n; plural=n-1;"): func(n int) int {
46
fmtForms("nplurals=1; plural=0;"): func(n int) int {
49
fmtForms("nplurals=2; plural=(n != 1);"): func(n int) int {
55
fmtForms("nplurals=2; plural=(n > 1);"): func(n int) int {
61
fmtForms("nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2);"): func(n int) int {
62
if n%10 == 1 && n%100 != 11 {
70
fmtForms("nplurals=3; plural=n==1 ? 0 : n==2 ? 1 : 2;"): func(n int) int {
79
fmtForms("nplurals=3; plural=n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2;"): func(n int) int {
83
if n == 0 || (n%100 > 0 && n%100 < 20) {
88
fmtForms("nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2);"): func(n int) int {
89
if n%10 == 1 && n%100 != 11 {
92
if n%10 >= 2 && (n%100 < 10 || n%100 >= 20) {
97
fmtForms("nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);"): func(n int) int {
98
if n%10 == 1 && n%100 != 11 {
101
if n%10 >= 2 && n%10 <= 4 && (n%100 < 10 || n%100 >= 20) {
106
fmtForms("nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);"): func(n int) int {
107
if n%10 == 1 && n%100 != 11 {
110
if n%10 >= 2 && n%10 <= 4 && (n%100 < 10 || n%100 >= 20) {
115
fmtForms("nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);"): func(n int) int {
116
if n%10 == 1 && n%100 != 11 {
119
if n%10 >= 2 && n%10 <= 4 && (n%100 < 10 || n%100 >= 20) {
124
fmtForms("nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);"): func(n int) int {
125
if n%10 == 1 && n%100 != 11 {
128
if n%10 >= 2 && n%10 <= 4 && (n%100 < 10 || n%100 >= 20) {
133
fmtForms("nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);"): func(n int) int {
134
if n%10 == 1 && n%100 != 11 {
137
if n%10 >= 2 && n%10 <= 4 && (n%100 < 10 || n%100 >= 20) {
142
fmtForms("nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;"): func(n int) int {
146
if n >= 2 && n <= 4 {
151
fmtForms("nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;"): func(n int) int {
155
if n >= 2 && n <= 4 {
160
fmtForms("nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);"): func(n int) int {
164
if n%10 >= 2 && n%10 <= 4 && (n%100 < 10 || n%100 >= 20) {
169
fmtForms("nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3);"): func(n int) int {
176
if n%100 == 3 || n%100 == 4 {