예제 #1
0
파일: plural.go 프로젝트: beatgammit/ginta
func (p pluralStem) Convert(l ginta.Locale, input interface{}) interface{} {
	base := PluralStemResourcesPath + string(p)
	bundle := l.GetResourceBundle(base)

	if f, convert := value(input); convert {
		priority := nothingFoundGlobal
		var translation string

		for key, val := range bundle {
			var nextPriority int
			var op func(float64) bool
			switch {
			case strings.HasPrefix(key, EqualOperation):
				op = equals(key[len(EqualOperation):])
				nextPriority = 0
			case strings.HasPrefix(key, GreaterEqualOperation):
				op = greaterEqual(key[len(GreaterEqualOperation):])
				nextPriority = 1
			case strings.HasPrefix(key, LessEqualOperation):
				op = lessEqual(key[len(LessEqualOperation):])
				nextPriority = 2
			case strings.HasPrefix(key, GreaterOperation):
				op = greater(key[len(GreaterOperation):])
				nextPriority = 3
			case strings.HasPrefix(key, LessOperation):
				op = less(key[len(LessOperation):])
				nextPriority = 4
			case strings.HasPrefix(key, RangeOperation):
				op = inRange(key[len(RangeOperation):])
				nextPriority = 5
			case strings.HasPrefix(key, ModuloOperation):
				op = moduleVal(key[len(ModuloOperation):])
				nextPriority = 6

			case key == DefaultOperation:
				op = _true
				nextPriority = nothingFoundGlobal - 1
			}

			if op != nil && nextPriority < priority && op(f) {
				priority = nextPriority
				translation = val
			}
		}

		if priority < nothingFoundGlobal {
			return translation
		}
	}

	return input

}
예제 #2
0
파일: time.go 프로젝트: beatgammit/ginta
// Evaluates the format stored under the provided hierarchical key, performing formatting and substitutions
// as defined in the current locale
func EvaluateFormat(format common.HierarchicalKey, locale ginta.Locale, instant time.Time) string {
	fmtString, err := locale.ResolveResource(format)
	if err == nil {
		result := instant.Format(fmtString)

		bundle := locale.ResolveResourceBundle(SubstitutionsResourceBundle)

		// now perform substitutions for strings (wednesday -> miércoles)
		for from, to := range bundle {
			result = strings.Replace(result, from, to, 1)
		}

		return result
	}

	return "?" + string(format)
}
예제 #3
0
파일: fmt.go 프로젝트: beatgammit/ginta
func (err *errorResource) LocalError(loc i18n.Locale) string {
	var str string
	var lookupErr error
	if str, lookupErr = loc.GetResource(err.key); err == nil {
		str = ApplyFormat(loc, str, err.arguments...)
	} else {
		if notFound, ok := lookupErr.(common.ResourceNotFoundError); ok {
			if notFoundTemplate, err2 := loc.GetResource(common.ResourceNotFoundResourceKey); err2 == nil {
				str = ApplyFormat(loc, notFoundTemplate, string(notFound))
			} else {
				str = notFound.Error()
			}
		} else {
			str = lookupErr.Error()
		}
	}

	return str
}