Ejemplo n.º 1
0
// Get tries to retrieve a currency
func (p ConfigCurrency) Get(sg config.ScopedGetter) (Currency, error) {
	cur := p.Str.Get(sg)
	u, err := currency.ParseISO(cur)
	if err != nil {
		return Currency{}, errgo.Mask(err)
	}
	return Currency{Unit: u}, nil
}
Ejemplo n.º 2
0
// BaseCurrencyCode returns the base currency code of a website TODO.
func (w *Website) BaseCurrencyCode() (currency.Currency, error) {
	var c string
	if w.Config.GetString(PathPriceScope) == PriceScopeGlobal {
		c, _ = w.cr.GetString(config.Path(directory.PathCurrencyBase)) // TODO check for error
	} else {
		c = w.Config.GetString(directory.PathCurrencyBase)
	}
	return currency.ParseISO(c)
}
Ejemplo n.º 3
0
// Format returns a money format struct with the money int64 value, as well as curency code and symbol
// NOTE that `symbol(code)` is a `currency.Value` and has a `Format()` function but it seems easier to use fmt.
func (m *Money) Format(isoCode string) (MoneyFormat, error) {
	var mf = MoneyFormat{MoneyVal: m}
	code, err := currency.ParseISO(isoCode)
	if err != nil {
		return mf, err
	}
	mf.IsoCode = code.String()
	mf.Symbol = fmt.Sprintf("%v", currencySymbolFunc(code))
	return mf, nil
}
Ejemplo n.º 4
0
// SetCurrencyISO parses a 3-letter ISO 4217 code and sets it to the Currency
// struct. If parsing fails errors will be logged and falls back to DefaultCurrencyName.
// Calling this function sets also the CurrencySign() to the at the moment
// 3-letter ISO code. (Missing feature in text/currency package)
// This function is called in NewCurrency().
func SetCurrencyISO(cur string) CurrencyOptions {
	return func(c *Currency) CurrencyOptions {
		previous := c.ISO.String()
		lc, err := currency.ParseISO(cur)
		if err != nil {
			if PkgLog.IsDebug() {
				PkgLog.Debug("i18n.CurrencyISO.ParseCurrency.error", "err", err, "cur", cur)
			}
			lc = currency.MustParseISO(DefaultCurrencyName)
		}
		c.ISO = lc
		SetCurrencySign([]byte(lc.String()))(c)
		return SetCurrencyISO(previous)
	}
}