cldr

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2021 License: MIT Imports: 8 Imported by: 2

README

razor-1/CLDR

GitHub Workflow Status Go Report Card Go Version PkgGoDev

This started as a fork of https://github.com/theplant/cldr. It incorporates a few significant changes:

  • Merges data from parent locales in the CLDR. This is not a complete implementation of all rules in http://unicode.org/reports/tr35/#Common_Elements, but it's substantially better than not doing this at all
  • Supports formatting timezone components using the GMT offset format
  • Uses the plural code generation logic from https://github.com/nicksnyder/go-i18n
  • The resources package is refactored to allow easy dynamic loading of locales at runtime

You're welcome to use this package on its own, but it might be easier to use https://github.com/razor-1/localizer. It will allow you to get a locale, populated with this CLDR data, load translations into it, and use it for all your localization needs.

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
}

Calendar is populated with the CLDR calendar data

func (Calendar) FmtDateFull

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

FmtDateFull returns the full date format for the calendar

func (Calendar) FmtDateLong

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

FmtDateLong returns the full date format for the calendar

func (Calendar) FmtDateMedium

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

FmtDateMedium returns the full date format for the calendar

func (Calendar) FmtDateShort

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

FmtDateShort returns the full date format for the calendar

func (Calendar) FmtDateTimeFull

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

FmtDateTimeFull returns the full date format for the calendar

func (Calendar) FmtDateTimeLong

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

FmtDateTimeLong returns the full date format for the calendar

func (Calendar) FmtDateTimeMedium

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

FmtDateTimeMedium returns the full date format for the calendar

func (Calendar) FmtDateTimeShort

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

FmtDateTimeShort returns the full date format for the calendar

func (Calendar) FmtTimeFull

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

FmtTimeFull returns the full date format for the calendar

func (Calendar) FmtTimeLong

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

FmtTimeLong returns the full date format for the calendar

func (Calendar) FmtTimeMedium

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

FmtTimeMedium returns the full date format for the calendar

func (Calendar) FmtTimeShort

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

FmtTimeShort returns the full date format for the calendar

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 }

CalendarDateFormat holds different date format strings

type CalendarDayFormatNameValue

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

CalendarDayFormatNameValue has a localized string for each day of the week

type CalendarDayFormatNames

type CalendarDayFormatNames struct {
	Abbreviated CalendarDayFormatNameValue
	Narrow      CalendarDayFormatNameValue
	Short       CalendarDayFormatNameValue
	Wide        CalendarDayFormatNameValue
}

CalendarDayFormatNames is the different forms of days

type CalendarFormatNames

type CalendarFormatNames struct {
	Months  CalendarMonthFormatNames
	Days    CalendarDayFormatNames
	Periods CalendarPeriodFormatNames
}

CalendarFormatNames contains various data related to localized names for calendar items

type CalendarFormats

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

CalendarFormats is populated with the CLDR calendar data

type CalendarMonthFormatNameValue

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

CalendarMonthFormatNameValue has a localized string for each month

type CalendarMonthFormatNames

type CalendarMonthFormatNames struct {
	Abbreviated CalendarMonthFormatNameValue
	Narrow      CalendarMonthFormatNameValue
	Short       CalendarMonthFormatNameValue
	Wide        CalendarMonthFormatNameValue
}

CalendarMonthFormatNames is the different forms of months

type CalendarPeriodFormatNameValue

type CalendarPeriodFormatNameValue struct {
	AM, PM string
}

CalendarPeriodFormatNameValue has a localized string for each time period

type CalendarPeriodFormatNames

CalendarPeriodFormatNames are the different forms of time periods

type Currencies

type Currencies map[string]Currency

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

type Currency

type Currency struct {
	DisplayName string
	Symbol      string
}

Currency has the basic currency information

type Languages added in v0.1.8

type Languages map[string]string

Languages maps a language code to the localized name of that language

type Locale

type Locale struct {
	Locale      string
	Number      Number
	Calendar    Calendar
	Plural      Plural
	Languages   Languages
	Territories Territories
	Display     LocaleDisplayPattern
}

Locale has all the relevant information from the CLDR for a locale

type LocaleDisplayPattern added in v0.1.13

type LocaleDisplayPattern struct {
	Pattern        string
	Separator      string
	KeyTypePattern string
}

LocaleDisplayPattern indicates how to display locales, e.g. Language (Region), using placeholders {0} ({1})

type Number

type Number struct {
	Symbols    Symbols
	Formats    NumberFormats
	Currencies Currencies
}

Number contains information required for locale-specific number formatting

func (Number) FmtCurrency

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

FmtCurrency 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) FmtCurrencyAccounting added in v0.1.3

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

FmtCurrencyAccounting is the same as FmtCurrency, but using the accounting format in the CLDR. If the accounting format is missing, it falls back on the standard format (the same one used in FmtCurrency).

func (Number) FmtCurrencyAccountingWhole added in v0.1.3

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

FmtCurrencyAccountingWhole is the accounting equivalent of FmtCurrencyWhole. It falls back on the standard format if the accounting format is missing.

func (Number) FmtCurrencyWhole

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

FmtCurrencyWhole does exactly what FmtCurrency 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

FmtNumber 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

FmtNumberWhole 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

FmtPercent 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
	CurrencyAccounting string
	Percent            string
}

NumberFormats contains the different format strings from the CLDR

type Plural

type Plural struct {
	Cardinal PluralData
	Ordinal  PluralData
}

Plural contains both cardinal and ordinal plural data from the CLDR

type PluralData

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

PluralData contains information about the plural forms for the locale and how to get plural operators

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
}

Symbols has the various CLDR symbols for the locale

type Territories added in v0.1.8

type Territories map[string]string

Territories maps a territory code to the localized name of the territory

Directories

Path Synopsis
cmd
Code generated by make_resources.go; DO NOT EDIT Code generated by make_resources.go; DO NOT EDIT.
Code generated by make_resources.go; DO NOT EDIT Code generated by make_resources.go; DO NOT EDIT.
currency
Code generated by make_resources.go; DO NOT EDIT.
Code generated by make_resources.go; DO NOT EDIT.
internal/locales
Code generated by make_resources.go; DO NOT EDIT.
Code generated by make_resources.go; DO NOT EDIT.
language
Code generated by make_resources.go; DO NOT EDIT.
Code generated by make_resources.go; DO NOT EDIT.
territory
Code generated by make_resources.go; DO NOT EDIT.
Code generated by make_resources.go; DO NOT EDIT.
locales Module

Jump to

Keyboard shortcuts

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