cldr

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2020 License: MIT Imports: 8 Imported by: 2

README

CLDR

GoDoc

cldr is a golang library using Common Locale Data Repository to format dates, plurals (and more in the future), inspired by twitter-cldr-rb and borrowing some codes from github.com/vube/i18n.

How to use

cldr embeds CLDR data in pure go and it doesn't import all those locale data by default. If you are using specific locale data, you can import that package as bellow:

package main

import (
	"github.com/theplant/cldr"
	_ "github.com/theplant/cldr/resources/locales/en"
)

func main() {
	cldr.Parse(
		"en",
		`{{p "Count" (one "{{.Count}} item") (other "{{.Count}} items")}}`,
		map[string]int{"Count": 1},
	) // "1 item in Your Cart"
}

If you don't like hand-importing locales, you can import github.com/theplant/cldr/resources/locales, which import all available locales in cldr pacakge.

More API could be found here.

How to add locales

cldr.RegisterLocale(Locale{...})

How to override default locales

// solution 1
// using the same locale name

import _ github.com/theplant/cldr/resources/locales/en
cldr.RegisterLocale(Locale{Locale: "en"})

// solution 2
// update the exported locale directly

import github.com/theplant/cldr/resources/locales/en
en.Locale.PluralRule = "2A"

Documentation

Index

Constants

View Source
const (
	DateFormatFull = iota
	DateFormatLong
	DateFormatMedium
	DateFormatShort
	TimeFormatFull
	TimeFormatLong
	TimeFormatMedium
	TimeFormatShort
	DateTimeFormatFull
	DateTimeFormatLong
	DateTimeFormatMedium
	DateTimeFormatShort
)

Standard Formats for Dates, Times & DateTimes These are the options to pass to the Format method.

Variables

This section is empty.

Functions

This section is empty.

Types

type Calendar

type Calendar struct {
	Formats     CalendarFormats
	FormatNames CalendarFormatNames
}

func (Calendar) FmtDateFull

func (c Calendar) FmtDateFull(t time.Time) (string, error)

func (Calendar) FmtDateLong

func (c Calendar) FmtDateLong(t time.Time) (string, error)

func (Calendar) FmtDateMedium

func (c Calendar) FmtDateMedium(t time.Time) (string, error)

func (Calendar) FmtDateShort

func (c Calendar) FmtDateShort(t time.Time) (string, error)

func (Calendar) FmtDateTimeFull

func (c Calendar) FmtDateTimeFull(t time.Time) (string, error)

func (Calendar) FmtDateTimeLong

func (c Calendar) FmtDateTimeLong(t time.Time) (string, error)

func (Calendar) FmtDateTimeMedium

func (c Calendar) FmtDateTimeMedium(t time.Time) (string, error)

func (Calendar) FmtDateTimeShort

func (c Calendar) FmtDateTimeShort(t time.Time) (string, error)

func (Calendar) FmtTimeFull

func (c Calendar) FmtTimeFull(t time.Time) (string, error)

func (Calendar) FmtTimeLong

func (c Calendar) FmtTimeLong(t time.Time) (string, error)

func (Calendar) FmtTimeMedium

func (c Calendar) FmtTimeMedium(t time.Time) (string, error)

func (Calendar) FmtTimeShort

func (c Calendar) FmtTimeShort(t time.Time) (string, error)

func (Calendar) Format

func (c Calendar) Format(datetime time.Time, pattern string) (string, error)

Format takes a time struct and a format and returns a formatted string. Callers should use a DateFormat, TimeFormat, or DateTimeFormat constant.

type CalendarDateFormat

type CalendarDateFormat struct{ Full, Long, Medium, Short string }

type CalendarDayFormatNameValue

type CalendarDayFormatNameValue struct {
	Sun, Mon, Tue, Wed, Thu, Fri, Sat string
}

type CalendarFormatNames

type CalendarFormatNames struct {
	Months  CalendarMonthFormatNames
	Days    CalendarDayFormatNames
	Periods CalendarPeriodFormatNames
}

type CalendarFormats

type CalendarFormats struct {
	Date     CalendarDateFormat
	Time     CalendarDateFormat
	DateTime CalendarDateFormat
	GMT      string
}

type CalendarMonthFormatNameValue

type CalendarMonthFormatNameValue struct {
	Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec string
}

type CalendarPeriodFormatNameValue

type CalendarPeriodFormatNameValue struct {
	AM, PM string
}

type Currencies

type Currencies map[string]Currency

maps a currency code (e.g. "USD") to a Currency

type Currency

type Currency struct {
	DisplayName string
	Symbol      string
}

type Locale

type Locale struct {
	Locale   string
	Number   Number
	Calendar Calendar
	Plural   Plural
}

type Number

type Number struct {
	Symbols    Symbols
	Formats    NumberFormats
	Currencies Currencies
}

func (Number) FmtCurrency

func (n Number) FmtCurrency(currency string, number float64) (formatted string, err error)

FormatCurrency takes a float number and a currency key and returns a string with a properly formatted currency amount with the correct currency symbol. If a symbol cannot be found for the reqested currency, the the key is used instead. If the currency key requested is not recognized, it is used as the symbol, and an error is returned with the formatted string.

func (Number) FmtCurrencyWhole

func (n Number) FmtCurrencyWhole(currency string, number float64) (formatted string, err error)

FormatCurrencyWhole does exactly what FormatCurrency does, but it leaves off any decimal places. AKA, it would return $100 rather than $100.00.

func (Number) FmtNumber

func (n Number) FmtNumber(number float64) string

FormatNumber takes a float number and returns a properly formatted string representation of that number according to the locale's number format.

func (Number) FmtNumberWhole

func (n Number) FmtNumberWhole(number float64) string

FormatNumberWhole does exactly what FormatNumber does, but it leaves off any decimal places. AKA, it would return 100 rather than 100.01.

func (Number) FmtPercent

func (n Number) FmtPercent(number float64) string

FormatPercent takes a float number and returns a properly formatted string representation of that number as a percentage according to the locale's percentage format.

type NumberFormats

type NumberFormats struct {
	Decimal  string
	Currency string
	Percent  string
}

type Plural

type Plural struct {
	Cardinal PluralData
	Ordinal  PluralData
}

type PluralData

type PluralData struct {
	Forms []plural.Form
	Func  func(ops *PluralOperands) plural.Form
}

type PluralOperands

type PluralOperands struct {
	N float64 // absolute value of the source number (integer and decimals)
	I int64   // integer digits of n
	V int64   // number of visible fraction digits in n, with trailing zeros
	W int64   // number of visible fraction digits in n, without trailing zeros
	F int64   // visible fractional digits in n, with trailing zeros
	T int64   // visible fractional digits in n, without trailing zeros
}

PluralOperands is a representation of http://unicode.org/reports/tr35/tr35-numbers.html#Operands

func NewOperands

func NewOperands(number interface{}) (*PluralOperands, error)

NewOperands returns the operands for number.

func (*PluralOperands) NEqualsAny

func (o *PluralOperands) NEqualsAny(any ...int64) bool

NEqualsAny returns true if o represents an integer equal to any of the arguments.

func (*PluralOperands) NInRange

func (o *PluralOperands) NInRange(from, to int64) bool

NInRange returns true if o represents an integer in the closed interval [from, to].

func (*PluralOperands) NModEqualsAny

func (o *PluralOperands) NModEqualsAny(mod int64, any ...int64) bool

NModEqualsAny returns true if o represents an integer equal to any of the arguments modulo mod.

func (*PluralOperands) NModInRange

func (o *PluralOperands) NModInRange(mod, from, to int64) bool

NModInRange returns true if o represents an integer in the closed interval [from, to] modulo mod.

type Symbols

type Symbols struct {
	Decimal  string
	Group    string
	Negative string
	Percent  string
	PerMille string
}

Directories

Path Synopsis
cmd
resources module
currency Module
language Module
locales Module
territory Module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL