qor / i18n

I18n is a golang implementation, provides internationalization support for your application, with different backends support
MIT License
105 stars 25 forks source link

Pluralization doesn't work #16

Open adhatama opened 6 years ago

adhatama commented 6 years ago

I have a simple pluralization like this:

lang/en-US.yml

en-US:
  demo:
    plural: '{{p "Count" (one "{{.Count}} item") (other "{{.Count}} items")}}'
main.go

I18n := i18n.New(
    yaml.New("lang"),
)
val := I18n.T(
    "en-US",
    "demo.plural",
    map[string]int{"Count": 1},
)
fmt.Println(val)

The printed value of val is 1 items where it should be 1 item Did I miss something?

shoamano83 commented 5 years ago

Hi, looking at CLDR document https://github.com/theplant/cldr#how-to-use, I think we need to import locale files.

These worked for me with en locale:

lang/en.yaml

en:
  demo:
    plural: '{{p "Count" (one "{{.Count}} item") (other "{{.Count}} items")}}'

main.go

package main

import (
        "fmt"
        "github.com/qor/i18n"
        "github.com/qor/i18n/backends/yaml"
        _ "github.com/theplant/cldr/resources/locales" // this one!
)

func main() {
        I18n := i18n.New(
                yaml.New("lang"),
        )
        val := I18n.T(
                "en",
                "demo.plural",
                map[string]int{"Count": 1},
        )
        fmt.Println(val)
}

Result:

$ go run main.go 
1 item

However, I couldn't figure out how to use it with en-US locale. It looks like CLDR has en but not en-US.